html-bundler-webpack-plugin: Webpack module magic comment for preloading script doesn't work
Current behaviour
Webpack has the ability to preload dynamic imported modules with magic comments Webpack cfg fragment:
new HtmlBundlerPlugin({
preload: [
{
test: /\.m?js$/i,
as: "script",
},
],
}),
index.html fragment:
<script src="@src/js/app.js" async></script>
</body>
app.js fragment
import(
/* webpackChunkName: "npm-name1" */
/* webpackPreload: true */
"@npm-name1"
).then(() =>
import(
/* webpackChunkName: "npm-name2" */
/* webpackPreload: true */
"npm-name2"
)
);
Compiled html fragment:
<head>
<link rel="preload" href="app.0646c5f9a.js" as="script">
<link href="sapp.8f622dab1.css" rel="stylesheet">
There’re no preload links for dynamic imports.
Expected behaviour
I would like to use module preloading via magic comments for dynamic imports. If this is not possible, the documentation should disclose this.
Reproduction
- repository URL
- example code
Environment
- OS: [macOS, Linux, Windows]
- version of Node.js: v22.12.0
- version of Webpack: 5.97.1
- version of the Plugin: 4.11.1
Additional context
Appreciation for the useful project
- After the problem is resolved, do not forget to give a star ⭐
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 37 (37 by maintainers)
Commits related to this issue
- feat: add support for preloading of dynamic imported modules, #138 — committed to webdiscus/html-bundler-webpack-plugin by webdiscus 2 years ago
I compiled a project, got the expected result. Didn’t have any problems with the preload filter. Didn’t try
includes/excludes. Thank you!According naming convention, if the
sourceFileis an array, then should be named in plural assourceFiles:Do you agree?
I like it)
I think opportunity is much better than nothing. We can handle it) Thank you!
@vralle
I have bad news. In Webpack is no legal way to get the source file of an asyncChunk.
Webpack is so over-engineered that some simple things become very difficult or impossible to achieve. 😕
For example, we have a asyncChunk (dynamic imported module). I can get its parent module, I can get all dependencies, but there is no way to get the module of the same asyncChunk.
I can only use a hack with the private property:
I can’t guarantee that first module will be always correct.
Therefore I decided to use the outputFile in the filter.
filter: ({ sourceFile, outputFile })looks logical for meOk, I will have more time to explain how to reproduce the errors
In the
v4.14.0is implemented the filter option.The filter’s curious. I’d like to try it.
@vralle
The plugin can now preload dynamic loaded modules too,
v4.13.0Unfortunately we can’t detect whether the magic comment was set or not and there is no way to read a value of the magic comment, because the magic comment does not leave a “footprint” in the Webpack module. Therefore the
/* webpackPreload: true */magic comment can be omitted. The preload works usingplugin.preload[].testoption independent ofwebpackPreloadcomment.For example:
It works for me locally. I’m already testing it…
Hi @webdiscus!
If
webpack.cfg.output.chunkFilename: "[name].js"and script have magic comment/* webpackChunkName: "bootstrap-module" */, chunk name will bebootstrap-module.jsCompiled HTML fragment now:
I expect