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

Most upvoted comments

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 sourceFile is an array, then should be named in plural as sourceFiles:

filter({ sourceFiles: Array<string>, outputFile: string }) => void | boolean

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:

const [firsModule] = chunkGroup._modulePreOrderIndices.entries().next().value;
const sourceFile = firsModule.resource;

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 me

Ok, I will have more time to explain how to reproduce the errors

In the v4.14.0 is 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.0

Unfortunately 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 using plugin.preload[].test option independent of webpackPreload comment.

For example:

import(
  /* webpackChunkName: "module1" */
  /* webpackPreload: false */ // <= the magic comment does not affect anything and can be omitted
  './module-a'
).then(
  () =>
    import(
      /* webpackChunkName: "module2" */
      /* webpackPreload: true */ // <= the magic comment does not affect anything and can be omitted
      './module-b'
    )
);

think preload script options don’t work for dynamic js import

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 be bootstrap-module.js

Compiled HTML fragment now:

    <meta name="robots" content="noindex">
    <link rel="preload" href="static/js/app.db1e2bc1a.js" as="script">
    <link rel="stylesheet" href="static/css/styles.95b733d30.css">

I expect

    <meta name="robots" content="noindex">
    <link rel="preload" href="static/js/app.db1e2bc1a.js" as="script">
    <link rel="preload" href="static/js/bootstrap-module.js" as="script">
    <link rel="stylesheet" href="static/css/styles.95b733d30.css">