Skip to content

Commit bd17793

Browse files
authored
Add webpack 5 compatibility (#33)
A few webpack plugins needed renaming. Backwards-compatible with webpack 4.
1 parent 1a1b070 commit bd17793

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
import * as Types from '@babel/types';
1818
import path from 'path';
19-
import ParserHelpers from 'webpack/lib/ParserHelpers';
19+
let ParserHelpers;
20+
try {
21+
ParserHelpers = require('webpack/lib/javascript/JavascriptParserHelpers'); // Webpack 5
22+
} catch (e) {}
23+
ParserHelpers = ParserHelpers || require('webpack/lib/ParserHelpers'); // Webpack 4
2024
import WORKER_PLUGIN_SYMBOL from './symbol';
2125

2226
const NAME = 'WorkerPlugin';

src/loader.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@
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 NodeTargetPlugin from 'webpack/lib/node/NodeTargetPlugin';
2221
import WORKER_PLUGIN_SYMBOL from './symbol';
2322

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

@@ -65,7 +70,7 @@ export function pitch (request) {
6570
new NodeTargetPlugin().apply(workerCompiler);
6671
}
6772
(new WebWorkerTemplatePlugin(workerOptions)).apply(workerCompiler);
68-
(new FetchCompileWasmTemplatePlugin({
73+
(new FetchCompileWasmPlugin({
6974
mangleImports: compilerOptions.optimization.mangleWasmImports
7075
})).apply(workerCompiler);
7176
(new SingleEntryPlugin(this.context, request, options.name)).apply(workerCompiler);
@@ -82,7 +87,7 @@ export function pitch (request) {
8287
if (!err && compilation.errors && compilation.errors.length) {
8388
err = compilation.errors[0];
8489
}
85-
const entry = entries && entries[0] && entries[0].files[0];
90+
const entry = entries && entries[0] && entries[0].files.values().next().value; // compatible with Array (v4) and Set (v5) prototypes
8691
if (!err && !entry) err = Error(`WorkerPlugin: no entry for ${request}`);
8792
if (err) return cb(err);
8893
return cb(null, `module.exports = __webpack_public_path__ + ${JSON.stringify(entry)}`);

0 commit comments

Comments
 (0)