javascriptrequirejsr.js

How to exclude urlArgs from build using r.js


I use r.js optimizer to combine js files based on build profile as it's suggested in documentation. Here's my build-config.js:

({
    baseUrl: ".",
    paths: {
        jquery: '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min',
    },
    name: "main",
    out: "main-built.2013-07-30.js"
})

As you can see it's based upon main.js file, here's a code of it:

requirejs.config({
    baseUrl: 'scripts',
    urlArgs: "bust=" + (new Date()).getTime(),
    paths: {
        jquery: [
            '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min',
            'lib/jquery-1.9.1.min',
        ],
    },
});

require([
    'layout',
    'cue',
], function() {

});

If I preserve urlArgs: "bust=" + (new Date()).getTime() in main.js all external files (here jquery which is loaded from CDN) look like .../jquery.js?bust=1377412213

So it's PITA to comment out this line every time I make a build. I've read through all the documentation and googled for a solution but everything in vain. Maybe I do it wrong?


Solution

  • The following solution would work in your case, where you're renaming the main.js file in the r.js build:

    urlArgs: require.specified('main') ? "bust="+(new Date()).getTime() : null
    

    The above snippet will check for the module named 'main', which will match in development, but not in production, where the module is named 'main-built.2013-07-30'.

    I've tested in development and production builds and it works! :-)

    On the require.specified() function: With requirejs is it possible to check if a module is defined without attempting to load it?