From 671ef527e7a37c459df616ee74dc92e106e8245e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Berthommier?= Date: Sun, 24 Jan 2021 08:40:16 +0100 Subject: [PATCH] fix: update hooks for webpack 5 (#676) * fix: update hooks for webpack 5 * fix: fix webpack hook type --- packages/webpack-plugin/src/index.js | 37 +++++++++++++++++++++------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/packages/webpack-plugin/src/index.js b/packages/webpack-plugin/src/index.js index e1bca431..eab9a28e 100644 --- a/packages/webpack-plugin/src/index.js +++ b/packages/webpack-plugin/src/index.js @@ -2,6 +2,8 @@ const nodePath = require('path') const fs = require('fs') const makeDir = require('make-dir') +const name = '@loadable/webpack-plugin'; + class LoadablePlugin { constructor({ filename = 'loadable-stats.json', @@ -15,8 +17,8 @@ class LoadablePlugin { this.compiler = null } - handleEmit = (hookCompiler, callback) => { - const stats = hookCompiler.getStats().toJson({ + handleEmit = compilation => { + const stats = compilation.getStats().toJson({ hash: true, publicPath: true, assets: true, @@ -28,8 +30,12 @@ class LoadablePlugin { }) const result = JSON.stringify(stats, null, 2) + if (this.opts.writeToDisk) { + this.writeAssetsFile(result) + } + if (this.opts.outputAsset) { - hookCompiler.assets[this.opts.filename] = { + return { source() { return result }, @@ -39,11 +45,7 @@ class LoadablePlugin { } } - if (this.opts.writeToDisk) { - this.writeAssetsFile(result) - } - - callback() + return null; } /** @@ -81,8 +83,25 @@ class LoadablePlugin { compiler.options.output.chunkLoadingGlobal = '__LOADABLE_LOADED_CHUNKS__' } + const { webpack } = compiler; + if (this.opts.outputAsset || this.opts.writeToDisk) { - compiler.hooks.emit.tapAsync('@loadable/webpack-plugin', this.handleEmit) + if (!webpack) { // v4 + compiler.hooks.emit.tap(name, compilation => { + const asset = this.handleEmit(compilation) + if (asset) compilation.assets[this.opts.filename] = asset + }) + } else { // v5 + compiler.hooks.make.tap(name, compilation => { + compilation.hooks.processAssets.tap( + { name, stage: webpack.Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE }, + () => { + const asset = this.handleEmit(compilation) + if (asset) compilation.emitAsset(this.opts.filename, asset) + } + ) + }) + } } } }