I use RequireJS and I have the following build profile for r.js. My main file is app.js which imports jquery, ember, handlebars, and underscore. I have another file specific to a page of my application called change_email.js. It imports ember as well.
({
modules: [{
name: 'js/app',
// jQuery is included in the page
exclude: ['jquery'],
include: ['ember', 'handlebars', 'underscore']
}, {
name: 'js/change_email',
exclude: ['jquery', 'js/app']
}],
mainConfigFile: 'requirejs_config.js'
})
My RequireJS config looks like:
requirejs.config({
paths: {
'jquery': 'js/lib/jquery',
'ember': 'js/lib/ember-1.0.0-rc.6.1',
'handlebars': 'js/lib/handlebars-1.0.0-rc.4',
'underscore': 'js/lib/underscore'
},
shim: {
'underscore': {
exports: '_'
},
'ember': {
deps: ['jquery', 'handlebars'],
exports: 'Ember'
},
'handlebars': {
exports: 'Handlebars'
}
}
});
I noticed that sometimes when I load the page, Ember is run from app.js and sometimes it is run from ember-1.0.0-rc.6.1.js
. I assume that this is a race condition. In the case when Ember runs from ember-1.0.0-rc.6.1.js
, 2 files containing Ember have been downloaded app.js
and ember-1.0.0-rc.6.1.js
. This seems counter intuitive because one of the goals of using RequireJS is to cut down on the amount of code that is downloaded.
I thought about making app.js import and then export ember and make other modules import Ember from app.js. I don't really like this idea because it requires me to change my imports to an unnatural style.
What's the most efficient way to remove the duplication?