From e8f57288fd6b5fff46eeea7f72fdcd79d56861af Mon Sep 17 00:00:00 2001 From: Sam Denty Date: Wed, 18 Mar 2020 21:39:34 +0000 Subject: [PATCH 1/4] feat: importScripts for chunks --- src/loader.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/loader.js b/src/loader.js index ae8da6e..9254521 100644 --- a/src/loader.js +++ b/src/loader.js @@ -18,6 +18,7 @@ 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 JsonpTemplatePlugin from 'webpack/lib/web/JsonpTemplatePlugin' import WORKER_PLUGIN_SYMBOL from './symbol'; const NAME = 'WorkerPluginLoader'; @@ -41,7 +42,7 @@ export function pitch (request) { const chunkFilename = compilerOptions.output.chunkFilename.replace(/\.([a-z]+)$/i, '.worker.$1'); const workerOptions = { filename: chunkFilename.replace(/\[(?:chunkhash|contenthash)(:\d+(?::\d+)?)?\]/g, '[hash$1]'), - chunkFilename, + chunkFilename: compilerOptions.output.chunkFilename,, globalObject: pluginOptions.globalObject || 'self' }; @@ -58,6 +59,8 @@ export function pitch (request) { const workerCompiler = this._compilation.createChildCompiler(NAME, workerOptions, plugins); workerCompiler.context = this._compiler.context; + + (new JsonpTemplatePlugin()).apply(workerCompiler) (new WebWorkerTemplatePlugin(workerOptions)).apply(workerCompiler); (new FetchCompileWasmTemplatePlugin({ mangleImports: compilerOptions.optimization.mangleWasmImports @@ -70,13 +73,28 @@ export function pitch (request) { if (!compilation.cache[subCache]) compilation.cache[subCache] = {}; compilation.cache = compilation.cache[subCache]; } + + compilation.hooks.optimizeChunkAssets.tapAsync(NAME, (chunks, callback) => { + const allFiles = chunks.filter(chunk => chunk.name !== options.name).reduce((accumulated, chunk) => { + return accumulated.concat(chunk.files); + }, []); + const uniqueFiles = [...new Set(allFiles)]; + + if (uniqueFiles.length) { + // Sort to ensure the output is predictable. + uniqueFiles.sort(); + const file = chunks.find(c => c.name === options.name).files[0] + compilation.assets[file] = new ConcatSource(uniqueFiles.map(chunk => `importScripts(${JSON.stringify(chunk)});`).join('\n'), compilation.assets[file]) + } + callback() + }) }); workerCompiler.runAsChild((err, entries, compilation) => { if (!err && compilation.errors && compilation.errors.length) { err = compilation.errors[0]; } - const entry = entries && entries[0] && entries[0].files[0]; + const entry = entries && entries[entries.length - 1] && entries[entries.length - 1].files[0]; 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)}`); From d280e4f53244d8f9ba31318fa3595c38f6e3dc96 Mon Sep 17 00:00:00 2001 From: Sam Denty Date: Wed, 18 Mar 2020 21:48:24 +0000 Subject: [PATCH 2/4] fix: split chunks plugin --- src/loader.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/loader.js b/src/loader.js index 9254521..3001744 100644 --- a/src/loader.js +++ b/src/loader.js @@ -19,6 +19,8 @@ import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin'; import WebWorkerTemplatePlugin from 'webpack/lib/webworker/WebWorkerTemplatePlugin'; import FetchCompileWasmTemplatePlugin from 'webpack/lib/web/FetchCompileWasmTemplatePlugin'; import JsonpTemplatePlugin from 'webpack/lib/web/JsonpTemplatePlugin' +import SplitChunksPlugin from 'webpack/lib/optimize/SplitChunksPlugin' +import { ConcatSource } from 'webpack-sources' import WORKER_PLUGIN_SYMBOL from './symbol'; const NAME = 'WorkerPluginLoader'; @@ -60,7 +62,12 @@ export function pitch (request) { const workerCompiler = this._compilation.createChildCompiler(NAME, workerOptions, plugins); workerCompiler.context = this._compiler.context; - (new JsonpTemplatePlugin()).apply(workerCompiler) + const splitChunks = compilerOptions.optimization && compilerOptions.optimization.splitChunks + if (splitChunks) { + (new SplitChunksPlugin(splitChunks)).apply(workerCompiler); + } + + (new JsonpTemplatePlugin()).apply(workerCompiler); (new WebWorkerTemplatePlugin(workerOptions)).apply(workerCompiler); (new FetchCompileWasmTemplatePlugin({ mangleImports: compilerOptions.optimization.mangleWasmImports @@ -83,10 +90,10 @@ export function pitch (request) { if (uniqueFiles.length) { // Sort to ensure the output is predictable. uniqueFiles.sort(); - const file = chunks.find(c => c.name === options.name).files[0] - compilation.assets[file] = new ConcatSource(uniqueFiles.map(chunk => `importScripts(${JSON.stringify(chunk)});`).join('\n'), compilation.assets[file]) + const file = chunks.find(c => c.name === options.name).files[0]; + compilation.assets[file] = new ConcatSource(uniqueFiles.map(chunk => `importScripts(${JSON.stringify(chunk)});`).join('\n'), compilation.assets[file]); } - callback() + callback(); }) }); From 69eadc89ae6dc425f29044f97eca50b39135eabe Mon Sep 17 00:00:00 2001 From: Sam Denty Date: Wed, 18 Mar 2020 22:05:10 +0000 Subject: [PATCH 3/4] fix: use single importScripts --- src/loader.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/loader.js b/src/loader.js index 3001744..75eafd8 100644 --- a/src/loader.js +++ b/src/loader.js @@ -91,8 +91,9 @@ export function pitch (request) { // Sort to ensure the output is predictable. uniqueFiles.sort(); const file = chunks.find(c => c.name === options.name).files[0]; - compilation.assets[file] = new ConcatSource(uniqueFiles.map(chunk => `importScripts(${JSON.stringify(chunk)});`).join('\n'), compilation.assets[file]); + compilation.assets[file] = new ConcatSource(`importScripts(${uniqueFiles.map(chunk => `${JSON.stringify(chunk)}`).join(', ')});\n\n`, compilation.assets[file]); } + callback(); }) }); From 20963444d43ca5835bc78708bda088f39db371e3 Mon Sep 17 00:00:00 2001 From: Sam Denty Date: Wed, 18 Mar 2020 22:17:12 +0000 Subject: [PATCH 4/4] fix: typo --- src/loader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/loader.js b/src/loader.js index 75eafd8..d3d9f28 100644 --- a/src/loader.js +++ b/src/loader.js @@ -44,7 +44,7 @@ export function pitch (request) { const chunkFilename = compilerOptions.output.chunkFilename.replace(/\.([a-z]+)$/i, '.worker.$1'); const workerOptions = { filename: chunkFilename.replace(/\[(?:chunkhash|contenthash)(:\d+(?::\d+)?)?\]/g, '[hash$1]'), - chunkFilename: compilerOptions.output.chunkFilename,, + chunkFilename: compilerOptions.output.chunkFilename, globalObject: pluginOptions.globalObject || 'self' };