Skip to content
This repository was archived by the owner on Apr 19, 2026. It is now read-only.

Commit f6b1b4f

Browse files
authored
Merge pull request #84 from gluck/webpack5
Webpack 5 compatibility
2 parents f7b576f + b68e6b5 commit f6b1b4f

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

src/loader.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@
1717
import loaderUtils from 'loader-utils';
1818
import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin';
1919
import WebWorkerTemplatePlugin from 'webpack/lib/webworker/WebWorkerTemplatePlugin';
20-
import FetchCompileWasmTemplatePlugin from 'webpack/lib/web/FetchCompileWasmTemplatePlugin';
2120
import WORKER_PLUGIN_SYMBOL from './symbol';
2221

22+
let FetchCompileWasmPlugin;
23+
try {
24+
FetchCompileWasmPlugin = require('webpack/lib/web/FetchCompileWasmPlugin'); // Webpack 5
25+
} catch (e) {}
26+
FetchCompileWasmPlugin = FetchCompileWasmPlugin || require('webpack/lib/web/FetchCompileWasmTemplatePlugin'); // Webpack 4
27+
2328
const NAME = 'WorkerPluginLoader';
2429
let hasWarned = false;
2530

@@ -59,7 +64,7 @@ export function pitch (request) {
5964
const workerCompiler = this._compilation.createChildCompiler(NAME, workerOptions, plugins);
6065
workerCompiler.context = this._compiler.context;
6166
(new WebWorkerTemplatePlugin()).apply(workerCompiler);
62-
(new FetchCompileWasmTemplatePlugin({
67+
(new FetchCompileWasmPlugin({
6368
mangleImports: compilerOptions.optimization.mangleWasmImports
6469
})).apply(workerCompiler);
6570
(new SingleEntryPlugin(this.context, request, options.name)).apply(workerCompiler);
@@ -76,7 +81,7 @@ export function pitch (request) {
7681
if (!err && compilation.errors && compilation.errors.length) {
7782
err = compilation.errors[0];
7883
}
79-
const entry = entries && entries[0] && entries[0].files[0];
84+
const entry = entries && entries[0] && entries[0].files.values().next().value; // compatible with Array (v4) and Set (v5) prototypes
8085
if (!err && !entry) err = Error(`WorkerPlugin: no entry for ${request}`);
8186
if (err) return cb(err);
8287
return cb(null, `${options.esModule ? 'export default' : 'module.exports ='} __webpack_public_path__ + ${JSON.stringify(entry)}`);

0 commit comments

Comments
 (0)