如何选择某个HTML页面将使用的包?

问题描述

项目

我有一个项目,其中使用Pug编写网站视图,然后编译为HTML。

index.pug ---> index.html

因为我希望项目具有多个页面,所以我也有这个页面

about.pug ---> about.html

现在,每个页面webpack.config.js中都有条目:

{
  entry: {
    index: ['scripts/index.ts'],about: ['scripts/about.ts'],}
}

我认为两者都没有一个捆绑包是不好的,因为每个页面条目都具有唯一的导入模块(也就是说:如果我知道我在说什么)。

问题

生成HTML时,html-webpack-plugin自动两个包注入两个页面,这比我刚才解释的要差。

如何告诉它只注入我想要的包?

ps:我尝试将它们每个都手动包含在*.pug文件中。不过,为了实现缓存清除,生成的包文件名具有哈希值。

解决方法

在写这个问题之前,我花了三个小时寻找解决方案。

尽管,提交后,我刷新了搜索页面并找到了解决方案。

关于块。

您将选项的哈希传递给html-webpack-plugin,其中有一个名为“块”的属性。此属性可以采用字符串数组,并带有所需条目的名称。


{
  plugins: [

    new HtmlWebpackPlugin({
      template: 'views/index.pug',scriptLoading: 'defer',filename: 'index.html',chunks: [
        "index"
      ]
    }),new HtmlWebpackPlugin({
      template: 'views/about.pug',filename: 'about.html',chunks: [
        "about"
      ]
    }),new HtmlWebpackPugPlugin()
  ]
}

很抱歉造成麻烦。