我想建三捆:main.js
,vendor.js
和polyfill.js
.polyfill.js
文件应包含polyfill.js
文件中定义的模块.vendor.js
应该通过提取从npm(node_modules
)导入的所有依赖项来动态创建文件.最后main.js
应该包含而不是polyfill
和vendor
模块,这本质上是我的应用程序代码.
polyfill.js
import 'core-js/es7/reflect';
import 'zone.js/dist/zone';
Run Code Online (Sandbox Code Playgroud)
main.js
import { NgModule, ApplicationRef } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { Store } from '@ngrx/store';
@NgModule([
imports: [...]
])
class AppModule {
}
Run Code Online (Sandbox Code Playgroud)
我写了下面的webpack配置,但总是得到以下错误:
"CommonsChunkPlugin: While running in normal mode it's not allowed to use a non-entry chunk (main)",
"CommonsChunkPlugin: While running in normal mode it's not allowed to use a non-entry chunk (vendor)"
Run Code Online (Sandbox Code Playgroud)
webpack.config.prod.js
{
entry: {
polyfill: './polyfill.ts',
main: './main.ts'
},
output: {
publicPath: options.assetPath
},
devtool: 'source-map',
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: function (module) {
const check = module.context && module.context.indexOf('node_modules') !== -1;
module.originalChunkNames = module.chunks.map(chunk=> chunk.name);
return check;
}
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'polyfill',
chunks: ['vendor'],
minChunks: ({ originalChunkNames }) => originalChunkNames.includes('polyfill'),
}),
new webpack.optimize.CommonsChunkPlugin({
names: ['main', 'vendor', 'polyfill'],
minChunks: Infinity
})
]
}
Run Code Online (Sandbox Code Playgroud)
这里我想做的是动态创建vendor
包,包括node_modules
我在源文件中导入的所有内容.polyfill
通过包含polyfill.js
文件中明确定义的所有模块来创建包.最后main
一捆.
我已经尝试了很多来自webpack github repo的例子,但是没有一个能帮助我实现上面提到的东西
我创建了一个Github 存储库来演示工作示例。
以下是实现此类输出的 webpack 配置的重要部分:
{
entry: {
polyfill: 'polyfill.ts',
main: 'main.ts'
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: "vendor",
chunks: ["main"],
minChunks: function(module) {
const check =
module.context && module.context.indexOf("node_modules") !== -1;
module.originalChunkNames = module.chunks.map(chunk => chunk.name);
return check;
}
}),
new webpack.optimize.CommonsChunkPlugin({
name: "polyfill",
chunks: ["vendor"],
minChunks: function({ originalChunkNames }) {
return originalChunkNames.includes("polyfill");
}
}),
]
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
638 次 |
最近记录: |