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

@ -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);
});
}