I have a problem with RequireJS and Modernizr.
I want the Modernizr resource to be on the head. And everything else in the body. The reason is that Modernizr need to do some stuffs before DOMContentLoad. I want to be able to load the Modernizr module into other modules using RequireJS. How can I accomplish that both in dev and production environment? I use requirejs that pulls all dependencies and minifies all the resources. And the yeoman build replaces <script data-main="scripts/main" src="scripts/vendor/require.js"></script>
with the minified version.
under the body tag:
<!-- build:js ikl.app.js -->
<script data-main="scripts/main" src="scripts/vendor/require.js"></script>
<!-- endbuild -->
<script>
requirejs.config({
paths: {
'jquery' : 'vendor/jquery',
'handlebars' : 'vendor/handlebars',
'ember' : 'vendor/ember',
'ember-data' : 'vendor/ember-data',
'modernizr' : 'vendor/modernizr' // This should be removed
},
baseUrl: 'scripts',
shim: {
'jquery' : {
exports : 'jQuery'
},
'ember': {
deps: ['jquery', 'handlebars'],
exports: 'Ember'
},
'ember-data': {
deps: ['ember'],
exports: 'DS'
},
'handlebars': {
exports: 'Handlebars'
},
'modernizr': {
exports: 'Modernizr'
}
}
});
require([
'modules/system/controllers/system.controller',
'jquery',
'ember',
'ember-data',
'handlebars',
'modernizr'
],
function( systemController ) {
systemController.renderView();
}
);
</script>
The reason we recommend placing Modernizr in the head is two-fold: the HTML5 Shiv (that enables HTML5 elements in IE) must execute before the <body> , and if you're using any of the CSS classes that Modernizr adds, you'll want to prevent a FOUC.
To include the Require. js file, you need to add the script tag in the html file. Within the script tag, add the data-main attribute to load the module. This can be taken as the main entry point to your application.
RequireJS, like LABjs, allows for asynchronous JavaScript loading and dependency management; but, RequireJS uses a much more modular approach to dependency definitions. This is just an initial exploration of RequireJS. RequireJS seems to be quite robust and includes optimization and "build" tools for deployment.
You should be able to get both.
Finally, before your bootstrapping require, define the modernizr module
define('modernizr', [], Modernizr);
require(['foo', 'bar', 'modernizr'], function(foo, bar, modernizr) {
//..profit
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With