From 19632dfaa6c3ed413cec46f0392a337ee6668c50 Mon Sep 17 00:00:00 2001 From: Brijesh Bittu <717550+brijeshb42@users.noreply.github.com> Date: Mon, 18 Jun 2018 15:23:20 +0530 Subject: [PATCH] Add support for webpack 3 --- plugins/AddWorkerEntryPointPlugin.js | 46 ++++++++++++++++++---------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/plugins/AddWorkerEntryPointPlugin.js b/plugins/AddWorkerEntryPointPlugin.js index 306c3d1b..9dc8d46c 100644 --- a/plugins/AddWorkerEntryPointPlugin.js +++ b/plugins/AddWorkerEntryPointPlugin.js @@ -1,4 +1,27 @@ const webpack = require('webpack'); +const webpackVersion = require('webpack/package.json').version; +const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin'); +const LoaderTargetPlugin = require('webpack/lib/LoaderTargetPlugin'); +const WebWorkerTemplatePlugin = require('webpack/lib/webworker/WebWorkerTemplatePlugin'); + +function getCompilerHook(compiler, {id, entry, filename, chunkFilename, plugins}) { + return function(compilation, callback) { + const outputOptions = { + filename, + chunkFilename, + publicPath: compilation.outputOptions.publicPath, + // HACK: globalObject is necessary to fix https://github.com/webpack/webpack/issues/6642 + globalObject: 'this', + }; + const childCompiler = compilation.createChildCompiler(id, outputOptions, [ + new WebWorkerTemplatePlugin(), + new LoaderTargetPlugin('webworker'), + new SingleEntryPlugin(compiler.context, entry, 'main'), + ]); + plugins.forEach((plugin) => plugin.apply(childCompiler)); + childCompiler.runAsChild(callback); + } +} class AddWorkerEntryPointPlugin { constructor({ @@ -12,23 +35,12 @@ class AddWorkerEntryPointPlugin { } apply(compiler) { - const { id, entry, filename, chunkFilename, plugins } = this.options; - compiler.hooks.make.tapAsync('AddWorkerEntryPointPlugin', (compilation, callback) => { - const outputOptions = { - filename, - chunkFilename, - publicPath: compilation.outputOptions.publicPath, - // HACK: globalObject is necessary to fix https://github.com/webpack/webpack/issues/6642 - globalObject: 'this', - }; - const childCompiler = compilation.createChildCompiler(id, outputOptions, [ - new webpack.webworker.WebWorkerTemplatePlugin(), - new webpack.LoaderTargetPlugin('webworker'), - new webpack.SingleEntryPlugin(compiler.context, entry, 'main'), - ]); - plugins.forEach((plugin) => plugin.apply(childCompiler)); - childCompiler.runAsChild(callback); - }); + const compilerHook = getCompilerHook(compiler, this.options); + if (webpackVersion < '4') { + compiler.plugin('make', compilerHook); + } else { + compiler.hooks.make.tapAsync('AddWorkerEntryPointPlugin', compilerHook); + } } }