Closed michaelficarra closed 10 years ago
root.moduleName
should be always exported, even if the module is also exported as a module :(
Also, I don't think it's a Cjs-Everywhere issue (see my comment on Constellation/escodegen#115).
Okay, how about this?
(function(root, factory) {
if (typeof exports === 'object') {
module.exports = factory();
} else {
if (typeof define === 'function' && define.amd)
define('moduleName', [], factory);
root.moduleName = factory();
/* or, if -x was not provided,
if (typeof define === 'function' && define.amd)
define([], factory);
factory();
*/
}
}(this, function() {
// ...
return require('mainModule');
}));
There is an issue in a Require.js context : the factory will be called twice. With a bit more code :
(function(root, factory) {
if (typeof exports === 'object') {
module.exports = factory();
} else {
var instance = root.moduleName = factory();
if (typeof define === 'function' && define.amd) {
define('moduleName', [], function () { return instance; });
}
/* or, if -x was not provided,
var instance = factory();
if (typeof define === 'function' && define.amd) {
define([], function () { return instance; });
}
*/
}
}(this, function() {
// ...
return require('mainModule');
}));
Okay, that works for me.
When
--amd
is given, we should output a wrapper like this:(adapted from umdjs/umd)
Requested by @Constellation in https://github.com/Constellation/escodegen/issues/115