Skip to content

Commit

Permalink
fix: skip double compression for child compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Aug 3, 2020
1 parent 718b1ce commit 37cc813
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/Webpack5Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class Cache {
async get(task) {
// eslint-disable-next-line no-param-reassign
task.cacheIdent =
task.cacheIdent || `${task.file}|${serialize(task.cacheKeys)}`;
task.cacheIdent || `${task.name}|${serialize(task.cacheKeys)}`;
// eslint-disable-next-line no-param-reassign
task.cacheETag =
task.cacheETag || this.cache.getLazyHashedEtag(task.assetSource);
Expand Down
95 changes: 64 additions & 31 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,6 @@ class TerserPlugin {
return `Terser Plugin: ${warningMessage}${locationMessage}`;
}

static removeQueryString(filename) {
let targetFilename = filename;

const queryStringIdx = targetFilename.indexOf('?');

if (queryStringIdx >= 0) {
targetFilename = targetFilename.substr(0, queryStringIdx);
}

return targetFilename;
}

static isWebpack4() {
return webpackVersion[0] === '4';
}
Expand All @@ -181,8 +169,48 @@ class TerserPlugin {
: Math.min(Number(parallel) || 0, cpus.length - 1);
}

*taskGenerator(compiler, compilation, allExtractedComments, file) {
const assetSource = compilation.assets[file];
// eslint-disable-next-line consistent-return
static getAsset(compilation, name) {
// New API
if (compilation.getAsset) {
return compilation.getAsset(name);
}

if (compilation.assets[name]) {
return { name, source: compilation.assets[name], info: {} };
}
}

static emitAsset(compilation, name, source, assetInfo) {
// New API
if (compilation.emitAsset) {
compilation.emitAsset(name, source, assetInfo);
}

// eslint-disable-next-line no-param-reassign
compilation.assets[name] = source;
}

static updateAsset(compilation, name, newSource, assetInfo) {
// New API
if (compilation.updateAsset) {
compilation.updateAsset(name, newSource, assetInfo);
}

// eslint-disable-next-line no-param-reassign
compilation.assets[name] = newSource;
}

*taskGenerator(compiler, compilation, allExtractedComments, name) {
const { info, source: assetSource } = TerserPlugin.getAsset(
compilation,
name
);

// Skip double minimize assets from child compilation
if (info.minimized) {
yield false;
}

let input;
let inputSourceMap;
Expand All @@ -200,7 +228,7 @@ class TerserPlugin {
inputSourceMap = map;

compilation.warnings.push(
new Error(`${file} contains invalid source map`)
new Error(`${name} contains invalid source map`)
);
}
}
Expand All @@ -221,7 +249,7 @@ class TerserPlugin {
this.options.extractComments.filename || '[file].LICENSE.txt[query]';

let query = '';
let filename = file;
let filename = name;

const querySplit = filename.indexOf('?');

Expand Down Expand Up @@ -259,7 +287,7 @@ class TerserPlugin {
compilation.errors.push(
TerserPlugin.buildError(
error,
file,
name,
sourceMap,
new RequestShortener(compiler.context)
)
Expand Down Expand Up @@ -290,7 +318,7 @@ class TerserPlugin {
if (map) {
outputSource = new SourceMapSource(
code,
file,
name,
map,
input,
inputSourceMap,
Expand All @@ -309,7 +337,7 @@ class TerserPlugin {
banner =
this.options.extractComments.banner ||
`For license information please see ${path
.relative(path.dirname(file), commentsFilename)
.relative(path.dirname(name), commentsFilename)
.replace(/\\/g, '/')}`;

if (typeof banner === 'function') {
Expand Down Expand Up @@ -341,10 +369,13 @@ class TerserPlugin {
});

// Extracted comments from child compilation
const previousExtractedComments = compilation.assets[commentsFilename];
const previousExtractedComments = TerserPlugin.getAsset(
compilation,
commentsFilename
);

if (previousExtractedComments) {
const previousExtractedCommentsSource = previousExtractedComments.source();
const previousExtractedCommentsSource = previousExtractedComments.source.source();

// Restore original comments and re-add them
previousExtractedCommentsSource
Expand All @@ -356,16 +387,17 @@ class TerserPlugin {
}
}

// Updating assets
// eslint-disable-next-line no-param-reassign
compilation.assets[file] = outputSource;
TerserPlugin.updateAsset(compilation, name, outputSource, {
...info,
minimized: true,
});

// Handling warnings
if (warnings && warnings.length > 0) {
warnings.forEach((warning) => {
const builtWarning = TerserPlugin.buildWarning(
warning,
file,
name,
sourceMap,
new RequestShortener(compiler.context),
this.options.warningsFilter
Expand All @@ -379,7 +411,7 @@ class TerserPlugin {
};

const task = {
file,
name,
input,
inputSourceMap,
commentsFilename,
Expand Down Expand Up @@ -410,11 +442,11 @@ class TerserPlugin {
'terser-webpack-plugin': require('../package.json').version,
'terser-webpack-plugin-options': this.options,
nodeVersion: process.version,
filename: file,
name,
contentHash: digest.substr(0, hashDigestLength),
};

task.cacheKeys = this.options.cacheKeys(defaultCacheKeys, file);
task.cacheKeys = this.options.cacheKeys(defaultCacheKeys, name);
}
} else {
// For webpack@5 cache
Expand Down Expand Up @@ -619,9 +651,10 @@ class TerserPlugin {
.sort()
.join('\n\n');

// eslint-disable-next-line no-param-reassign
compilation.assets[commentsFilename] = new RawSource(
`${extractedComments}\n`
TerserPlugin.emitAsset(
compilation,
commentsFilename,
new RawSource(`${extractedComments}\n`)
);
});

Expand Down
6 changes: 3 additions & 3 deletions src/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ const buildComments = (options, terserOptions, extractedComments) => {
};

const minify = (options) => {
const { file, input, inputSourceMap, minify: minifyFn } = options;
const { name, input, inputSourceMap, minify: minifyFn } = options;

if (minifyFn) {
return minifyFn({ [file]: input }, inputSourceMap);
return minifyFn({ [name]: input }, inputSourceMap);
}

// Copy terser options
Expand All @@ -169,7 +169,7 @@ const minify = (options) => {
);

const { error, map, code, warnings } = terserMinify(
{ [file]: input },
{ [name]: input },
terserOptions
);

Expand Down
4 changes: 2 additions & 2 deletions test/worker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('worker', () => {

it('should match snapshot with options.inputSourceMap', () => {
const options = {
file: 'test6.js',
name: 'test6.js',
input: 'function foo(x) { if (x) { return bar(); not_called1(); } }',
inputSourceMap: {
version: 3,
Expand All @@ -99,6 +99,6 @@ describe('worker', () => {
};
const workerResult = transform(serialize(options));

expect(workerResult).toMatchSnapshot(options.file);
expect(workerResult).toMatchSnapshot(options.name);
});
});

0 comments on commit 37cc813

Please sign in to comment.