Skip to content

Commit

Permalink
fix: parallel in multi compilation mode
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored May 6, 2020
1 parent 2b5961c commit 0ee7ed2
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ class TerserPlugin {
yield task;
}

async runTasks() {
async runTasks(assetNames) {
const availableNumberOfCores = TerserPlugin.getAvailableNumberOfCores(
this.options.parallel
);
Expand All @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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(
Expand All @@ -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();
}

Expand All @@ -604,7 +603,7 @@ class TerserPlugin {
allExtractedComments
);

await this.runTasks();
await this.runTasks(assetNames);

Object.keys(allExtractedComments).forEach((commentsFilename) => {
const extractedComments = new Set([
Expand Down
46 changes: 46 additions & 0 deletions test/TerserPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()],
Expand Down
20 changes: 20 additions & 0 deletions test/__snapshots__/TerserPlugin.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions test/__snapshots__/TerserPlugin.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0ee7ed2

Please sign in to comment.