Html-webpack-plugin: Filter chunk per entry

Created on 30 Jan 2019  路  5Comments  路  Source: jantimon/html-webpack-plugin

Is your feature request related to a problem? Please describe.
I am experiencing the same problem as some before me, e.g. #125 or or #218. I have multiple entry points and want to produce multiple HTML file. Each one of them corresponds to one entry point.

I know I can filter the chunk I want to include using the chunks argument and so far it is working. But I am also using chunk splitting optimization (because a lot of code is shared between the pages). It becomes quickly complicated to follow what entry depends on what chunk..

I have an optimization setting that looks like this:

optimization: {
  runtimeChunk: "single",
  splitChunks: {
    chunks: "all",
  },
}

This is how one my HtmlWebpackPlugin config currently looks like:

new HtmlWebpackPlugin({
  title: "Foo",
  template: resolve("src/index.pug"),
  chunks: [
    "runtime",
    "main",
    "vendors~main",
    "vendors~page1~main",
    "vendors~page2~main",
    "vendors~page1~page2~main",
  ],
})

It obviously quickly gets out of control..

Describe the solution you'd like

I wish we can have a new option, for example entry where we could specify what is the entry(ies) whose chunks must be injected.

new HtmlWebpackPlugin({
  title: "Foo",
  template: resolve("src/index.pug"),
  entries: ["main"]
})

Most helpful comment

Works correctly in the 4.0.0-beta.5 version. There is the explanation. Thanks for your work.

All 5 comments

I need same feature too

According to https://stackoverflow.com/a/51463575/1441476 this feature added to next release of HtmlWebpackPlugin

Right now the closest you can get is the manual sort mode:

https://github.com/jantimon/html-webpack-plugin/blob/267e0e0eac155569c822c34f120490bdf3f56d43/lib/chunksorter.js#L18-L35

However I believe this should be improved.
Would you be able to provide a pull request?

@jantimon I wish I would. But I am in academia so there is not way I can get founding for this. And mostly I am just already very busy..
But isn't it already implemented? After @behzadian, I did some more digging and realized that it works when using the entry as chunk name in the current @next version.

new HtmlWebpackPlugin({
  title: "Foo",
  filename: "index.html",
  template: resolve("src/main.pug"),
  chunks: ["main"],
}),

In my opinion it is a little bit strange to see the two options merged, but it fits my needs.

I am not sure how it would work with the manual sort option. I suppose the idea is to filter out every chunks with a different entryPointName? But what about chunks that are part of two entry points? I did not check how this API works in details.

Works correctly in the 4.0.0-beta.5 version. There is the explanation. Thanks for your work.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lonelyclick picture lonelyclick  路  3Comments

ruoyiqing picture ruoyiqing  路  3Comments

yyx990803 picture yyx990803  路  4Comments

amorphius picture amorphius  路  3Comments

MatthewKosloski picture MatthewKosloski  路  3Comments