【发布时间】:2016-08-19 15:18:18
【问题描述】:
在 Webpack 1 中,我们使用 require.ensure 进行代码拆分导入,它可以采用一组模块。这些模块组合成一个包并通过一个 HTTP 请求获取:
require.ensure(['module1', 'module2'], (require) => {
const module1 = require('module1');
const module2 = require('module2');
// use these modules here...
});
// ==> both modules are served as a single bundle, e.g. '5.js'
使用 Webpack 2,我们现在可以使用 System.import 来获得更简洁的语法……但似乎 System.import 只接受单个模块来导入。很好——我可以使用Promise.all——但我最终得到了两个包:
Promise.all([
System.import('module1'),
System.import('module2')
]).then( (module1, module2) => {
// use these modules here...
});
// ==> each module served as its own bundle, e.g. '5.js', '6.js'
有没有办法使用System.import,但仍将请求的模块组合成一个包?
(是的,在某些情况下,我可以添加一个新的模块文件,然后导入并使用这两个依赖项,这通常是最好的方法,但对于我的几个用例,它只是添加了额外的样板文件)
【问题讨论】:
标签: javascript webpack