Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include Modernizr in RequireJS and have the Modernizr in the head tags

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>
like image 640
einstein Avatar asked Nov 19 '12 10:11

einstein


People also ask

Where do I put modernizr?

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.

How add RequireJS to HTML?

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.

Is RequireJS asynchronous?

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.


1 Answers

You should be able to get both.

  • First, remove your paths configuration that relates to modernizr, you won't need it
  • Next Load Modernizr in the head - this will create a window.Modernizr variable.
  • Finally, before your bootstrapping require, define the modernizr module

    define('modernizr', [], Modernizr);
    require(['foo', 'bar', 'modernizr'], function(foo, bar, modernizr) {
         //..profit
    }
    
like image 151
George Mauer Avatar answered Oct 16 '22 07:10

George Mauer