Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack 5 compatibility #84

Merged
merged 1 commit into from
Jul 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
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 WORKER_PLUGIN_SYMBOL from './symbol';

let FetchCompileWasmPlugin;
try {
FetchCompileWasmPlugin = require('webpack/lib/web/FetchCompileWasmPlugin'); // Webpack 5
} catch (e) {}
FetchCompileWasmPlugin = FetchCompileWasmPlugin || require('webpack/lib/web/FetchCompileWasmTemplatePlugin'); // Webpack 4

const NAME = 'WorkerPluginLoader';
let hasWarned = false;

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