0

When we are running our site in debug mode without minifying the script this works:

var defines = [
    'globals',
    'templates'
];

define(defines, function(globals, templates) {
    //code...
});

But as soon the code is minified it stops to work. Do you know why?

Please notice that this works:

define([
    'globals',
    'templates'
], function(globals, templates) {
    //code...
});

The reason why we want to use an array is because we want to loop through it to replace 'template' with 'template.1010101010' where 10101010101 is a timestamp based on the creation of the file, maintaining the file like this enables us to automatically bypass the cache when the file is updated.

Please note that we have more files to loop through.

Louis
  • 128,628
  • 25
  • 249
  • 295
PaRoxUs
  • 99
  • 1
  • 10

1 Answers1

2

r.js cannot handle dependencies that are specified as something else than a literal array. This is why this works:

define(['globals', 'templates'], function(globals, templates)...

But this does not work:

var deps = ['globals', 'templates'];
define(deps, function(globals, templates)...

To make it work r.js would have to perform code analysis, which would make it much more complex and slower.

For cache busting purposes, one way to get what you were trying to achieve is to generate a runtime configuration where the paths setting for templates is set dynamically in a built step:

paths: {
    templates: 'templates.1.3.2'
}
Louis
  • 128,628
  • 25
  • 249
  • 295
  • It is also possible to do it like this: var templates = 'template.123'; define(['globals', templates], function(globals, templates)... Although you gave me the answer I needed this does not solve our problem where we sometimes wants to load an extra file from main, of course we could load it in an extra require request but I would love to be able to load it faster – PaRoxUs Dec 18 '14 at 13:06