Optimization fails when passing a variable with a list of dependencies to define()

51 views Asked by At

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.

1

There are 1 answers

1
Louis On BEST ANSWER

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'
}