Frage

I'm using the RequireJS optimiser to minify and concatenate my code. At the moment r.js doesn't minify a lot of my scripts because of the nested dependencies.

I want to include nested dependencies for:

  • All dependencies inside: 'js/services'
  • All nested dependencies for a list of specific JavaScript files

Note: I do realise that there is the option findNestedDependencies: true but this looks for absolutely every dependency across all JavaScript files in the app where as I only want to do this for certain JavaScript files and folders as I have a set of files which are always used on every page/view.

My current build file looks like:

  ({
    baseUrl: '../static/js',
    mainConfigFile: '../static/js/main.js',
    name: 'main',
    out: '../static/js/scripts.min.js',
    paths: {
        requireLib: 'vendor/require/require.min'
    },
    include: 'requireLib'
})

I have been following this tutorial to get the optimiser running: http://www.youtube.com/watch?v=m6VNhqKDM4E

War es hilfreich?

Lösung

You can use include to specify all dependencies you want to force into the output file:

// ...
include: ['requireLib', 'js/services/dep1', 'js/services/dep2'],
// ...

I don't think there's a way to include entire folder (something like "js/services/*"), though.


Since in my project I had many dynamic dependencies I wanted to include in the output I ended up creating an "js/services/_all.js" module which lists files in its directory, for example:

define([
    './dep1',
    './dep2'
  ],
  function () {
    // this module imports all modules from current folder so 
    // there's no need to list every single file in the build config
  });

and then configuring r.js with:

// ...
include: ['requireLib', 'js/services/_all'],
// ...
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top