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

Fails when a depedency (module) requires a webpack "external" module

Open Place1 opened this issue 7 years ago • 3 comments

Awesome library! I've just found an edge case when workerizing a module that depends on a webpack external.

In my webpack config i have:

module.exports = {
  // ...
  externals: {
    'config': JSON.stringify(config),
  },
  // ...
}

and in my worker module I have:

import * as config from 'config';

I'm using typescript in case that matters.

Here's the compilation error from webpack:

Module not found: Error: Can't resolve 'config' in '/directory/src'
 @ ./src/MyWorker.ts 1:0-33

Place1 avatar Feb 14 '18 22:02 Place1

This is likely an issue with Webpack not passing externals to child Compiler instances.

developit avatar Apr 13 '18 02:04 developit

is there any solution for this case? in electron, worker should have the same externals with the renderer

zhaoyao91 avatar Jan 09 '20 03:01 zhaoyao91

If someone wants to open a PR that copies compiler.options.external to the ChildCompiler instance that should fix the Webpack error here.

Personally I'm not sure externals in the Worker compiler actually make sense: any values declared as external are unlikely to be available in the Worker context. Generally they're values defined by other scripts on the main thread, and those aren't automatically run in a Worker too.

Perhaps I'm not thinking about this correctly though, if anyone has a bit more detail on what they're wanting this for I'm all ears!

developit avatar Feb 21 '20 01:02 developit