Bug: UMD bundle template is too tricky for its own good. (CJS failure) #471
Description
Greets Guy et al,
I'm definitely glad there is a UMD bundle option now. However, the template implementation is just being a bit too tricky for its own good. In particular it is the deps.map(require)) call which fails. In particular it fails for my backbone-parse-es6 library.
The solution is to not be tricky and essentially duplicate the AMD and CJS templates removing the deps.map(require))
statement.
I have tested the following and it works fine for the sfx-umd.js
template:
(function(factory) {
if (typeof define == 'function' && define.amd)
define(${JSON.stringify(deps)}, factory);
else if (typeof module == 'object' && module.exports && typeof require == 'function')
module.exports = factory(${deps.map(function(dep) {
return 'require("' + dep + '")';
}).join(', ')});
else
${ deps.length && !globalDeps.length
? 'throw new Error("Module must be loaded as AMD or CommonJS")'
: (globalName ? globalName + ' = ' : '') + 'factory(' + (globalDeps.length ? globalDeps.join(', ') : '') + ')'};
});
In particular if you are interested though in the error here it is:
system.src.js:4837 Uncaught Error: Module underscore not declared as a dependency.(anonymous function) @ system.src.js:4837a @ system.src.js:4837(anonymous function) @ backbone-parse.js:10054(anonymous function) @ backbone-parse.js:2(anonymous function) @ backbone-parse.js:10048(anonymous function) @ backbone-parse.js:10058$ @ system.src.js:4837d.execute @ system.src.js:4837s @ system.src.js:4837n @ system.src.js:4837execute @ system.src.js:4837y @ system.src.js:4837x @ system.src.js:4837p @ system.src.js:4837h @ system.src.js:4837(anonymous function) @ system.src.js:4837
indexSrc.html:1 Uncaught (in promise) Uncaught Error: Module underscore not declared as a dependency.
Evaluating http://localhost:63342/backbone-parse-es6-todos/backbone-parse.js
Error loading http://localhost:63342/backbone-parse-es6-todos/main.js
The generated output which fails is this:
(function(factory) {
var deps = ["underscore","jquery","parse","parse/lib/browser/encode"];
if (typeof define == 'function' && define.amd)
define(deps, factory);
else if (typeof module == 'object' && module.exports && typeof require == 'function')
module.exports = factory.apply(null, deps.map(require));
else
throw new Error("Module must be loaded as AMD or CommonJS");
});
While the UMD template I listed above which essentially duplicates the relevant part of the AMD / CJS templates produces this which works:
(function(factory) {
if (typeof define == 'function' && define.amd)
define(["underscore","parse","jquery","parse/lib/browser/encode"], factory);
else if (typeof module == 'object' && module.exports && typeof require == 'function')
module.exports = factory(require("underscore"), require("parse"), require("jquery"), require("parse/lib/browser/encode"));
else
throw new Error("Module must be loaded as AMD or CommonJS");
});
I can create a PR for this change after receiving your input. I don't see any problem / conflict with this change.
Regards...