Skip to content

Commit bdb3f52

Browse files
feat: optimize CSS assets added later by plugins (webpack@5 only) (#47)
1 parent 66c054f commit bdb3f52

File tree

5 files changed

+102
-7
lines changed

5 files changed

+102
-7
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();
@@ -463,6 +475,7 @@ class CssMinimizerPlugin {
463475
{
464476
name: pluginName,
465477
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
478+
additionalAssets: true,
466479
},
467480
(assets) => this.optimize(compiler, compilation, assets, CacheEngine)
468481
);

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

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ 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

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)