I'm confused about my r.js optimised script. I imagine the answer to my question is in the documentation but I think I'm going doc-blind from staring at it too long.
My application has a directory structure like this
-index.htm
-js/app.js
-js/init.js
-js/appname/*.js
When in non-optimised mode index.htm contains the following line:
<script type="text/javascript" data-main="js/app" src="js/lib/require-2.1.11.js"></script>
and everything works fine. My abridged js/app.js
script looks like this:
requirejs.config({
baseUrl: 'js',
paths: {
...
}
});
require(['init']);
When I build the optimised script I specify js/app.js
as the mainConfigFile
and everything builds as expected. However when I update my script tag's data-main
attribute to the build product my application doesn't initialise.
If I manually execute require(['init'])
in the console it starts up as expected. Because r.js is using js/app.js
as its config file that doesn't get included in the optimised script, which means my require(['init'])
also doesn't get included.
I thought I could fix this by moving require(['init'])
to the end of js/init.js
and this does fix the optimised build (it initialised as expected), but now that the call isn't in js/app.js
the non-optimised version never initialises. If I include the call in both files I get an error.
How can I ensure my first module is required after either the optimised or non-optimised file(s) are loaded? I don't understand how I'm supposed to make that first call after my first module's dependencies have fully loaded.
Because r.js is using js/app.js as its config file that doesn't get included in the optimised script
Well, then modify the build config you pass to r.js
so that js/app.js
is included in the final optimized bundle. There's nothing that forbids you from including the file you point to with mainConfigFile
in the final bundle.