Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 20 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,25 @@ class CssMinimizerPlugin {
async optimize(compiler, compilation, assets, CacheEngine, weakCache) {
const assetNames = Object.keys(
typeof assets === 'undefined' ? compilation.assets : assets
).filter((assetName) =>
ModuleFilenameHelpers.matchObject.bind(
// eslint-disable-next-line no-undefined
undefined,
this.options
)(assetName)
);
).filter((assetName) => {
if (
!ModuleFilenameHelpers.matchObject.bind(
// eslint-disable-next-line no-undefined
undefined,
this.options
)(assetName)
) {
return false;
}

const { info } = CssMinimizerPlugin.getAsset(compilation, assetName);

if (info.minimized) {
return false;
}

return true;
});

if (assetNames.length === 0) {
return Promise.resolve();
Expand Down Expand Up @@ -463,6 +475,7 @@ class CssMinimizerPlugin {
{
name: pluginName,
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
additionalAssets: true,
},
(assets) => this.optimize(compiler, compilation, assets, CacheEngine)
);
Expand Down
38 changes: 38 additions & 0 deletions test/CssMinimizerPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
readAsset,
removeCache,
ModifyExistingAsset,
EmitNewAsset,
} from './helpers';

describe('CssMinimizerPlugin', () => {
Expand Down Expand Up @@ -1102,4 +1103,41 @@ describe('CssMinimizerPlugin', () => {
resolve();
});
});

if (!getCompiler.isWebpack4()) {
it('should run plugin against assets added later by plugins', async () => {
const compiler = getCompiler({
output: {
pathinfo: false,
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
chunkFilename: '[id].[name].js',
},
entry: {
entry: `${__dirname}/fixtures/test/foo.css`,
},
module: {
rules: [
{
test: /.s?css$/i,
use: ['css-loader'],
},
],
},
});

new CssMinimizerPlugin({
minimizerOptions: {
preset: ['default', { discardEmpty: false }],
},
}).apply(compiler);
new EmitNewAsset({ name: 'newFile.css' }).apply(compiler);

const stats = await compile(compiler);

expect(readAssets(compiler, stats, /\.css$/)).toMatchSnapshot('assets');
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});
}
});
10 changes: 10 additions & 0 deletions test/__snapshots__/CssMinimizerPlugin.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ exports[`CssMinimizerPlugin should respect the hash options #1: errors 1`] = `Ar

exports[`CssMinimizerPlugin should respect the hash options #1: warnings 1`] = `Array []`;

exports[`CssMinimizerPlugin should run plugin against assets added later by plugins: assets 1`] = `
Object {
"newFile.css": ".a{display:block;color:coral}",
}
`;

exports[`CssMinimizerPlugin should run plugin against assets added later by plugins: errors 1`] = `Array []`;

exports[`CssMinimizerPlugin should run plugin against assets added later by plugins: warnings 1`] = `Array []`;

exports[`CssMinimizerPlugin should throw error from postcss: error 1`] = `
Array [
"Error: foo.css from Css Minimizer
Expand Down
32 changes: 32 additions & 0 deletions test/helpers/EmitNewAsset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export default class EmitNewAsset {
constructor(options = {}) {
this.options = options;
}

apply(compiler) {
const pluginName = this.constructor.name;

const { RawSource } = compiler.webpack.sources;

compiler.hooks.compilation.tap(pluginName, (compilation) => {
compilation.hooks.processAssets.tap(
{
name: pluginName,
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_REPORT,
},
() => {
// eslint-disable-next-line no-param-reassign
compilation.emitAsset(
this.options.name,
new RawSource(`
.a {
display: block;
color: coral;
}
`)
);
}
);
});
}
}
2 changes: 2 additions & 0 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import getCompiler from './getCompiler';
import readAsset from './readAsset';
import readAssets from './readAssets';
import ModifyExistingAsset from './ModifyExistingAsset';
import EmitNewAsset from './EmitNewAsset';
import removeCache from './removeCache';
import getErrors from './getErrors';
import getWarnings from './getWarnings';
Expand All @@ -14,6 +15,7 @@ export {
readAsset,
readAssets,
ModifyExistingAsset,
EmitNewAsset,
removeCache,
getErrors,
getWarnings,
Expand Down