简体   繁体   English

带有 webpack DevServer 的多个 HtmlWebpackPlugins

[英]Multiple HtmlWebpackPlugins with webpack DevServer

You can use multiple HtmlWebpackPlugin plugins to create more than one HTML file in production, but only one of the HTML files will be used by the DevServer.您可以使用多个HtmlWebpackPlugin插件在生产中创建多个 HTML 文件,但 DevServer 只会使用 HTML 文件之一。 Is there any way to use all the HtmlWebpackPlugin plugins in development as well?有没有办法在开发中使用所有的HtmlWebpackPlugin插件?

module.exports = {
  entry: {
    main: './src/main.js',
    anotherEntry: './src/anotherEntry.js'
  },
  // This only serves the index.html file on 404 responses
  devServer: {
    contentBase: './dist',
    historyApiFallback: true,
    port: 3000,
  },
  // ...
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: './src/main.html',
      chunks: ['main'],
    }),
    new HtmlWebpackPlugin({
      filename: 'anotherEntry.html',
      template: './src/anotherEntry.html',
      chunks: ['anotherEntry'],
    }),
  ]
};

historyApiFallback can be given manual rewrites to control in a more fine-grained manner what the DevServer should fallback to on 404 responses.可以对historyApiFallback进行手动rewrites ,以便以更细粒度的方式控制 DevServer 在404响应时应该回退到什么。 This way we can serve the other files in development as well.这样我们也可以为开发中的其他文件提供服务。

module.exports = {
  entry: {
    main: './src/main.js',
    anotherEntry: './src/anotherEntry.js'
  },
  devServer: {
    contentBase: './dist',
    historyApiFallback: {
      rewrites: [
        { from: /^\/anotherEntry/, to: '/anotherEntry.html' },
        { to: '/index.html' },
      ],
    },
    port: 3000,
  },
  // ...
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: './src/main.html',
      chunks: ['main'],
    }),
    new HtmlWebpackPlugin({
      filename: 'anotherEntry.html',
      template: './src/anotherEntry.html',
      chunks: ['anotherEntry'],
    }),
  ]
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM