Skip to content

Commit

Permalink
Fix sources unavailable in newer versions of Webpack 4
Browse files Browse the repository at this point in the history
  • Loading branch information
developit committed Mar 15, 2019
1 parent 938352e commit af3c70f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 18 additions & 11 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 9 additions & 9 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

exports[`size-plugin should display the size of a multiple output files 1`] = `
"
bundle.js ⏤ [1m[32m1.06 kB[39m[22m ([31m+1.06 kB[39m)
1.*****.chunk.js ⏤ [32m137 B[39m (+137 B)
2.*****.chunk.js ⏤ [32m102 B[39m (+102 B)
bundle.js ⏤ [1m[32m1.04 kB[39m[22m ([31m+1.04 kB[39m)
1.*****.chunk.js ⏤ [32m102 B[39m (+102 B)
2.*****.chunk.js ⏤ [32m137 B[39m (+137 B)
"
`;

Expand All @@ -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 ⏤ [1m[32m1.11 kB[39m[22m ([31m+1.11 kB[39m)
js-1.********************.js ⏤ [32m137 B[39m (+137 B)
js-2.********************.js ⏤ [32m102 B[39m (+102 B)
js-main.********************.js ⏤ [1m[32m1.09 kB[39m[22m ([31m+1.09 kB[39m)
js-1.********************.js ⏤ [32m102 B[39m (+102 B)
js-2.********************.js ⏤ [32m137 B[39m (+137 B)
"
`;

exports[`size-plugin should show size deltas for subsequent builds 1`] = `
"
bundle.js ⏤ [1m[32m7.22 kB[39m[22m ([31m+6.16 kB[39m)
1.*****.chunk.js ⏤ [32m303 B[39m (+166 B)
2.*****.chunk.js ⏤ [32m102 B[39m
bundle.js ⏤ [1m[32m7.2 kB[39m[22m ([31m+6.16 kB[39m)
1.*****.chunk.js ⏤ [32m102 B[39m
2.*****.chunk.js ⏤ [32m303 B[39m (+166 B)
"
`;

0 comments on commit af3c70f

Please sign in to comment.