Skip to content

Commit

Permalink
Merge pull request #84 from gluck/webpack5
Browse files Browse the repository at this point in the history
Webpack 5 compatibility
  • Loading branch information
developit authored Jul 4, 2020
2 parents f7b576f + b68e6b5 commit f6b1b4f
Showing 1 changed file with 8 additions and 3 deletions.
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

0 comments on commit f6b1b4f

Please sign in to comment.