I am trying to use RequireJS to modularize our JS into a few larger JS files so that dependencies can be loaded on-demand but without the heft of a large single-JS download when loading the page.
As an example, with the JS files as:
- js/main.js
- js/views/category1/js1.js
- js/views/category1/js2.js
- js/views/category2/js1.js
- js/views/category2/js2.js
Some parts of the application (e.g. everything in category1) are only used by certain types of users, and similarly with category2, so it doesn't make sense loading it all for every user.
I'm trying a build config for r.js to create two dynamically loaded modules (category1.js & category2.js) which contain all the code from their respective js1.js and js2.js files.
({
appDir: './',
baseUrl: './js',
dir: './dist',
modules: [
{
name: 'main',
exclude: [
"category1",
"category2"
]
},
{
name: "category1",
include: [
"views/category1/js1",
"views/category1/js2"
],
create: true
},
{
name: "category2",
include: [
"views/category2/js1",
"views/category2/js2"
],
create: true
}
],
fileExclusionRegExp: /^(r|build)\.js$/,
writeBuildTxt: false,
optimizeCss: 'standard',
removeCombined: true,
paths: {
jquery: 'lib/jquery',
underscore: 'lib/underscore',
backbone: 'lib/backbone/backbone'
},
shim: {
underscore: {
exports: '_'
},
backbone: {
deps: [
'underscore',
'jquery'
],
exports: 'Backbone'
}
}
})
However, when loading the compiled output, the browser is complaining that it can't find views/category1/js1, views/category1/js2 and so on.
I'm not even sure it's possible to create a few high-level modules from multiple smaller JS files in RequireJS. Has anyone had any experience with this?
EDIT
main.js:
require.config({
shim: {
underscore: {
exports: '_'
},
backbone: {
deps: [
'underscore',
'jquery'
],
exports: 'Backbone'
}
},
paths: {
jquery: 'lib/jquery',
underscore: 'lib/underscore',
backbone: 'lib/backbone/backbone'
}
});
require([
'views/app',
'router'
], function(AppView, Router) {
new Router();
Backbone.history.start();
});
index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<script data-main="js/main" src="js/lib/require/require.js"></script>
</head>
<body></body>
</html>
The JS1, JS2, etc files are being referenced from router.js.