From af3c70f469004a5b8647e5ce3dba27a142c983a6 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Fri, 15 Mar 2019 18:25:49 -0500 Subject: [PATCH] Fix sources unavailable in newer versions of Webpack 4 --- package.json | 2 +- src/index.mjs | 29 +++++++++++++++++---------- test/__snapshots__/index.test.js.snap | 18 ++++++++--------- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index c857f18..1a5247d 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "fs-extra": "^7.0.0", "jest": "^23.5.0", "microbundle": "^0.6.0", - "webpack": "^4.17.1" + "webpack": "^4.29.6" }, "dependencies": { "chalk": "^2.4.1", diff --git a/src/index.mjs b/src/index.mjs index 94480a5..24e9133 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -91,17 +91,23 @@ export default class SizePlugin { this.output = compiler.options.output; this.sizes = this.getSizes(outputPath); const afterEmit = (compilation, callback) => { - process.nextTick(() => { - this.outputSizes(compilation.assets).catch(console.error); - callback(); - }); + this.outputSizes(compilation.assets).then(output => { + if (output) { + process.nextTick(() => { + console.log('\n' + output); + }); + } + }).catch(console.error).then(callback); }; + // for webpack version > 4 - if (compiler.hooks && compiler.hooks.afterEmit) { - return compiler.hooks.afterEmit.tapAsync(NAME, afterEmit); + if (compiler.hooks && compiler.hooks.emit) { + compiler.hooks.emit.tapAsync(NAME, afterEmit); + } + else { + // for webpack version < 3 + compiler.plugin('after-emit', afterEmit); } - // for webpack version < 3 - return compiler.plugin('after-emit', afterEmit); } async outputSizes (assets) { @@ -140,9 +146,10 @@ export default class SizePlugin { } output += msg + sizeText + '\n'; } - if (output) { - console.log('\n' + output); - } + return output; + // if (output) { + // console.log('\n' + output); + // } } async getSizes (cwd) { diff --git a/test/__snapshots__/index.test.js.snap b/test/__snapshots__/index.test.js.snap index 2569fb3..36d2b18 100644 --- a/test/__snapshots__/index.test.js.snap +++ b/test/__snapshots__/index.test.js.snap @@ -2,9 +2,9 @@ exports[`size-plugin should display the size of a multiple output files 1`] = ` " - bundle.js ⏤ 1.06 kB (+1.06 kB) - 1.*****.chunk.js ⏤ 137 B (+137 B) - 2.*****.chunk.js ⏤ 102 B (+102 B) + bundle.js ⏤ 1.04 kB (+1.04 kB) + 1.*****.chunk.js ⏤ 102 B (+102 B) + 2.*****.chunk.js ⏤ 137 B (+137 B) " `; @@ -16,16 +16,16 @@ exports[`size-plugin should display the size of a single bundle file 1`] = ` exports[`size-plugin should respect output.filename / output.chunkFilename 1`] = ` " - js-main.********************.js ⏤ 1.11 kB (+1.11 kB) - js-1.********************.js ⏤ 137 B (+137 B) - js-2.********************.js ⏤ 102 B (+102 B) + js-main.********************.js ⏤ 1.09 kB (+1.09 kB) + js-1.********************.js ⏤ 102 B (+102 B) + js-2.********************.js ⏤ 137 B (+137 B) " `; exports[`size-plugin should show size deltas for subsequent builds 1`] = ` " - bundle.js ⏤ 7.22 kB (+6.16 kB) - 1.*****.chunk.js ⏤ 303 B (+166 B) - 2.*****.chunk.js ⏤ 102 B + bundle.js ⏤ 7.2 kB (+6.16 kB) + 1.*****.chunk.js ⏤ 102 B + 2.*****.chunk.js ⏤ 303 B (+166 B) " `;