Skip to content

Commit

Permalink
fix: do not run parallel mode when you have only one file (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Sep 4, 2019
1 parent 84fed6b commit 8b88b39
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/TaskRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
? {
Expand Down
4 changes: 4 additions & 0 deletions test/__snapshots__/parallel-option-failure.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
`;
Expand All @@ -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",
]
`;
Expand Down
25 changes: 25 additions & 0 deletions test/__snapshots__/parallel-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -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)}}]);",
Expand All @@ -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)}}]);",
Expand Down
1 change: 1 addition & 0 deletions test/parallel-option-failure.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('parallel option', () => {
compiler = createCompiler({
entry: {
one: `${__dirname}/fixtures/entry.js`,
two: `${__dirname}/fixtures/entry.js`,
},
});

Expand Down
67 changes: 67 additions & 0 deletions test/parallel-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

0 comments on commit 8b88b39

Please sign in to comment.