kle*_*wis 4 javascript html-webpack-plugin webpack-4
我正在使用带有 HTMLWebPackPlugin 的 Webpack 4。我目前正在处理接近 30 个不同的页面,而且还有更多的内容。这是我在代码中的示例...
new HTMLWebpackPlugin({
template: './src/game11/index.html',
mViewPort: 'width=device-width, initial-scale=1.0',
favicon: './src/game11/media/favicon-16x16.png',
title: 'Game 11 Training',
filename: 'game11.htm',
chunks: ['game11']
}),
new HTMLWebpackPlugin({
template: './src/game12/index.html',
mViewPort: 'width=device-width, initial-scale=1.0',
favicon: './src/game12/media/favicon-16x16.png',
title: 'Game 12 Training',
filename: 'game12.htm',
chunks: ['game12']
}),
Run Code Online (Sandbox Code Playgroud)
到目前为止,我的 webpack.config.js 文件中有 30 个。但我更愿意做这样的事情而不是......
['game11','game12','something-else','etc'].forEach((event) => {
new HtmlWebpackPlugin({
template: './src/${event}/index.html',
mViewPort: 'width=device-width, initial-scale=1.0',
favicon: './src/${event}/media/favicon-16x16.png',
title: '${event} Training',
filename: '${event}.htm',
chunks: ['${event}']
}),
}),
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用,它只是一个草图。但是今天是否有可能在不添加额外插件或修改我的输出的情况下做这样的事情?我只是想添加数组值,这将在其本身创建一个新实例。
非常感谢!
按照您在问题中建议的相同逻辑,您可以使用map而不是forEach像这样构建插件数组:
webpack.config.js
{
plugins: [
new MiniCSSExtractPlugin(),
...['game11', 'game12', 'something-else', 'etc'].map((event) => {
return new HtmlWebpackPlugin({
template: `./src/${event}/index.html`,
mViewPort: `width=device-width, initial-scale=1.0`,
favicon: `./src/${event}/media/favicon-16x16.png`,
title: `${event} Training`,
filename: `${event}.htm`,
chunks: [`${event}`]
})
})
]
}
Run Code Online (Sandbox Code Playgroud)