From 0ee7ed270b8cbf6312cc1009c5aa5f909fc7fbb7 Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Wed, 6 May 2020 16:50:04 +0300 Subject: [PATCH] fix: parallel in multi compilation mode --- src/index.js | 19 ++++---- test/TerserPlugin.test.js | 46 +++++++++++++++++++ .../TerserPlugin.test.js.snap.webpack4 | 20 ++++++++ .../TerserPlugin.test.js.snap.webpack5 | 20 ++++++++ 4 files changed, 95 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index 36b22c40..0be0e88c 100644 --- a/src/index.js +++ b/src/index.js @@ -428,7 +428,7 @@ class TerserPlugin { yield task; } - async runTasks() { + async runTasks(assetNames) { const availableNumberOfCores = TerserPlugin.getAvailableNumberOfCores( this.options.parallel ); @@ -438,10 +438,7 @@ class TerserPlugin { if (availableNumberOfCores > 0) { // Do not create unnecessary workers when the number of files is less than the available cores, it saves memory - const numWorkers = Math.min( - this.assetNames.length, - availableNumberOfCores - ); + const numWorkers = Math.min(assetNames.length, availableNumberOfCores); concurrency = numWorkers; @@ -468,7 +465,7 @@ class TerserPlugin { const limit = pLimit(concurrency); const scheduledTasks = []; - for (const assetName of this.assetNames) { + for (const assetName of assetNames) { const enqueue = async (task) => { let taskResult; @@ -565,8 +562,10 @@ class TerserPlugin { this.options ); + let assetNames; + if (TerserPlugin.isWebpack4()) { - this.assetNames = [] + assetNames = [] .concat(Array.from(compilation.additionalChunkAssets || [])) .concat( Array.from(chunksOrAssets).reduce( @@ -576,12 +575,12 @@ class TerserPlugin { ) .filter((file) => matchObject(file)); } else { - this.assetNames = [] + assetNames = [] .concat(Object.keys(chunksOrAssets)) .filter((file) => matchObject(file)); } - if (this.assetNames.length === 0) { + if (assetNames.length === 0) { return Promise.resolve(); } @@ -604,7 +603,7 @@ class TerserPlugin { allExtractedComments ); - await this.runTasks(); + await this.runTasks(assetNames); Object.keys(allExtractedComments).forEach((commentsFilename) => { const extractedComments = new Set([ diff --git a/test/TerserPlugin.test.js b/test/TerserPlugin.test.js index 39a91eed..19bc261a 100644 --- a/test/TerserPlugin.test.js +++ b/test/TerserPlugin.test.js @@ -197,6 +197,52 @@ describe('TerserPlugin', () => { }); }); + it('should work in multi compiler mode with the one plugin and with the same file', async () => { + const plugins = [new TerserPlugin()]; + const multiCompiler = getCompiler([ + { + mode: 'production', + bail: true, + cache: getCompiler.isWebpack4() ? false : { type: 'memory' }, + entry: `${__dirname}/fixtures/entry.js`, + output: { + path: `${__dirname}/dist-0`, + filename: '[name].js', + chunkFilename: '[id].[name].js', + }, + optimization: { + minimize: false, + }, + plugins, + }, + { + mode: 'production', + bail: true, + cache: getCompiler.isWebpack4() ? false : { type: 'memory' }, + entry: `${__dirname}/fixtures/entry.js`, + output: { + path: `${__dirname}/dist-1`, + filename: '[name].js', + chunkFilename: '[id].[name].js', + }, + optimization: { + minimize: false, + }, + plugins, + }, + ]); + + const multiStats = await compile(multiCompiler); + + multiStats.stats.forEach((stats, index) => { + expect( + readsAssets(multiCompiler.compilers[index], stats) + ).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); + }); + }); + it('should work as a plugin', async () => { const compiler = getCompiler({ plugins: [new TerserPlugin()], diff --git a/test/__snapshots__/TerserPlugin.test.js.snap.webpack4 b/test/__snapshots__/TerserPlugin.test.js.snap.webpack4 index 278a1b73..2df522b0 100644 --- a/test/__snapshots__/TerserPlugin.test.js.snap.webpack4 +++ b/test/__snapshots__/TerserPlugin.test.js.snap.webpack4 @@ -184,6 +184,26 @@ exports[`TerserPlugin should work as a plugin: errors 1`] = `Array []`; exports[`TerserPlugin should work as a plugin: warnings 1`] = `Array []`; +exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: assets 1`] = ` +Object { + "main.js": "!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,\\"a\\",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p=\\"\\",n(n.s=0)}([function(e,t){e.exports=function(){console.log(7)}}]);", +} +`; + +exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: assets 2`] = ` +Object { + "main.js": "!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,\\"a\\",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p=\\"\\",n(n.s=0)}([function(e,t){e.exports=function(){console.log(7)}}]);", +} +`; + +exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: errors 1`] = `Array []`; + +exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: errors 2`] = `Array []`; + +exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: warnings 1`] = `Array []`; + +exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: warnings 2`] = `Array []`; + exports[`TerserPlugin should work in multi compiler mode with the one plugin: assets 1`] = ` Object { "main-1.js": "/******/ (function(modules) { // webpackBootstrap diff --git a/test/__snapshots__/TerserPlugin.test.js.snap.webpack5 b/test/__snapshots__/TerserPlugin.test.js.snap.webpack5 index c8e37861..a1073716 100644 --- a/test/__snapshots__/TerserPlugin.test.js.snap.webpack5 +++ b/test/__snapshots__/TerserPlugin.test.js.snap.webpack5 @@ -184,6 +184,26 @@ exports[`TerserPlugin should work as a plugin: errors 1`] = `Array []`; exports[`TerserPlugin should work as a plugin: warnings 1`] = `Array []`; +exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: assets 1`] = ` +Object { + "main.js": "(()=>{var r={791:r=>{r.exports=function(){console.log(7)}}},o={};!function t(e){if(o[e])return o[e].exports;var n=o[e]={exports:{}};return r[e](n,n.exports,t),n.exports}(791)})();", +} +`; + +exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: assets 2`] = ` +Object { + "main.js": "(()=>{var r={791:r=>{r.exports=function(){console.log(7)}}},o={};!function t(e){if(o[e])return o[e].exports;var n=o[e]={exports:{}};return r[e](n,n.exports,t),n.exports}(791)})();", +} +`; + +exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: errors 1`] = `Array []`; + +exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: errors 2`] = `Array []`; + +exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: warnings 1`] = `Array []`; + +exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: warnings 2`] = `Array []`; + exports[`TerserPlugin should work in multi compiler mode with the one plugin: assets 1`] = ` Object { "main-1.js": "/******/ (() => { // webpackBootstrap