worker-loader icon indicating copy to clipboard operation
worker-loader copied to clipboard

build error: chunkFilename.replace is not a function at getDefaultChunkFilename

Open sanderg93 opened this issue 3 years ago • 5 comments

  • Operating System: Ubuntu 20.04
  • Node Version: v14.16.1
  • NPM Version: 7.9.0
  • webpack Version: 5.30.0
  • worker-loader Version: v3.0.8

Expected Behavior

PdfjsLib library uses worker-loader to load pdf worker.

Expecting for webpack to build just fine.

Actual Behavior

Building gives the following error:

ERROR in ./node_modules/pdfjs-dist/build/pdf.worker.js (./node_modules/worker-loader/dist/cjs.js?esModule=false&filename=[name].js!./node_modules/pdfjs-dist/build/pdf.worker.js) Module build failed (from ./node_modules/worker-loader/dist/cjs.js): TypeError: chunkFilename.replace is not a function at getDefaultChunkFilename (~/node_modules/worker-loader/dist/utils.js:23:24) at Object.pitch (~/node_modules/worker-loader/dist/index.js:61:108)

webpack compiled with 1 error

Code

"use strict";

var pdfjs = require("./build/pdf.js");
var PdfjsWorker = require("worker-loader?esModule=false&filename=[name].js!./build/pdf.worker.js");

if (typeof window !== "undefined" && "Worker" in window) {
  pdfjs.GlobalWorkerOptions.workerPort = new PdfjsWorker();
}

module.exports = pdfjs;

How Do We Reproduce?

Try to run the above code with the versions mentioned

sanderg93 avatar Apr 16 '21 10:04 sanderg93

npm install [email protected] is used in this example

sanderg93 avatar Apr 16 '21 10:04 sanderg93

You don't need this loader for webpack v5, please look at this https://webpack.js.org/guides/web-workers/

alexander-akait avatar Apr 16 '21 11:04 alexander-akait

I've got the exact same issue.

@alexander-akait when using that approach all my jest tests that are importing the module that uses that syntax explodes. It's due to this issue, which seems to be unfixable atm.

AdamRamberg avatar Apr 22 '21 13:04 AdamRamberg

Let's open an issue in jest, because it is valid ESM

alexander-akait avatar Apr 22 '21 13:04 alexander-akait

There is already a long-lived issue open that aims to solve this problem here. Haven't been diving into the specifics too much, but from my understanding this is not a problem that is easily fixed.

Just noticed that it seems to be fixed in jest 27 though. See this comment.

AdamRamberg avatar Apr 22 '21 14:04 AdamRamberg