From d49e16650689dabfb85ef3a76cabb2854ac7efa5 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Thu, 16 May 2019 10:35:18 -0600 Subject: [PATCH 1/2] Minify worker source --- tasks/serialize-workers.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/tasks/serialize-workers.js b/tasks/serialize-workers.js index dbf7e12c1c..0e2b14182f 100644 --- a/tasks/serialize-workers.js +++ b/tasks/serialize-workers.js @@ -29,26 +29,27 @@ async function build(input, {minify = true} = {}) { } ] ] - }), - { - name: 'serialize worker and export create function', - renderChunk(code) { - return ` - const source = ${JSON.stringify(code)}; - const blob = new Blob([source], {type: 'application/javascript'}); - const url = URL.createObjectURL(blob); - export function create() { - return new Worker(url); - } - `; - } - } + }) ]; if (minify) { plugins.push(terser()); } + plugins.push({ + name: 'serialize worker and export create function', + renderChunk(code) { + return ` + const source = ${JSON.stringify(code)}; + const blob = new Blob([source], {type: 'application/javascript'}); + const url = URL.createObjectURL(blob); + export function create() { + return new Worker(url); + } + `; + } + }); + const bundle = await rollup.rollup({input, plugins}); const {output} = await bundle.generate({format: 'es'}); From 0d489f2ea9ed962b848fc3263dd50ddd19b40fad Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Thu, 16 May 2019 10:36:23 -0600 Subject: [PATCH 2/2] Minify worker in examples for a production build --- examples/webpack/worker-loader.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/examples/webpack/worker-loader.js b/examples/webpack/worker-loader.js index b8d10eae09..cbb7b38bf7 100644 --- a/examples/webpack/worker-loader.js +++ b/examples/webpack/worker-loader.js @@ -3,7 +3,15 @@ const build = require('../../tasks/serialize-workers').build; function loader() { const callback = this.async(); - build(this.resource, {minify: false}) + let minify = false; + + // TODO: remove when https://github.com/webpack/webpack/issues/6496 is addressed + const compilation = this._compilation; + if (compilation) { + minify = compilation.compiler.options.mode === 'production'; + } + + build(this.resource, {minify}) .then(chunk => { for (const filePath in chunk.modules) { this.addDependency(filePath);