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

Circular dependency not working with worker-loader?

Open webpack-bot opened this issue 5 years ago • 3 comments

I've got package A that depends on package B, which depends on package C (worker), which depends on package A.

I'm getting an error like this: http://trusktr.io:7777/ixujinuzij


This issue was moved from webpack/webpack#1564 by @evilebottnawi. Original issue was by @trusktr.

webpack-bot avatar Jul 29 '20 12:07 webpack-bot

I agree, I had a similar issue. For me, the build was never completed and just hung. I worked around it by removing circular dependencies in my code.

mwanago avatar Nov 25 '20 12:11 mwanago

I have also run into this issue recently. In my case, I am trying to write 2 versions of a library (one has the main thread run expensive() directly, and the other has main posting runExpensive messages to a web worker).

I only managed to get around this by writing 2 separate files main.js (expensive() runs in-place) and worker.js (runExpensive message is posted to a webworker), and then using the nasty webpack.NormalModuleReplacementPlugin in my webpack config.

Instead of importing the module directly, my code was changed to something like import * as api from ./api/THREAD_TARGET and then...

new webpack.NormalModuleReplacementPlugin(/THREAD_TARGET/, function(resource) {
        // Imports occuring in a webworker should use main; otherwise worker.                                                                                                                       
        const compiler = resource.contextInfo.compiler;
        const mainOk = (compiler && compiler.startsWith("worker-loader"));
        if (mainOk) {
          const newRequest = resource.request.replace(/THREAD_TARGET/, "main");
          resource.request = newRequest;
        } else {
          const newRequest = resource.request.replace(/THREAD_TARGET/, "worker");
          resource.request = newRequest;
        }
      })

rcrowell avatar Jan 22 '21 03:01 rcrowell

@rcrowell What is version of webpack?

alexander-akait avatar Jan 22 '21 10:01 alexander-akait