Skip to content

Commit

Permalink
Switch to using the "done" hook display stats as late as possible (fixes
Browse files Browse the repository at this point in the history
 #4)
  • Loading branch information
developit committed Jan 29, 2021
1 parent dd45ab5 commit 20e5122
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
18 changes: 8 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,32 +152,30 @@ export default class SizePlugin {
}
return this.getSizes(outputPath);
}

/** @param {import('webpack').Compiler} compiler */
async apply(compiler) {
const outputPath = compiler.options.output.path;
this.output = compiler.options.output;
this.sizes = this.load(outputPath);
this.mode = this.options.mode || compiler.options.mode;

const afterEmit = (compilation, callback) => {
this.outputSizes(compilation.assets)
const done = (stats, callback) => {
this.outputSizes(stats.compilation.assets)
.then(output => {
if (output) {
process.nextTick(() => {
console.log('\n' + output);
});
}
if (output) console.log('\n' + output);
})
.catch(console.error)
.then(callback);
};

// for webpack version > 4
if (compiler.hooks && compiler.hooks.emit) {
compiler.hooks.emit.tapAsync(NAME, afterEmit);
if (compiler.hooks && compiler.hooks.done) {
compiler.hooks.done.tapAsync(NAME, done);
}
else {
// for webpack version < 3
compiler.plugin('after-emit', afterEmit);
compiler.plugin('done', done);
}
}

Expand Down
10 changes: 4 additions & 6 deletions test/_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ export function compile(entry, configDecorator) {
config = configDecorator(config) || config;
}
webpack(config, (err, stats) => {
setTimeout(() => {
if (err) return reject(err);
const info = stats.toJson();
if (stats.hasErrors()) return reject(info.errors.join('\n'));
resolve(info);
}, 10);
if (err) return reject(err);
const info = stats.toJson();
if (stats.hasErrors()) return reject(info.errors.join('\n'));
resolve(info);
});
});
}
16 changes: 8 additions & 8 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ async function clearDist () {
beforeAll(clearDist);
afterAll(clearDist);

let consoleLog;
const consoleLog = jest.spyOn(console, 'log');
beforeEach(() => {
consoleLog = jest.spyOn(console, 'log');
consoleLog.mockReset();
});

afterEach(() => {
afterAll(() => {
consoleLog.mockRestore();
});

Expand All @@ -53,11 +53,11 @@ describe('size-plugin', () => {

it('should display the size of a multiple output files', async () => {
const info = await compile('fixtures/splits/index.js', withSizePlugin);
expect(info.assets).toHaveLength(3);
expect(info.assets).toHaveLength(3);

expect(consoleLog).toHaveBeenCalled();
expect(consoleLog).toHaveBeenCalled();

expect(consoleLog.mock.calls[0][0]).toMatchSnapshot();
expect(consoleLog.mock.calls[0][0]).toMatchSnapshot();
});

it('should show size deltas for subsequent builds', async () => {
Expand All @@ -77,12 +77,12 @@ describe('size-plugin', () => {
config.output.filename = 'js-[name].[contenthash].js';
delete config.output.chunkFilename;
});
expect(info.assets).toHaveLength(3);
expect(info.assets).toHaveLength(3);

expect(consoleLog).toHaveBeenCalled();

expect(consoleLog.mock.calls[0][0]).toMatch(/js-2\.\*{20}\.js/);

expect(consoleLog.mock.calls[0][0]).toMatchSnapshot();
expect(consoleLog.mock.calls[0][0]).toMatchSnapshot();
});
});

0 comments on commit 20e5122

Please sign in to comment.