diff --git a/src/index.js b/src/index.js index a395078..d311f3d 100644 --- a/src/index.js +++ b/src/index.js @@ -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); } } diff --git a/test/_helpers.js b/test/_helpers.js index 24d89bf..c2be30d 100644 --- a/test/_helpers.js +++ b/test/_helpers.js @@ -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); }); }); } diff --git a/test/index.test.js b/test/index.test.js index e1d7d2b..3482cd6 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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(); }); @@ -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 () => { @@ -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(); }); });