Skip to content

Commit cb038b9

Browse files
feat: show minimized assets in stats for webpack@5 (#19)
1 parent ffc71c2 commit cb038b9

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

src/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,17 @@ class CssMinimizerPlugin {
526526
plugin,
527527
optimizeFn.bind(this, compilation)
528528
);
529+
530+
compilation.hooks.statsPrinter.tap(plugin, (stats) => {
531+
stats.hooks.print
532+
.for('asset.info.minimized')
533+
.tap(
534+
'css-minimizer-webpack-plugin',
535+
(minimized, { green, formatFlag }) =>
536+
// eslint-disable-next-line no-undefined
537+
minimized ? green(formatFlag('minimized')) : undefined
538+
);
539+
});
529540
}
530541
});
531542
}

test/CssMinimizerPlugin.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -371,4 +371,23 @@ describe('CssMinimizerPlugin', () => {
371371
expect(getErrors(stats)).toMatchSnapshot('errors');
372372
expect(getWarnings(stats)).toMatchSnapshot('warnings');
373373
});
374+
375+
it('should work and show minimized assets in stats', async () => {
376+
const compiler = getCompiler({
377+
entry: {
378+
foo: `${__dirname}/fixtures/entry.js`,
379+
},
380+
});
381+
382+
new CssMinimizerPlugin().apply(compiler);
383+
384+
const stats = await compile(compiler);
385+
386+
expect(stats.toString().indexOf('[minimized]') !== -1).toBe(
387+
!getCompiler.isWebpack4()
388+
);
389+
expect(readAssets(compiler, stats, '.css')).toMatchSnapshot('assets');
390+
expect(getErrors(stats)).toMatchSnapshot('errors');
391+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
392+
});
374393
});

test/__snapshots__/CssMinimizerPlugin.test.js.snap.webpack4

+10
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 work and show minimized assets in stats: assets 1`] = `
63+
Object {
64+
"foo.css": "body{color:red}a{color:#00f}",
65+
}
66+
`;
67+
68+
exports[`CssMinimizerPlugin should work and show minimized assets in stats: errors 1`] = `Array []`;
69+
70+
exports[`CssMinimizerPlugin should work and show minimized assets in stats: warnings 1`] = `Array []`;
71+
6272
exports[`CssMinimizerPlugin should work with assets using querystring: entry.css.map?v=test 1`] = `"{\\"version\\":3,\\"sources\\": [replaced for tests], \\"names\\":[],\\"mappings\\":\\"AAAA,KACE,SACF,CACA,EACE,UACF\\",\\"file\\":\\"entry.css?v=test\\",\\"sourcesContent\\":[\\"body {\\\\n color: red;\\\\n}\\\\na {\\\\n color: blue;\\\\n}\\"],\\"sourceRoot\\":\\"\\"}"`;
6373

6474
exports[`CssMinimizerPlugin should work with assets using querystring: entry.css?v=test 1`] = `

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

+10
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 work and show minimized assets in stats: assets 1`] = `
63+
Object {
64+
"foo.css": "body{color:red}a{color:#00f}",
65+
}
66+
`;
67+
68+
exports[`CssMinimizerPlugin should work and show minimized assets in stats: errors 1`] = `Array []`;
69+
70+
exports[`CssMinimizerPlugin should work and show minimized assets in stats: warnings 1`] = `Array []`;
71+
6272
exports[`CssMinimizerPlugin should work with assets using querystring: entry.css.map?v=test 1`] = `"{\\"version\\":3,\\"sources\\": [replaced for tests], \\"names\\":[],\\"mappings\\":\\"AAAA,KACE,SACF,CACA,EACE,UACF\\",\\"file\\":\\"entry.css?v=test\\",\\"sourcesContent\\":[\\"body {\\\\n color: red;\\\\n}\\\\na {\\\\n color: blue;\\\\n}\\"],\\"sourceRoot\\":\\"\\"}"`;
6373

6474
exports[`CssMinimizerPlugin should work with assets using querystring: entry.css?v=test 1`] = `

0 commit comments

Comments
 (0)