html-bundler-webpack-plugin: Unpredictably, after a random number of runs, Webpack compilation fails.

Current behaviour

Unpredictably, after a random number of runs, Webpack compilation fails if the same resource file is loaded into different templates.

error:

[webpack-cli] HookWebpackError: Cannot read properties of undefined (reading 'endsWith')
    at makeWebpackError (/workspace/node_modules/webpack/lib/HookWebpackError.js:48:9)
    at /workspace/node_modules/webpack/lib/Compilation.js:3217:12
    at eval (eval at create (/workspace/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:41:1)
    at fn (/workspace/node_modules/webpack/lib/Compilation.js:499:17)
    at _next1 (eval at create (/workspace/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:39:1)
    at eval (eval at create (/workspace/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:53:1)
    at fn (/workspace/node_modules/webpack/lib/Compilation.js:511:9)
    at _next0 (eval at create (/workspace/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:49:1)
    at eval (eval at create (/workspace/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:64:1)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
-- inner error --
TypeError: Cannot read properties of undefined (reading 'endsWith')
    at Integrity.processAssets (/workspace/node_modules/html-bundler-webpack-plugin/src/Plugin/Extras/Integrity.js:128:62)
    at fn (/workspace/node_modules/webpack/lib/Compilation.js:497:10)
    at _next1 (eval at create (/workspace/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:39:1)
    at eval (eval at create (/workspace/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:53:1)
    at fn (/workspace/node_modules/webpack/lib/Compilation.js:511:9)
    at _next0 (eval at create (/workspace/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:49:1)
    at eval (eval at create (/workspace/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:64:1)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
caused by plugins in Compilation.hooks.processAssets
TypeError: Cannot read properties of undefined (reading 'endsWith')
    at Integrity.processAssets (/workspace/node_modules/html-bundler-webpack-plugin/src/Plugin/Extras/Integrity.js:128:62)
    at fn (/workspace/node_modules/webpack/lib/Compilation.js:497:10)
    at _next1 (eval at create (/workspace/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:39:1)
    at eval (eval at create (/workspace/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:53:1)
    at fn (/workspace/node_modules/webpack/lib/Compilation.js:511:9)
    at _next0 (eval at create (/workspace/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:49:1)
    at eval (eval at create (/workspace/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:64:1)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
npm error Lifecycle script `build` failed with error:
npm error code 2

Expected behaviour

Webpack compilation completes without errors.

Reproduction

src/home.html

<!DOCTYPE html>
<html>
    <head>
        <title>Home</title>
    </head>
    <body>
        <h1>Hello World!</h1>
       <script src="./main.js"></script>
    </body>
</html>

src/about.html

<!DOCTYPE html>
<html>
    <head>
        <title>About</title>
    </head>
    <body>
        <h1>Hello World!</h1>
       <!-- load the same script with a different URL -->
       <script src="./main.js?q=1"></script>
    </body>
</html>

src/main.js

console.log('Hello World!');

webpack.config.js

const path = require("node:path");
const HtmlBundlerPlugin = require("html-bundler-webpack-plugin");

module.exports = {
  mode: "production",
  output: {
    path: path.join(__dirname, "dist/"),
  },
  plugins: [
    new HtmlBundlerPlugin({
      entry: {
        index: "./src/home.html",
        about: "./src/about.html",
      },
    }),
  ],
};

  • repository URL
  • example code

Environment

  • OS: Debian
  • version of Node.js: v22.12.0
  • version of Webpack: 5.97.1
  • version of the Plugin: 4.13.0

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: 30 (30 by maintainers)

Commits related to this issue

Most upvoted comments

I found a solution for this second Webpack bug! I can fix it on my side.

oh nooo.

After the fix this excepttion I found other Webpack bug, yet more bad.

In this unpredictably case when the sequence of processing modules is changed, Webpack generate a singe JS file but with double content:

source ./main.js

console.log('Hello World!');

wrong generated output file dist/main.fde2c043.js:

console.log("Hello World!"),console.log("Hello World!");

I will try to handle this strange unpredictable case.

In heavy production configurations the probability of an error increases to 100%. But without integrity parameter, compilations complete successfully.

I found the BUG in the Webpack

I don’t know what’s more surprising - catching this error using Webpack as a static HTML generator or finding the right line in a file with more than 2000 lines)

I found the BUG in the Webpack:

Image

Error: Error: Conflicting entry option filename = (pathData, assetInfo) => {
      // the template filename stays the same after changes in watch mode because have not a hash substitution
      if (assetEntryOptions.isTemplate && assetEntryOptions.filename != null) return assetEntryOptions.filename;

      let filename = filenameTemplate;

      if (isFunction(filenameTemplate)) {
        // clone the pathData object to modify the chunk object w/o side effects in the main compilation
        const pathDataCloned = { ...pathData };
        pathDataCloned.chunk = { ...pathDataCloned.chunk };
        if (originalName) {
          pathDataCloned.chunk.name = originalName;
          pathDataCloned.chunk.runtime = originalName;
        }

        // the `filename` property of the `PathData` type should be a source file, but in entry this property not exists
        if (pathDataCloned.filename == null) {
          pathDataCloned.filename = assetEntryOptions.sourceFile;
        }

        filename = filenameTemplate(pathDataCloned, assetInfo);
      }

      if (assetEntryOptions.publicPath) {
        filename = path.posix.join(assetEntryOptions.publicPath, filename);
      }

      return filename;
    } vs (pathData, assetInfo) => {
      // the template filename stays the same after changes in watch mode because have not a hash substitution
      if (assetEntryOptions.isTemplate && assetEntryOptions.filename != null) return assetEntryOptions.filename;

      let filename = filenameTemplate;

      if (isFunction(filenameTemplate)) {
        // clone the pathData object to modify the chunk object w/o side effects in the main compilation
        const pathDataCloned = { ...pathData };
        pathDataCloned.chunk = { ...pathDataCloned.chunk };
        if (originalName) {
          pathDataCloned.chunk.name = originalName;
          pathDataCloned.chunk.runtime = originalName;
        }

        // the `filename` property of the `PathData` type should be a source file, but in entry this property not exists
        if (pathDataCloned.filename == null) {
          pathDataCloned.filename = assetEntryOptions.sourceFile;
        }

        filename = filenameTemplate(pathDataCloned, assetInfo);
      }

      if (assetEntryOptions.publicPath) {
        filename = path.posix.join(assetEntryOptions.publicPath, filename);
      }

      return filename;
    }
    at /workspace/node_modules/html-bundler-webpack-plugin/src/Plugin/AssetEntry.js:648:22
    at Compilation._addEntryItem (/workspace/node_modules/webpack/lib/Compilation.js:2281:13)
    at Compilation.addEntry (/workspace/node_modules/webpack/lib/Compilation.js:2220:8)
    at AssetEntry.addToCompilation (/workspace/node_modules/html-bundler-webpack-plugin/src/Plugin/AssetEntry.js:647:17)
    at #addToCompilation (/workspace/node_modules/html-bundler-webpack-plugin/src/Plugin/Collection.js:129:21)
    at Collection.addResource (/workspace/node_modules/html-bundler-webpack-plugin/src/Plugin/Collection.js:726:33)
    at Render.requireScript (/workspace/node_modules/html-bundler-webpack-plugin/src/Loader/Modes/PreprocessorModeAbstract.js:127:21)
    at Template.resolveFile (/workspace/node_modules/html-bundler-webpack-plugin/src/Loader/Template.js:130:31)
    at Template.resolve (/workspace/node_modules/html-bundler-webpack-plugin/src/Loader/Template.js:39:29)
    at /workspace/node_modules/html-bundler-webpack-plugin/src/Loader/index.js:138:23
    at resolveError (/workspace/node_modules/html-bundler-webpack-plugin/src/Loader/Messages/Exeptions.js:174:10)
    at /workspace/node_modules/html-bundler-webpack-plugin/src/Loader/index.js:164:19