分配单独的入口点脚本以分隔HtmlWebpackPlugin实例

时间:2018-05-03 20:02:46

标签: javascript node.js webpack html-webpack-plugin

给出类似

的输入/输出
entry: {
    app: 'src/client/app.js',
    unauthApp.js: 'src/client/unauth.js'
  },
output: {
  path: 'dist',
  filename: 'scripts/[name]-[chunkhash].js'
},
...
plugins: [
  new HtmlWebpackPlugin({
    filename: 'views/index.html'
  }),
  new HtmlWebpackPlugin({
    filename: 'views/index-inauth.html'
  })
]

是否可以使用HtmlWebpackPlugin的两个实例将app脚本放入一个模板,将unauthApp脚本放入另一个模板中。到目前为止,我所能做的就是将两个脚本放在两者中。我也在玩htmlWebpackTemplate,所以也许有一个我没见过的选项。

webpack@^4.6.0

1 个答案:

答案 0 :(得分:2)

您是否尝试过chunks字段?它Allows you to add only some chunks (e.g only the unit-test chunk)

plugins: [
  new HtmlWebpackPlugin({
    filename: 'views/index.html',
    chunks: ['app'],
    inject: true
  }),
  new HtmlWebpackPlugin({
    filename: 'views/index-unauth.html',
    chunks: ['unauthApp.js'],
    inject: true
  })
]