-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Can't get assets by entry point chunk name in v4 #1369
Comments
unfortunately you are right this is the entire information available inside the template: Lines 696 to 710 in 2ab27f0
what we could do is to extend the template variable generator: Lines 1015 to 1045 in 2ab27f0
To provide for example a key value map like htmlWebpackPlugin.entryFileMapping = {
'path/to/my/js/file.js': 'main'
} Or we could iterate over those entries and attach it to the object directly here: Lines 387 to 390 in 2ab27f0
|
The original feature seems to be quite popular (https://github.com/search?q=htmlWebpackPlugin.files.chunks&type=Code), would it be possible to maintain backwards compatibility to make the upgrade process as easy as possible to all? Happy to discuss alternatives if not :) |
The goal was to make it easier for plugin writers to modify which tags are injected into the html The idea behind that is that there would be a single source of truth - If the information is hold at two places the plugin writers will always need to keep it in sync. I would prefer to not add An api might look like for example like this (would output all <script> / tags for the <%= htmlWebpackPlugin.utils.getChunkTags('common', htmlWebpackPlugin.headTags) %> Also other utils might be possible e.g. (would inline js or css directly in the markup for all headTags): <%= htmlWebpackPlugin.utils.inlineTags(htmlWebpackPlugin.headTags) %> And they could be combined e.g.: <%= htmlWebpackPlugin.utils.inlineTags(htmlWebpackPlugin.utils.getChunkTags('common', htmlWebpackPlugin.headTags)) %> |
The README references the custom html-webpack-template that no longer works with v4:
That template reliant on the |
Reposting in right ticket. So... Doing <!--
// DEBUG
<%= JSON.stringify(htmlWebpackPlugin.files, null, 1) %>
--> in template will reveal structure such as this in 3.2.0
however in 4.x it generates
thus in 4.0 it is not as shown in docs https://github.com/jantimon/html-webpack-plugin#writing-your-own-templates which shows
Thus template that worked with 3.2.0 <% for (let key in htmlWebpackPlugin.files.chunks) { %>
<script src="<%= htmlWebpackPlugin.files.chunks[key].entry %>"></script>
<% } %> does not work any more with 4.0. Just as the OP states. So what is the supposed way to get chunks in template in v4? |
@kroko I am very sorry but right now I have some solutions in mind but it will take me a moment to implement it (as I am doing that in my spare time). Thanks for bringing up the issue with the outdated documentation 👍 |
Sure, I know that the changes are for good, as you have explained it in the comment. Thanks for the great work! 🙇 Just to clarify - what does "not supported" mean given that |
Sorry for the confusion you are right only The new structure is: html-webpack-plugin/typings.d.ts Lines 149 to 171 in c5a5882
The easiest way to output all head tags is:
|
Is this going to change again? |
I will not remove or change the existing structure but probably extend it in some way to allow accessing the chunkname and source of the files to allow inlining again |
Version 4.x dropped support for accessing the "chunks" field in templates. See the discussion in: jantimon/html-webpack-plugin#1369 (comment) This fixes loading web plugins in a Graylog production build. Also sync "yarn.lock".
Version 4.x dropped support for accessing the "chunks" field in templates. See the discussion in: jantimon/html-webpack-plugin#1369 (comment) This fixes loading web plugins in a Graylog production build. Also sync "yarn.lock".
I don't know if I am following these commits properly, but it looks like they're not going the way the discussion here was going, and more like the opposite! @jantimon the |
Any updates about this issue? |
@jantimon This seems to still be an issue, do you have any plan to provide some way how to get common chunk names? |
Bumping for viz |
I just upgraded from v3 to v5 and ran into this issue. This was part of a larger effort to upgrade to Webpack 5. The removal of the I ended up writing a backwards compatibility plugin however this is far from complete. class HtmlWebpackBackwardsCompatibilityPlugin {
apply(compiler) {
compiler
.hooks
.compilation
.tap("HtmlWebpackBackwardsCompatibilityPlugin", compilation => {
HtmlWebpackPlugin
.getHooks(compilation)
.beforeAssetTagGeneration
.tapAsync("HtmlWebpackBackwardsCompatibilityPlugin", (data, callback) => {
const { publicPath } = data.assets;
data.assets.chunks = {};
for (const entryPoint of compilation.entrypoints.values()) {
for (const chunk of entryPoint.chunks) {
data.assets.chunks[chunk.name] = {
entry: chunk.files
.map(file => publicPath + file)
.find(file => file.endsWith(".js"))
};
}
}
callback(null, data);
}
);
});
}
} There is a lot of logic that would need to be copied from Line 629 in d5ce5a8
In our index.ejs file we need to be able to look up, and filter, a chunk entry by name. Not all of our chunks are simply loaded as script tags. Given the barrier to upgrade by either rearchitecting our chunk loading strategy or writing a custom Webpack plugin to duplicate the logic in the HTMLWebpack plugin I think it makes sense for this plugin to add back the |
Congrats on the new major version guys.
Expected behaviour
In v3, it was possible to obtain chunk assets by accessing
htmlWebpackPlugin.files.chunks[entryName].entry
from the template.Current behaviour
According to https://github.com/jantimon/html-webpack-plugin/blob/master/CHANGELOG.md#breaking-changes there's a breaking change affecting such feature:
According to that, one would expect
js
andcss
properties inhtmlWebpackPlugin.files
to contain the mentioned object but I still get the same array of strings as in v3:Environment
Tell us which operating system you are using, as well as which versions of Node.js, npm, webpack, and html-webpack-plugin. Run the following to get it quickly:
Config
Copy the minimal
webpack.config.js
to produce this issue:https://github.com/frosas/lag/blob/f82961175485b1540cefc7f48bc251dad83df10b/webpack.config.js#L55-L71
Copy your template file if it is part of this issue:
https://github.com/frosas/lag/blob/f82961175485b1540cefc7f48bc251dad83df10b/html/index.ejs
Additional context
I couldn't find any reference to
entryName
in the plugin code that suggested the new behavior was actually implemented.Also,
htmlWebpackPlugin.files
section in https://github.com/jantimon/html-webpack-plugin/blob/master/README.md seems outdated now.The text was updated successfully, but these errors were encountered: