2

我正在使用 RequireJS 优化器来缩小和连接我的代码。由于嵌套的依赖关系,目前 r.js 并没有缩小我的很多脚本。

我想包含嵌套依赖项:

  • 里面的所有依赖项:'js/services'
  • 特定 JavaScript 文件列表的所有嵌套依赖项

注意:我确实意识到有这个选项findNestedDependencies: true,但这绝对会查找应用程序中所有 JavaScript 文件的所有依赖项,因为我只想对某些 JavaScript 文件和文件夹执行此操作,因为我有一组始终使用的文件每个页面/视图。

我当前的构建文件如下所示:

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

我一直在关注本教程以使优化器运行: http ://www.youtube.com/watch?v=m6VNhqKDM4E

4

1 回答 1

6

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'],
// ...
于 2013-10-12T20:20:41.010 回答