Prevent bundling worker fallbacks into compiled workers

This commit is contained in:
Tim Kendrick 2018-03-19 22:26:10 +00:00
parent a47ca0bf3e
commit 2535fd5a79
2 changed files with 36 additions and 13 deletions

View file

@ -125,8 +125,16 @@ function createPlugins(webpack, workers, outputPath) {
...Object.keys(IGNORED_IMPORTS).map((id) => ...Object.keys(IGNORED_IMPORTS).map((id) =>
createIgnoreImportsPlugin(webpack, id, IGNORED_IMPORTS[id]) createIgnoreImportsPlugin(webpack, id, IGNORED_IMPORTS[id])
), ),
...workers.map(({ id, entry, output }) => ...uniqBy(workers, ({ id }) => id).map(({ id, entry, output }) =>
createEntryPointPlugin(webpack, id, resolveMonacoPath(entry), path.join(outputPath, 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)] : []), ...(workerFallbacks ? [createContextPlugin(webpack, WORKER_LOADER_PATH, workerFallbacks)] : []),
]; ];
@ -137,7 +145,7 @@ function createContextPlugin(webpack, filePath, contextPaths) {
new RegExp(`^${path.dirname(filePath)}$`), new RegExp(`^${path.dirname(filePath)}$`),
'', '',
contextPaths contextPaths
) );
} }
function createIgnoreImportsPlugin(webpack, targetPath, ignoredModules) { 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) { function flatMap(items, iteratee) {
return items.map(iteratee).reduce((acc, item) => [...acc, ...item], []); 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; module.exports = MonacoWebpackPlugin;

View file

@ -1,15 +1,22 @@
class AddWorkerEntryPointPlugin { class AddWorkerEntryPointPlugin {
constructor(webpack, { id, entry, output }) { constructor(webpack, {
id,
entry,
filename,
chunkFilename = undefined,
plugins = undefined,
}) {
this.webpack = webpack; this.webpack = webpack;
this.options = { id, entry, output }; this.options = { id, entry, filename, chunkFilename, plugins };
} }
apply(compiler) { apply(compiler) {
const webpack = this.webpack; const webpack = this.webpack;
const { id, entry, output } = this.options; const { id, entry, filename, chunkFilename, plugins } = this.options;
compiler.plugin('make', (compilation, callback) => { compiler.hooks.make.tapAsync('AddWorkerEntryPointPlugin', (compilation, callback) => {
const outputOptions = { const outputOptions = {
filename: output, filename,
chunkFilename,
publicPath: compilation.outputOptions.publicPath, publicPath: compilation.outputOptions.publicPath,
// HACK: globalObject is necessary to fix https://github.com/webpack/webpack/issues/6642 // HACK: globalObject is necessary to fix https://github.com/webpack/webpack/issues/6642
globalObject: 'this', globalObject: 'this',
@ -17,8 +24,9 @@ class AddWorkerEntryPointPlugin {
const childCompiler = compilation.createChildCompiler(id, outputOptions, [ const childCompiler = compilation.createChildCompiler(id, outputOptions, [
new webpack.webworker.WebWorkerTemplatePlugin(), new webpack.webworker.WebWorkerTemplatePlugin(),
new webpack.LoaderTargetPlugin('webworker'), 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); childCompiler.runAsChild(callback);
}); });
} }