生成RequireJS"捆绑"使用Grunt动态配置

时间:2016-10-20 10:14:02

标签: javascript gruntjs requirejs amd

  1. 根据RequireJS API,bundles配置属性用于将模块映射到其捆绑文件(在生产模式下,假设您在大型项目中有多个捆绑包)。

  2. 我当然使用Grunt捆绑r.js,如何根据实际捆绑动态生成bundle配置?例如如果模块angular已绑定到common.js,则应将其添加到common包的模块列表中。

  3. 这部分requirejs配置不应该手动维护,因为它会导致许多错误。

    有什么建议吗?

1 个答案:

答案 0 :(得分:1)

RequireJS 2.2.0版引入了new optimizer option来写出从构建中生成的bundles选项:

//Introduced in 2.2.0. Path to file to write out bundles config
//(http://requirejs.org/docs/api.html#config-bundles) found in the module
//layers built by the optimizer. The path is relative to the "dir" config's
//path. Only applies to full project optimization:
//http://requirejs.org/docs/optimization.html#wholeproject
//Only use if the optimized layers are grouped more intricately then just
//a simple optimization of main app entry points. The file path specified
//should be to one that has the top level requirejs.config() call that sets
//up the loader. If using "mainConfigFile", then this path likely should be
//the path to that file where it is placed in the "dir" output directory.
bundlesConfigOutFile: 'some/path/to/main.js',

如果上述选项对您不起作用,那么您必须执行我所做的操作并滚动您自己的代码以生成bundles的正确值。