Skip to content

Commit

Permalink
feat: show minimized assets in stats for webpack@5 (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito authored Aug 4, 2020
1 parent ffc71c2 commit cb038b9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,17 @@ class CssMinimizerPlugin {
plugin,
optimizeFn.bind(this, compilation)
);

compilation.hooks.statsPrinter.tap(plugin, (stats) => {
stats.hooks.print
.for('asset.info.minimized')
.tap(
'css-minimizer-webpack-plugin',
(minimized, { green, formatFlag }) =>
// eslint-disable-next-line no-undefined
minimized ? green(formatFlag('minimized')) : undefined
);
});
}
});
}
Expand Down
19 changes: 19 additions & 0 deletions test/CssMinimizerPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,23 @@ describe('CssMinimizerPlugin', () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});

it('should work and show minimized assets in stats', async () => {
const compiler = getCompiler({
entry: {
foo: `${__dirname}/fixtures/entry.js`,
},
});

new CssMinimizerPlugin().apply(compiler);

const stats = await compile(compiler);

expect(stats.toString().indexOf('[minimized]') !== -1).toBe(
!getCompiler.isWebpack4()
);
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.webpack4
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 work and show minimized assets in stats: assets 1`] = `
Object {
"foo.css": "body{color:red}a{color:#00f}",
}
`;

exports[`CssMinimizerPlugin should work and show minimized assets in stats: errors 1`] = `Array []`;

exports[`CssMinimizerPlugin should work and show minimized assets in stats: warnings 1`] = `Array []`;

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\\":\\"\\"}"`;

exports[`CssMinimizerPlugin should work with assets using querystring: entry.css?v=test 1`] = `
Expand Down
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 work and show minimized assets in stats: assets 1`] = `
Object {
"foo.css": "body{color:red}a{color:#00f}",
}
`;

exports[`CssMinimizerPlugin should work and show minimized assets in stats: errors 1`] = `Array []`;

exports[`CssMinimizerPlugin should work and show minimized assets in stats: warnings 1`] = `Array []`;

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\\":\\"\\"}"`;

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

0 comments on commit cb038b9

Please sign in to comment.