Imagine this project scaffold:
- utils.js
- module1/controllers.js
- module1/services.js
- module2/controllers.js
- module2/services.js
utils.js
define([], function(){ /* My utils functions */ });
module1/controllers.js
define(['../utils'], function(utils){ /* Module1 stuff */ });
module2/controllers.js
define(['../utils'], function(utils){ /* Module2 stuff */ });
This works very well in a non-optimization context, because utils.js is downloaded only once, no matter whether is from module1 or module2. However I need to optimize and package each module in a JS file.
In order to get this, I add a main.js file in each module, for example:
module1/main.js
define(['./controllers', './services.js'], function(){});
Now I start playing with the optimization tool, this is my app.build.js
...
modules: [
{ name: "module1/main" },
{ name: "module2/main" },
]
...
Ok, I get the behaviour I wanted, but I see that both modules include the utils.js code. This behaviour is right because you can't guarantee the loading order.
So, these are my questions:
- Does exist any smart solution for this?
- If this can't be skipped, does anybody know how requirejs works under the hood? Could this strategy generate any kind of problems?