I am trying to combine two require amd modules into one combined.js javascript file using Webpack.
module1.js
//module1.js - objectTest1
(function (root, factory) {
'use strict';
//Universal Module Definition
if (typeof define === 'function' && define.amd) {
// AMD
define(factory);
} else if (typeof exports === 'object') {
// Node, CommonJS-like
module.exports = factory();
} else {
// Browser globals (root is window)
root.returnExports = factory();
}
}(this, function () {
'use strict';
return function objectTest1() {
var test = '1';
return test;
};
}));
module2.js
//module2.js - objectTest2
(function (root, factory) {
'use strict';
//Universal Module Definition
if (typeof define === 'function' && define.amd) {
// AMD
define(factory);
} else if (typeof exports === 'object') {
// Node, CommonJS-like
module.exports = factory();
} else {
// Browser globals (root is window)
root.returnExports = factory();
}
}(this, function () {
'use strict';
return function objectTest2() {
var test = '2';
return test;
};
}));
Recommended good practice
In requireJS wiki and other sources, as a good practice, they explicitly recommend not to set a name for each module, but instead leave it anonymous without an id: https://github.com/requirejs/requirejs/wiki/Updating-existing-libraries#anon
"Normally you should not register a named module, but instead register as an anonymous module..."
WebPack - Combining the modules in one js file
[webpack.config.js]
module.exports= {
entry: [
"./modules/module1.js",
"./modules/module2.js"
],
output: {
filename:"combined.js",
libraryTarget: "umd",
umdNamedDefine: true
}
};
[Ejecute WebPack combine]
webpack.js
webpack 2.5.1
Time: 64ms
Asset Size Chunks Chunk Names
combined.js 5.11 kB 0 [emitted] main
[0] ./modules/module1.js 551 bytes {0} [built]
[1] ./modules/module2.js 551 bytes {0} [built]
[2] multi ./modules/module1.js ./modules/module2.js 40 bytes {0} [built]
WebPack - Output - combined.js
Trying to load combined.js file and modules with RequireJS
[index.html]
require(["combined", "combined/modules/module1", "combined/modules/module2"],
function (combined, objectTest1, objectTest2) {
//combined = objectTest1
//
//objectTest1 = objectTest1 - OK!
//
//objectTest2 = objectTest1 - **WRONG**
//
}
);
Problem
When loading the combined.js file, I am always getting the first anonymous module defined in the combined file. And I do not know how to get the second module, its never set in requireJS variable: window.requirejs.s.contexts._.defined
Additional Info
If I build the modules with a 'id name' in the define('name',..) of each module, it works perfectly, and I can load them perfectly, but as explained above, its not a good practice to name your modules.
Questions
- How can I combine those ANONYMOUS modules in one combined.js file, load that file with requireJS and then get each module?
- Could there be something wrong in the code?
- I have looked into the requireJS variable: window.requirejs.s.contexts._.defined to look for all the modules, but the second module is never added there. So I suppose it could be a problem of the UMD pattern I am using or a feature not supported by Webpack.
I am getting totally desesperate trying to solve it and already looked through many resources but could not find a clear answer. Thanks a lot