Skip to content

Commit 9df552d

Browse files
authored
Fix Electron web worker node integration (#16)
1 parent 5e0c012 commit 9df552d

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ When using TypeScript, make sure this setting is part of your TypeScript configu
100100
}
101101
```
102102

103+
## Electron
104+
105+
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)
106+
and the plugin option [target](#target) is set to `electron-node-worker`.
107+
103108
## Options
104109

105110
In most cases, no options are necessary to use WorkerPlugin.
@@ -153,6 +158,23 @@ module.exports = {
153158
}
154159
```
155160

161+
### `target`
162+
163+
Due to the way webpack works, it may be necessary to change the target environment of the workers.
164+
165+
Possible values:
166+
167+
- `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).
168+
169+
Default value: `undefined`
170+
171+
Example with electron node workers:
172+
```js
173+
new ThreadsPlugin({
174+
target: 'electron-node-worker'
175+
})
176+
```
177+
156178
## License
157179

158180
Apache-2.0

src/loader.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import loaderUtils from 'loader-utils';
1818
import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin';
1919
import WebWorkerTemplatePlugin from 'webpack/lib/webworker/WebWorkerTemplatePlugin';
2020
import FetchCompileWasmTemplatePlugin from 'webpack/lib/web/FetchCompileWasmTemplatePlugin';
21+
import NodeTargetPlugin from 'webpack/lib/node/NodeTargetPlugin';
2122
import WORKER_PLUGIN_SYMBOL from './symbol';
2223

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

5859
const workerCompiler = this._compilation.createChildCompiler(NAME, workerOptions, plugins);
5960
workerCompiler.context = this._compiler.context;
61+
if (pluginOptions.target === "electron-node-worker") {
62+
new NodeTargetPlugin().apply(workerCompiler);
63+
}
6064
(new WebWorkerTemplatePlugin(workerOptions)).apply(workerCompiler);
6165
(new FetchCompileWasmTemplatePlugin({
6266
mangleImports: compilerOptions.optimization.mangleWasmImports

0 commit comments

Comments
 (0)