From 8b88b399cf9c95d7d45e597235d6fa5e5a8aa34c Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Wed, 4 Sep 2019 16:38:56 +0300 Subject: [PATCH] fix: do not run parallel mode when you have only one file (#134) --- src/TaskRunner.js | 2 +- .../parallel-option-failure.test.js.snap | 4 ++ .../parallel-option.test.js.snap | 25 +++++++ test/parallel-option-failure.test.js | 1 + test/parallel-option.test.js | 67 +++++++++++++++++++ 5 files changed, 98 insertions(+), 1 deletion(-) diff --git a/src/TaskRunner.js b/src/TaskRunner.js index bf6989c0..f60ee086 100644 --- a/src/TaskRunner.js +++ b/src/TaskRunner.js @@ -43,7 +43,7 @@ export default class TaskRunner { return; } - if (this.maxConcurrentWorkers > 1) { + if (this.maxConcurrentWorkers > 1 && tasks.length > 1) { const workerOptions = process.platform === 'win32' ? { diff --git a/test/__snapshots__/parallel-option-failure.test.js.snap b/test/__snapshots__/parallel-option-failure.test.js.snap index 6a8d6dc7..2e6bf410 100644 --- a/test/__snapshots__/parallel-option-failure.test.js.snap +++ b/test/__snapshots__/parallel-option-failure.test.js.snap @@ -3,6 +3,8 @@ exports[`parallel option should match snapshot for errors into the "worker-farm" package whe the "cache" option is "true": errors 1`] = ` Array [ "Error: one.js from Terser +Error: worker-farm failed", + "Error: two.js from Terser Error: worker-farm failed", ] `; @@ -12,6 +14,8 @@ exports[`parallel option should match snapshot for errors into the "worker-farm" exports[`parallel option should match snapshot for errors into the "worker-farm" package: errors 1`] = ` Array [ "Error: one.js from Terser +Error: worker-farm failed", + "Error: two.js from Terser Error: worker-farm failed", ] `; diff --git a/test/__snapshots__/parallel-option.test.js.snap b/test/__snapshots__/parallel-option.test.js.snap index ef61fca4..f145684a 100644 --- a/test/__snapshots__/parallel-option.test.js.snap +++ b/test/__snapshots__/parallel-option.test.js.snap @@ -1,5 +1,9 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`parallel option should match snapshot for errors into the "worker-farm" package: errors 1`] = `Array []`; + +exports[`parallel option should match snapshot for errors into the "worker-farm" package: warnings 1`] = `Array []`; + exports[`parallel option should match snapshot for the "2" value: assets 1`] = ` Object { "four.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)}}]);", @@ -26,6 +30,27 @@ exports[`parallel option should match snapshot for the "false" value: errors 1`] exports[`parallel option should match snapshot for the "false" value: warnings 1`] = `Array []`; +exports[`parallel option should match snapshot for the "true" value and only one file passed: 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[`parallel option should match snapshot for the "true" value and only one file passed: errors 1`] = `Array []`; + +exports[`parallel option should match snapshot for the "true" value and only one file passed: warnings 1`] = `Array []`; + +exports[`parallel option should match snapshot for the "true" value and two files passed: assets 1`] = ` +Object { + "one.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)}}]);", + "two.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[`parallel option should match snapshot for the "true" value and two files passed: errors 1`] = `Array []`; + +exports[`parallel option should match snapshot for the "true" value and two files passed: warnings 1`] = `Array []`; + exports[`parallel option should match snapshot for the "true" value: assets 1`] = ` Object { "four.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)}}]);", diff --git a/test/parallel-option-failure.test.js b/test/parallel-option-failure.test.js index 0ce26ff6..a8148916 100644 --- a/test/parallel-option-failure.test.js +++ b/test/parallel-option-failure.test.js @@ -48,6 +48,7 @@ describe('parallel option', () => { compiler = createCompiler({ entry: { one: `${__dirname}/fixtures/entry.js`, + two: `${__dirname}/fixtures/entry.js`, }, }); diff --git a/test/parallel-option.test.js b/test/parallel-option.test.js index 7456a8c4..21a63c2a 100644 --- a/test/parallel-option.test.js +++ b/test/parallel-option.test.js @@ -140,4 +140,71 @@ describe('parallel option', () => { expect(warnings).toMatchSnapshot('warnings'); expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); }); + + it('should match snapshot for the "true" value and only one file passed', async () => { + compiler = createCompiler({ + entry: `${__dirname}/fixtures/entry.js`, + }); + + new TerserPlugin({ parallel: true }).apply(compiler); + + const stats = await compile(compiler); + + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); + + expect(workerFarm.mock.calls.length).toBe(0); + expect(workerFarm.end.mock.calls.length).toBe(0); + + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); + expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + }); + + it('should match snapshot for the "true" value and two files passed', async () => { + compiler = createCompiler({ + entry: { + one: `${__dirname}/fixtures/entry.js`, + two: `${__dirname}/fixtures/entry.js`, + }, + }); + + new TerserPlugin({ parallel: true }).apply(compiler); + + const stats = await compile(compiler); + + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); + + expect(workerFarm.mock.calls.length).toBe(1); + expect(workerFarm.mock.calls[0][0].maxConcurrentWorkers).toBe( + os.cpus().length - 1 + ); + expect(workerFarmMock.mock.calls.length).toBe( + Object.keys(stats.compilation.assets).length + ); + expect(workerFarm.end.mock.calls.length).toBe(1); + + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); + expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + }); + + it('should match snapshot for errors into the "worker-farm" package', async () => { + new TerserPlugin({ parallel: true, cache: false }).apply(compiler); + + const stats = await compile(compiler); + + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); + + expect(workerFarm.mock.calls.length).toBe(1); + expect(workerFarmMock.mock.calls.length).toBe( + Object.keys(stats.compilation.assets).length + ); + expect(workerFarm.end.mock.calls.length).toBe(1); + + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); + }); });