Skip to content

Commit

Permalink
feat: enable the parallel option by default (#131)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the `parallel` option is `true` by default
  • Loading branch information
evilebottnawi committed Sep 3, 2019
1 parent 41a9fff commit 7b342af
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ module.exports = {
### `parallel`

Type: `Boolean|Number`
Default: `false`
Default: `true`

Use multi-process parallel running to improve the build speed.
Default number of concurrent runs: `os.cpus().length - 1`.
Expand Down
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
testEnvironment: 'node',
};
9 changes: 5 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* eslint-disable
no-param-reassign
*/
import crypto from 'crypto';
import path from 'path';

Expand Down Expand Up @@ -34,7 +31,7 @@ class TerserPlugin {
sourceMap = false,
cache = false,
cacheKeys = (defaultCacheKeys) => defaultCacheKeys,
parallel = false,
parallel = true,
include,
exclude,
} = options;
Expand Down Expand Up @@ -160,6 +157,7 @@ class TerserPlugin {
apply(compiler) {
const buildModuleFn = (moduleArg) => {
// to get detailed location info about errors
// eslint-disable-next-line no-param-reassign
moduleArg.useSourceMap = true;
};

Expand Down Expand Up @@ -356,19 +354,22 @@ class TerserPlugin {
compilation.assets[commentsFile].add('\n');
compilation.assets[commentsFile].add(commentsSource);
} else {
// eslint-disable-next-line no-param-reassign
compilation.assets[commentsFile] = new ConcatSource(
compilation.assets[commentsFile],
'\n',
commentsSource
);
}
} else {
// eslint-disable-next-line no-param-reassign
compilation.assets[commentsFile] = commentsSource;
}
}
}

// Updating assets
// eslint-disable-next-line no-param-reassign
processedAssets.add((compilation.assets[file] = outputSource));

// Handling warnings
Expand Down
7 changes: 2 additions & 5 deletions src/minify.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/* eslint-disable
arrow-body-style
*/
import { minify as terserMinify } from 'terser';
const { minify: terserMinify } = require('terser');

const buildTerserOptions = ({
ecma,
Expand Down Expand Up @@ -187,4 +184,4 @@ const minify = (options) => {
return { error, map, code, warnings, extractedComments };
};

export default minify;
module.exports = minify;
2 changes: 1 addition & 1 deletion src/worker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import minify from './minify';
const minify = require('./minify');

module.exports = (options, callback) => {
try {
Expand Down
2 changes: 1 addition & 1 deletion test/TerserPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('TerserPlugin', () => {
mappings: '',
};

it('should works (without options)', async () => {
it('should work (without options)', async () => {
const compiler = createCompiler();

new TerserPlugin().apply(compiler);
Expand Down
6 changes: 3 additions & 3 deletions test/__snapshots__/TerserPlugin.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ exports[`TerserPlugin should regenerate hash: errors 1`] = `Array []`;

exports[`TerserPlugin should regenerate hash: warnings 1`] = `Array []`;

exports[`TerserPlugin should works (without options): assets 1`] = `
exports[`TerserPlugin should work (without options): 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 works (without options): errors 1`] = `Array []`;
exports[`TerserPlugin should work (without options): errors 1`] = `Array []`;

exports[`TerserPlugin should works (without options): warnings 1`] = `Array []`;
exports[`TerserPlugin should work (without options): warnings 1`] = `Array []`;
13 changes: 13 additions & 0 deletions test/__snapshots__/parallel-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,16 @@ Object {
exports[`parallel option should match snapshot for the "true" value: errors 1`] = `Array []`;

exports[`parallel option should match snapshot for the "true" value: warnings 1`] = `Array []`;

exports[`parallel option should match snapshot when a value is not specify: 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)}}]);",
"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)}}]);",
"three.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 when a value is not specify: errors 1`] = `Array []`;

exports[`parallel option should match snapshot when a value is not specify: warnings 1`] = `Array []`;
24 changes: 24 additions & 0 deletions test/parallel-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ jest.mock('worker-farm', () => {
require(worker)(data, callback)
))
);

mock.end = jest.fn();

return mock;
});

Expand All @@ -49,6 +51,28 @@ describe('parallel option', () => {
});
});

it('should match snapshot when a value is not specify', async () => {
new TerserPlugin().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 the "false" value', async () => {
new TerserPlugin({ parallel: false }).apply(compiler);

Expand Down
1 change: 1 addition & 0 deletions test/terserOptions-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ describe('terserOptions option', () => {
const compiler = createCompiler();

new TerserPlugin({
parallel: false,
terserOptions: {
output: {
unknown: true,
Expand Down

0 comments on commit 7b342af

Please sign in to comment.