Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 205
dVaffection Avatar asked Aug 25 '13 06:08

dVaffection


2 Answers

Way late to the party on this, but here the solution I used: append the urlArgs param to the config with a subsequent call.

HTML:

<script src="js/libs/require.js"></script>
<script src="js/config.js"></script>
<script>require(['main-app']);</script>

Config File:

requirejs.config({
  paths: {...},
  shim: {...}
}); 

// Apply the cache bust only for the browser. 
if (window) {
  requirejs.config({
    urlArgs: REQUIRE_NOCACHE ? "bust="+(new Date()).getTime() : null
  });
}

The optimizer only takes the first requirejs.config declaration and it ignores the subsequent code. The second requirejs.config declaration extends rather than overrides the first, so urlArgs is still successfully applied to modules in the browser. Hope that helps.

like image 198
Jes Avatar answered Oct 30 '22 04:10

Jes


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?

like image 32
Julian V. Modesto Avatar answered Oct 30 '22 04:10

Julian V. Modesto