Skip to content

Commit

Permalink
fix: multi-compiler crash
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Jun 15, 2020
1 parent 2362813 commit f6499af
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 72 deletions.
66 changes: 22 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 14 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ class TerserPlugin {
yield task;
}

async runTasks(assetNames) {
async runTasks(assetNames, getTaskForAsset, cache) {
const availableNumberOfCores = TerserPlugin.getAvailableNumberOfCores(
this.options.parallel
);
Expand Down Expand Up @@ -482,8 +482,8 @@ class TerserPlugin {
taskResult = { error };
}

if (this.cache.isEnabled() && !taskResult.error) {
taskResult = await this.cache.store(task, taskResult).then(
if (cache.isEnabled() && !taskResult.error) {
taskResult = await cache.store(task, taskResult).then(
() => taskResult,
() => taskResult
);
Expand All @@ -496,15 +496,15 @@ class TerserPlugin {

scheduledTasks.push(
limit(() => {
const task = this.getTaskForAsset(assetName).next().value;
const task = getTaskForAsset(assetName).next().value;

if (!task) {
// Something went wrong, for example the `cacheKeys` option throw an error
return Promise.resolve();
}

if (this.cache.isEnabled()) {
return this.cache.get(task).then(
if (cache.isEnabled()) {
return cache.get(task).then(
(taskResult) => task.callback(taskResult),
() => enqueue(task)
);
Expand Down Expand Up @@ -587,26 +587,21 @@ class TerserPlugin {
return Promise.resolve();
}

const CacheEngine = TerserPlugin.isWebpack4()
? // eslint-disable-next-line global-require
require('./Webpack4Cache').default
: // eslint-disable-next-line global-require
require('./Webpack5Cache').default;

this.cache = new CacheEngine(compilation, {
cache: this.options.cache,
});

const allExtractedComments = {};

this.getTaskForAsset = this.taskGenerator.bind(
const getTaskForAsset = this.taskGenerator.bind(
this,
compiler,
compilation,
allExtractedComments
);
const CacheEngine = TerserPlugin.isWebpack4()
? // eslint-disable-next-line global-require
require('./Webpack4Cache').default
: // eslint-disable-next-line global-require
require('./Webpack5Cache').default;
const cache = new CacheEngine(compilation, { cache: this.options.cache });

await this.runTasks(assetNames);
await this.runTasks(assetNames, getTaskForAsset, cache);

Object.keys(allExtractedComments).forEach((commentsFilename) => {
const extractedComments = new Set([
Expand Down
47 changes: 47 additions & 0 deletions test/TerserPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
removeCache,
} from './helpers';

jest.setTimeout(10000);

describe('TerserPlugin', () => {
const rawSourceMap = {
version: 3,
Expand Down Expand Up @@ -252,6 +254,51 @@ describe('TerserPlugin', () => {
},
plugins,
},
{
mode: 'production',
bail: true,
cache: getCompiler.isWebpack4() ? false : { type: 'memory' },
entry: `${__dirname}/fixtures/entry.js`,
output: {
path: `${__dirname}/dist-2`,
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-3`,
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-4`,
filename: '[name].js',
chunkFilename: '[id].[name].js',
},
optimization: {
minimize: false,
},
plugins,
},
]);

const multiStats = await compile(multiCompiler);
Expand Down
30 changes: 30 additions & 0 deletions test/__snapshots__/TerserPlugin.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,44 @@ Object {
}
`;

exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: assets 3`] = `
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 4`] = `
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 5`] = `
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: errors 3`] = `Array []`;

exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: errors 4`] = `Array []`;

exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: errors 5`] = `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 and with the same file: warnings 3`] = `Array []`;

exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: warnings 4`] = `Array []`;

exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: warnings 5`] = `Array []`;

exports[`TerserPlugin should work in multi compiler mode with the one plugin: assets 1`] = `
Object {
"main-1.js": "/******/ (function(modules) { // webpackBootstrap
Expand Down
30 changes: 30 additions & 0 deletions test/__snapshots__/TerserPlugin.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,44 @@ Object {
}
`;

exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: assets 3`] = `
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 4`] = `
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 5`] = `
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: errors 3`] = `Array []`;

exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: errors 4`] = `Array []`;

exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: errors 5`] = `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 and with the same file: warnings 3`] = `Array []`;

exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: warnings 4`] = `Array []`;

exports[`TerserPlugin should work in multi compiler mode with the one plugin and with the same file: warnings 5`] = `Array []`;

exports[`TerserPlugin should work in multi compiler mode with the one plugin: assets 1`] = `
Object {
"main-1.js": "/******/ (() => { // webpackBootstrap
Expand Down
Loading

0 comments on commit f6499af

Please sign in to comment.