Last active
March 28, 2017 21:01
-
-
Save ben-ng/4dee217ced0ac72a8de802ac93460356 to your computer and use it in GitHub Desktop.
AMD silently misbehaving on Salesforce Marketing Cloud
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define('foo', function () { | |
function Foo () { | |
Write('Hello World'); | |
}; | |
return Foo; | |
}) | |
require(['foo'], function (Foo) { | |
// Foo is undefined on Salesforce | |
var f = new Foo(); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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