From 2535fd5a79da90c88c42afc5bd0c33ca2471e5c2 Mon Sep 17 00:00:00 2001 From: Tim Kendrick Date: Mon, 19 Mar 2018 22:26:10 +0000 Subject: [PATCH] Prevent bundling worker fallbacks into compiled workers --- webpack/index.js | 29 +++++++++++++++----- webpack/plugins/AddWorkerEntryPointPlugin.js | 20 ++++++++++---- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/webpack/index.js b/webpack/index.js index 6475d1ab..52bf519e 100644 --- a/webpack/index.js +++ b/webpack/index.js @@ -125,8 +125,16 @@ function createPlugins(webpack, workers, outputPath) { ...Object.keys(IGNORED_IMPORTS).map((id) => createIgnoreImportsPlugin(webpack, id, IGNORED_IMPORTS[id]) ), - ...workers.map(({ id, entry, output }) => - createEntryPointPlugin(webpack, id, resolveMonacoPath(entry), path.join(outputPath, output)) + ...uniqBy(workers, ({ id }) => id).map(({ id, entry, output }) => + new AddWorkerEntryPointPlugin(webpack, { + id, + entry: resolveMonacoPath(entry), + filename: path.join(outputPath, output), + plugins: [ + createContextPlugin(webpack, WORKER_LOADER_PATH, {}), + new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }), + ], + }) ), ...(workerFallbacks ? [createContextPlugin(webpack, WORKER_LOADER_PATH, workerFallbacks)] : []), ]; @@ -137,7 +145,7 @@ function createContextPlugin(webpack, filePath, contextPaths) { new RegExp(`^${path.dirname(filePath)}$`), '', contextPaths - ) + ); } function createIgnoreImportsPlugin(webpack, targetPath, ignoredModules) { @@ -147,10 +155,6 @@ function createIgnoreImportsPlugin(webpack, targetPath, ignoredModules) { ); } -function createEntryPointPlugin(webpack, id, entry, output) { - return new AddWorkerEntryPointPlugin(webpack, { id, entry, output }); -} - function flatMap(items, iteratee) { return items.map(iteratee).reduce((acc, item) => [...acc, ...item], []); } @@ -170,4 +174,15 @@ function mapValues(object, iteratee) { ); } +function uniqBy(items, iteratee) { + const keys = {}; + return items.reduce((acc, item) => { + const key = iteratee(item); + if (key in keys) { return acc; } + keys[key] = true; + acc.push(item); + return acc; + }, []); +} + module.exports = MonacoWebpackPlugin; diff --git a/webpack/plugins/AddWorkerEntryPointPlugin.js b/webpack/plugins/AddWorkerEntryPointPlugin.js index 46f52a35..04f2ab9d 100644 --- a/webpack/plugins/AddWorkerEntryPointPlugin.js +++ b/webpack/plugins/AddWorkerEntryPointPlugin.js @@ -1,15 +1,22 @@ class AddWorkerEntryPointPlugin { - constructor(webpack, { id, entry, output }) { + constructor(webpack, { + id, + entry, + filename, + chunkFilename = undefined, + plugins = undefined, + }) { this.webpack = webpack; - this.options = { id, entry, output }; + this.options = { id, entry, filename, chunkFilename, plugins }; } apply(compiler) { const webpack = this.webpack; - const { id, entry, output } = this.options; - compiler.plugin('make', (compilation, callback) => { + const { id, entry, filename, chunkFilename, plugins } = this.options; + compiler.hooks.make.tapAsync('AddWorkerEntryPointPlugin', (compilation, callback) => { const outputOptions = { - filename: output, + filename, + chunkFilename, publicPath: compilation.outputOptions.publicPath, // HACK: globalObject is necessary to fix https://github.com/webpack/webpack/issues/6642 globalObject: 'this', @@ -17,8 +24,9 @@ class AddWorkerEntryPointPlugin { const childCompiler = compilation.createChildCompiler(id, outputOptions, [ new webpack.webworker.WebWorkerTemplatePlugin(), new webpack.LoaderTargetPlugin('webworker'), - new webpack.SingleEntryPlugin(this.context, entry, 'main'), + new webpack.SingleEntryPlugin(compiler.context, entry, 'main'), ]); + plugins.forEach((plugin) => plugin.apply(childCompiler)); childCompiler.runAsChild(callback); }); }