Skip to content

Commit

Permalink
feat: show minimized assets in stats for webpack@5 (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Aug 3, 2020
1 parent 37cc813 commit d59eae2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,15 @@ class TerserPlugin {
plugin,
optimizeFn.bind(this, compilation)
);

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

it('should work and show minimized assets in stats', async () => {
const compiler = getCompiler();

new TerserPlugin().apply(compiler);

const stats = await compile(compiler);

expect(stats.toString().indexOf('[minimized]') !== -1).toBe(
!getCompiler.isWebpack4()
);
expect(readsAssets(compiler, stats)).toMatchSnapshot('assets');
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});
});
10 changes: 10 additions & 0 deletions test/__snapshots__/TerserPlugin.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ Unexpected token name «Object», expected punc «,» [main.js:1,8]",

exports[`TerserPlugin should work and respect "terser" errors (the "parallel" option is "true"): warnings 1`] = `Array []`;

exports[`TerserPlugin should work and show minimized assets in stats: assets 1`] = `
Object {
"main.js": "!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,\\"a\\",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p=\\"\\",n(n.s=0)}([function(e,t){e.exports=function(){console.log(7)}}]);",
}
`;

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

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

exports[`TerserPlugin should work as a minimizer: assets 1`] = `
Object {
"main.js": "!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,\\"a\\",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p=\\"\\",n(n.s=0)}([function(e,t){e.exports=function(){console.log(7)}}]);",
Expand Down
10 changes: 10 additions & 0 deletions test/__snapshots__/TerserPlugin.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ Unexpected token name «Object», expected punc «,» [main.js:1,8]",

exports[`TerserPlugin should work and respect "terser" errors (the "parallel" option is "true"): warnings 1`] = `Array []`;

exports[`TerserPlugin should work and show minimized assets in stats: assets 1`] = `
Object {
"main.js": "(()=>{var r={791:r=>{r.exports=function(){console.log(7)}}},o={};!function t(e){if(o[e])return o[e].exports;var n=o[e]={exports:{}};return r[e](n,n.exports,t),n.exports}(791)})();",
}
`;

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

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

exports[`TerserPlugin should work as a minimizer: assets 1`] = `
Object {
"main.js": "(()=>{var r={791:r=>{r.exports=function(){console.log(7)}}},o={};!function t(e){if(o[e])return o[e].exports;var n=o[e]={exports:{}};return r[e](n,n.exports,t),n.exports}(791)})();",
Expand Down
6 changes: 2 additions & 4 deletions test/helpers/BrokenCodePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ export default class BrokenCodePlugin {
apply(compiler) {
const plugin = { name: this.constructor.name };

compiler.hooks.compilation.tap(plugin, (compilation) => {
compilation.hooks.additionalChunkAssets.tap(plugin, () => {
compilation.additionalChunkAssets.push('broken.js');

compiler.hooks.thisCompilation.tap(plugin, (compilation) => {
compilation.hooks.additionalAssets.tap(plugin, () => {
// eslint-disable-next-line no-param-reassign
compilation.assets['broken.js'] = new RawSource('`Broken===');
});
Expand Down

0 comments on commit d59eae2

Please sign in to comment.