Skip to content

Commit 7ca675e

Browse files
feat: run optimize against assets added later by plugins for webpack@5
1 parent 2afc01b commit 7ca675e

File tree

5 files changed

+105
-8
lines changed

5 files changed

+105
-8
lines changed

src/index.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,25 @@ class CssMinimizerPlugin {
192192
async optimize(compiler, compilation, assets, CacheEngine, weakCache) {
193193
const assetNames = Object.keys(
194194
typeof assets === 'undefined' ? compilation.assets : assets
195-
).filter((assetName) =>
196-
ModuleFilenameHelpers.matchObject.bind(
197-
// eslint-disable-next-line no-undefined
198-
undefined,
199-
this.options
200-
)(assetName)
201-
);
195+
).filter((assetName) => {
196+
if (
197+
!ModuleFilenameHelpers.matchObject.bind(
198+
// eslint-disable-next-line no-undefined
199+
undefined,
200+
this.options
201+
)(assetName)
202+
) {
203+
return false;
204+
}
205+
206+
const { info } = CssMinimizerPlugin.getAsset(compilation, assetName);
207+
208+
if (info.minimized) {
209+
return false;
210+
}
211+
212+
return true;
213+
});
202214

203215
if (assetNames.length === 0) {
204216
return Promise.resolve();
@@ -467,6 +479,7 @@ class CssMinimizerPlugin {
467479
{
468480
name: pluginName,
469481
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
482+
additionalAssets: true,
470483
},
471484
(assets) => this.optimize(compiler, compilation, assets, CacheEngine)
472485
);

test/CssMinimizerPlugin.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
readAsset,
1818
removeCache,
1919
ModifyExistingAsset,
20+
EmitNewAsset,
2021
} from './helpers';
2122

2223
describe('CssMinimizerPlugin', () => {
@@ -1104,4 +1105,41 @@ describe('CssMinimizerPlugin', () => {
11041105
resolve();
11051106
});
11061107
});
1108+
1109+
if (!getCompiler.isWebpack4()) {
1110+
it('should run plugin against assets added later by plugins', async () => {
1111+
const compiler = getCompiler({
1112+
output: {
1113+
pathinfo: false,
1114+
path: path.resolve(__dirname, 'dist'),
1115+
filename: '[name].js',
1116+
chunkFilename: '[id].[name].js',
1117+
},
1118+
entry: {
1119+
entry: `${__dirname}/fixtures/test/foo.css`,
1120+
},
1121+
module: {
1122+
rules: [
1123+
{
1124+
test: /.s?css$/i,
1125+
use: ['css-loader'],
1126+
},
1127+
],
1128+
},
1129+
});
1130+
new CssMinimizerPlugin({
1131+
minimizerOptions: {
1132+
preset: ['default', { discardEmpty: false }],
1133+
},
1134+
}).apply(compiler);
1135+
1136+
new EmitNewAsset({ name: 'newFile.css' }).apply(compiler);
1137+
1138+
const stats = await compile(compiler);
1139+
1140+
expect(readAssets(compiler, stats, /\.css$/)).toMatchSnapshot('assets');
1141+
expect(getErrors(stats)).toMatchSnapshot('errors');
1142+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
1143+
});
1144+
}
11071145
});

test/__snapshots__/CssMinimizerPlugin.test.js.snap.webpack5

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,21 @@ exports[`CssMinimizerPlugin should respect the hash options #1: errors 1`] = `Ar
5959

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

62+
exports[`CssMinimizerPlugin should run plugin against assets added later by plugins: assets 1`] = `
63+
Object {
64+
"newFile.css": ".a{display:block;color:coral}",
65+
}
66+
`;
67+
68+
exports[`CssMinimizerPlugin should run plugin against assets added later by plugins: errors 1`] = `Array []`;
69+
70+
exports[`CssMinimizerPlugin should run plugin against assets added later by plugins: warnings 1`] = `Array []`;
71+
6272
exports[`CssMinimizerPlugin should throw error from postcss: error 1`] = `
6373
Array [
6474
"Error: foo.css from Css Minimizer
75+
error-plugin: /foo.css:2:3: Postcss error [foo.css:2,3]",
76+
"Error: foo.css from Css Minimizer
6577
error-plugin: /foo.css:2:3: Postcss error [foo.css:2,3]",
6678
]
6779
`;
@@ -94,7 +106,7 @@ exports[`CssMinimizerPlugin should work and do not use memory cache when the "ca
94106

95107
exports[`CssMinimizerPlugin should work and generate real content hash: assets 1`] = `
96108
Object {
97-
"entry.19e4764f9c1d9fe130e2.2b805513ea22ef0b0e87.569cdce223d5e9f199ef.css": "body{color:red}a{color:#00f}",
109+
"entry.19e4764f9c1d9fe130e2.4c83f86dfff978a8623b.b0f9ad463b6a0480ea34.css": "body{color:red}a{color:#00f}",
98110
}
99111
`;
100112

test/helpers/EmitNewAsset.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export default class EmitNewAsset {
2+
constructor(options = {}) {
3+
this.options = options;
4+
}
5+
6+
apply(compiler) {
7+
const pluginName = this.constructor.name;
8+
9+
const { RawSource } = compiler.webpack.sources;
10+
11+
compiler.hooks.compilation.tap(pluginName, (compilation) => {
12+
compilation.hooks.processAssets.tap(
13+
{
14+
name: pluginName,
15+
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_REPORT,
16+
},
17+
() => {
18+
// eslint-disable-next-line no-param-reassign
19+
compilation.emitAsset(
20+
this.options.name,
21+
new RawSource(`
22+
.a {
23+
display: block;
24+
color: coral;
25+
}
26+
`)
27+
);
28+
}
29+
);
30+
});
31+
}
32+
}

test/helpers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import getCompiler from './getCompiler';
33
import readAsset from './readAsset';
44
import readAssets from './readAssets';
55
import ModifyExistingAsset from './ModifyExistingAsset';
6+
import EmitNewAsset from './EmitNewAsset';
67
import removeCache from './removeCache';
78
import getErrors from './getErrors';
89
import getWarnings from './getWarnings';
@@ -14,6 +15,7 @@ export {
1415
readAsset,
1516
readAssets,
1617
ModifyExistingAsset,
18+
EmitNewAsset,
1719
removeCache,
1820
getErrors,
1921
getWarnings,

0 commit comments

Comments
 (0)