RequireJS R.js not seeing dependent require files if assigned to a variable in a nested require

StackOverflow https://stackoverflow.com/questions/14816854

  •  09-03-2022
  •  | 
  •  

Frage

I'm having a problem with r.js finding file dependencies on nested require calls.

If I pass require an array of strings, the compressor works fine and all the file dependancies are found.

define([
    'jquery', 
    'underscore', 
    'backbone'
], function() {
    require(['views/MobileNavView']);   // Works fine!
});

If I pass require an array of strings I've assigned to a variable, the compressor doesn't find the file dependencies.

var requiredFiles = [
    'views/MobileNavView'
];

define([
    'jquery', 
    'underscore', 
    'backbone'
], function() {
    require(requiredFiles); // Doesn't Work!
});

What could be causing the compressor to not find the file dependencies if I assign the array of strings to a variable?

Here is my app.build.js

({
    baseUrl: '.',
    findNestedDependencies: true,
    mainConfigFile: 'Main.js',
    name: 'Main',
    out: 'Core.js',
    optimize: 'none'
})
War es hilfreich?

Lösung

That's actually mentioned and explained on the r.js docs page

(...) So, it will not find modules that are loaded via a variable name:

 var mods = someCondition ? ['a', 'b'] : ['c', 'd'];
 require(mods);

That's because r.js scans the scripts as text, it doesn't actually evaluate them. Take a look at its source code, you'll see there's a lot of regular expression matching going on.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top