Skip to content

Instantly share code, notes, and snippets.

@ben-ng
Last active March 28, 2017 21:01
Show Gist options
  • Save ben-ng/4dee217ced0ac72a8de802ac93460356 to your computer and use it in GitHub Desktop.
Save ben-ng/4dee217ced0ac72a8de802ac93460356 to your computer and use it in GitHub Desktop.
AMD silently misbehaving on Salesforce Marketing Cloud
define('foo', function () {
function Foo () {
Write('Hello World');
};
return Foo;
})
require(['foo'], function (Foo) {
// Foo is undefined on Salesforce
var f = new Foo();
})
// The workaround is to /not/ use RequireJS, and instead implement
// just the parts of the runtime that we need.
var moduleMap = {};
function resolve (deps) {
var resolvedDeps = [];
var resolved;
for (var i=0, ii=deps.length; i<ii; ++i){
resolved = moduleMap[deps[i]];
if (resolved === undefined)
throw new Error('Etsy RequireJS shim: could not find module ' + deps[i]);
resolvedDeps.push(resolved);
}
return resolvedDeps;
}
function define (name, deps, fn) {
if (typeof name !== 'string')
throw new Error('Etsy RequireJS shim: you must specify a module name');
if (fn == null) {
fn = deps;
deps = [];
}
if (typeof fn !== 'function')
throw new Error('Etsy RequireJS shim: fn should be a Function');
if (moduleMap[name] !== undefined)
throw new Error('Etsy RequireJS shim: duplicate definition of ' + name);
moduleMap[name] = fn.apply(null, resolve(deps));
}
define.amd = true;
function require (config, deps, fn) {
if (fn == null) {
fn = deps;
deps = config;
config = null;
}
if (deps == null) {
fn = deps;
deps = [];
}
if (typeof fn !== 'function')
throw new Error('Etsy RequireJS shim: fn should be a Function');
fn.apply(null, resolve(deps));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment