Search code examples
javascriptrequirejsmodernizramdjs-amd

What's the Best Way To Namespace AMD Modules?


Modernizr was written using AMD definitions, which is nice since that's what I'm using for my current project and can pull in Modernizr dependencies when I need them. Problem is, the Modernizr modules all expect a flat folder heirarchy, but I've moved the files into my project like so: lib > modernizr

What's the best way to change the paths on the dependencies in the Modernizr definitions? Is this something I can handle with RequireJS? I could obviously do it by hand, but I want to be able to easily pull in Modernizr updates in the future and not have to manually update the paths every time.


Solution

  • I don't think this would be a good idea. From the docs:

    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.

    Anyway, you can specify the full paths in a requirejs.config:

    // in your main
    require.config({
        paths: {
            "modernizr": "lib/modernizr"
        }
    });
    
    //in your module
    define(["modernizr"], function (modernizr) {
    });