Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: skip double compression for child compilation
  • Loading branch information
cap-Bernardito committed Aug 4, 2020
commit 883c7586c466a15ab861c3a6dfaeaffca6044f5d
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
55 changes: 43 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,38 @@ class CssMinimizerPlugin {
: Math.min(Number(parallel) || 0, cpus.length - 1);
}

*taskGenerator(compiler, compilation, 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 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, name) {
const { info, source: assetSource } = CssMinimizerPlugin.getAsset(
compilation,
name
);

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

let input;
let inputSourceMap;
Expand All @@ -190,7 +220,7 @@ class CssMinimizerPlugin {
inputSourceMap = map;

compilation.warnings.push(
new Error(`${file} contains invalid source map`)
new Error(`${name} contains invalid source map`)
);
}
}
Expand Down Expand Up @@ -222,7 +252,7 @@ class CssMinimizerPlugin {
compilation.errors.push(
CssMinimizerPlugin.buildError(
error,
file,
name,
sourceMap,
new RequestShortener(compiler.context)
)
Expand All @@ -236,7 +266,7 @@ class CssMinimizerPlugin {
if (map) {
outputSource = new SourceMapSource(
code,
file,
name,
map,
input,
inputSourceMap,
Expand All @@ -246,16 +276,17 @@ class CssMinimizerPlugin {
outputSource = new RawSource(code);
}

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

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

const task = {
file,
name,
input,
inputSourceMap,
map: this.options.sourceMap,
Expand Down Expand Up @@ -299,11 +330,11 @@ class CssMinimizerPlugin {
'css-minimizer-webpack-plugin': require('../package.json').version,
'css-minimizer-webpack-plugin-options': this.options,
nodeVersion: process.version,
filename: file,
filename: 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
4 changes: 2 additions & 2 deletions src/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ function warningsToString(warnings) {

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

const postcssOptions = { to: file, from: file };
const postcssOptions = { to: name, from: name };

if (minifyFn) {
const result = await minifyFn(
Expand Down
10 changes: 5 additions & 5 deletions test/worker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { normalizeErrors } from './helpers';
describe('worker', () => {
it('should minify css', async () => {
const options = {
file: 'entry.css',
name: 'entry.css',
input: '.foo{color:red;}\n.bar{color:coral;}',
inputSourceMap: {
version: 3,
Expand All @@ -27,7 +27,7 @@ describe('worker', () => {

it('should work inputSourceMap as prev', async () => {
const options = {
file: 'entry.css',
name: 'entry.css',
input: '.foo{color:red;}\n.bar{color:coral;}',
minimizerOptions: { discardComments: false },
inputSourceMap: {
Expand All @@ -47,7 +47,7 @@ describe('worker', () => {

it('should work options.minify function', async () => {
const options = {
file: 'entry.css',
name: 'entry.css',
input: '.foo{color:red;}\n.bar{color:coral;}',
minimizerOptions: { discardComments: false },
minify: () => {
Expand All @@ -62,7 +62,7 @@ describe('worker', () => {

it('should emit error', async () => {
const options = {
file: 'entry.css',
name: 'entry.css',
input: false,
};

Expand All @@ -79,7 +79,7 @@ describe('worker', () => {

it('should emit minimizer error', async () => {
const options = {
file: 'entry.css',
name: 'entry.css',
input: false,
minify: () => {
return { error: new Error('css minimizer error') };
Expand Down