Skip to content

Commit

Permalink
Fix Electron web worker node integration (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
kalbert312 authored Mar 14, 2020
1 parent 5e0c012 commit 9df552d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ When using TypeScript, make sure this setting is part of your TypeScript configu
}
```

## Electron

To use standard Node packages in Electron web workers, make sure Node integration in workers is [turned on](https://www.electronjs.org/docs/tutorial/multithreading)
and the plugin option [target](#target) is set to `electron-node-worker`.

## Options

In most cases, no options are necessary to use WorkerPlugin.
Expand Down Expand Up @@ -153,6 +158,23 @@ module.exports = {
}
```

### `target`

Due to the way webpack works, it may be necessary to change the target environment of the workers.

Possible values:

- `electron-node-worker` use this if you are using Electron and compiling web workers that have node integration enabled. See [Electron Multithreading](https://www.electronjs.org/docs/tutorial/multithreading).

Default value: `undefined`

Example with electron node workers:
```js
new ThreadsPlugin({
target: 'electron-node-worker'
})
```

## License

Apache-2.0
4 changes: 4 additions & 0 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import loaderUtils from 'loader-utils';
import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin';
import WebWorkerTemplatePlugin from 'webpack/lib/webworker/WebWorkerTemplatePlugin';
import FetchCompileWasmTemplatePlugin from 'webpack/lib/web/FetchCompileWasmTemplatePlugin';
import NodeTargetPlugin from 'webpack/lib/node/NodeTargetPlugin';
import WORKER_PLUGIN_SYMBOL from './symbol';

const NAME = 'WorkerPluginLoader';
Expand Down Expand Up @@ -57,6 +58,9 @@ export function pitch (request) {

const workerCompiler = this._compilation.createChildCompiler(NAME, workerOptions, plugins);
workerCompiler.context = this._compiler.context;
if (pluginOptions.target === "electron-node-worker") {
new NodeTargetPlugin().apply(workerCompiler);
}
(new WebWorkerTemplatePlugin(workerOptions)).apply(workerCompiler);
(new FetchCompileWasmTemplatePlugin({
mangleImports: compilerOptions.optimization.mangleWasmImports
Expand Down

0 comments on commit 9df552d

Please sign in to comment.