使用 Grunt 动态生成 RequireJS "bundles" 配置
Generate RequireJS "bundles" config dynamically with Grunt
根据 RequireJS API,bundles
配置 属性 用于将模块映射到它们的包文件(在生产模式下,假设您有多个大型项目上的捆绑包)。
我使用 Grunt 与 r.js
进行捆绑,当然,如何根据实际捆绑动态生成 bundle
配置?例如如果模块 angular
被捆绑到 common.js
,那么它应该被添加到 common
捆绑包的模块列表中。
这部分requirejs配置不应该手动维护,因为它会导致很多错误。
有什么建议吗?
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
.
生成一个合适的值
根据 RequireJS API,
bundles
配置 属性 用于将模块映射到它们的包文件(在生产模式下,假设您有多个大型项目上的捆绑包)。我使用 Grunt 与
r.js
进行捆绑,当然,如何根据实际捆绑动态生成bundle
配置?例如如果模块angular
被捆绑到common.js
,那么它应该被添加到common
捆绑包的模块列表中。
这部分requirejs配置不应该手动维护,因为它会导致很多错误。
有什么建议吗?
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
.