Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter chunk per entry #1152

Closed
QuentinRoy opened this issue Jan 30, 2019 · 5 comments
Closed

Filter chunk per entry #1152

QuentinRoy opened this issue Jan 30, 2019 · 5 comments

Comments

@QuentinRoy
Copy link

@QuentinRoy QuentinRoy commented Jan 30, 2019

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"]
})
@behzadian

This comment has been minimized.

Copy link

@behzadian behzadian commented Jan 31, 2019

I need same feature too

@behzadian

This comment has been minimized.

Copy link

@behzadian behzadian commented Jan 31, 2019

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

@jantimon

This comment has been minimized.

Copy link
Owner

@jantimon jantimon commented Feb 4, 2019

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

/**
* Sort manually by the chunks
* @param {string[]} entryPointNames the chunks to sort
* @param {WebpackCompilation} compilation the webpack compilation
* @param htmlWebpackPluginOptions the plugin options
* @return {string[]} The sorted chunks
*/
module.exports.manual = (entryPointNames, compilation, htmlWebpackPluginOptions) => {
const chunks = htmlWebpackPluginOptions.chunks;
if (!Array.isArray(chunks)) {
return entryPointNames;
}
// Remove none existing entries from
// htmlWebpackPluginOptions.chunks
return chunks.filter((entryPointName) => {
return compilation.entrypoints.has(entryPointName);
});
};

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

@QuentinRoy

This comment has been minimized.

Copy link
Author

@QuentinRoy QuentinRoy commented Feb 4, 2019

@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.

@Neporotovskiy

This comment has been minimized.

Copy link

@Neporotovskiy Neporotovskiy commented Mar 14, 2019

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

@jantimon jantimon closed this Jul 19, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Aug 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants
You can’t perform that action at this time.