Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用webpack生成多个html文件?_Javascript_Webpack - Fatal编程技术网

Javascript 如何使用webpack生成多个html文件?

Javascript 如何使用webpack生成多个html文件?,javascript,webpack,Javascript,Webpack,我需要在构建后生成2个html文件,并且只在第一个html包中包含.js包(无头、体等),但在另一个.css包中包含(无头、体等)。我如何理解为了允许htmlWebpackPlugin生成2个htmls,我需要在webpackconfig中指定如下内容: new HtmlWebpackPlugin({ template: './templates/index1.html', filename: '/templates/template1.html'

我需要在构建后生成2个html文件,并且只在第一个html包中包含
.js
包(无头、体等),但在另一个
.css
包中包含(无头、体等)。我如何理解为了允许htmlWebpackPlugin生成2个htmls,我需要在
webpack
config中指定如下内容:

new HtmlWebpackPlugin({
            template: './templates/index1.html',
            filename: '/templates/template1.html',

        }),
        new HtmlWebpackPlugin({
            template: './templates/index2.html',
            filename: '/templates/template2.html',

        }), 

但是如何在它们中分离捆绑包呢?

您可以通过使用多个入口点来实现

module.exports = {
  entry: {
    'page1': './apps/page1/scripts/main.js',
    'page2': './apps/page2/src/main.js'
  },
  output: {
    path: __dirname,
    filename: "apps/[name]/build/bundle.js"
  },
  plugins: [
    new HtmlWebpackPlugin({
      chunks: ['page1'],
      filename: 'apps/page1/build/index.html'
    }),
    new HtmlWebpackPlugin({
      chunks: ['page2'],
      filename: 'apps/page2/build/index.html'
    })
  ]
};

您可以通过使用多个入口点来实现它

module.exports = {
  entry: {
    'page1': './apps/page1/scripts/main.js',
    'page2': './apps/page2/src/main.js'
  },
  output: {
    path: __dirname,
    filename: "apps/[name]/build/bundle.js"
  },
  plugins: [
    new HtmlWebpackPlugin({
      chunks: ['page1'],
      filename: 'apps/page1/build/index.html'
    }),
    new HtmlWebpackPlugin({
      chunks: ['page2'],
      filename: 'apps/page2/build/index.html'
    })
  ]
};

使用inject:false时,它不会将任何块放入html。有了js捆绑包,它就可以工作了。css呢?(我用js捆绑包中的文本eextract插件生成了它)通过删除inject编辑了答案。是的,您可以使用textextractpluginwith使用inject:false插入css文件,它不会将任何块放入html。有了js捆绑包,它就可以工作了。css呢?(我用js捆绑包中的文本eextract插件生成了它)通过删除inject编辑了答案。是的,您可以使用textextractplugin插入css文件