Skip to content

Instantly share code, notes, and snippets.

@ben-ng ben-ng/a-repro.js
Last active Mar 28, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.