From e4e9aa5ce0d90592369de31437618349bb4b1e49 Mon Sep 17 00:00:00 2001 From: wood1986 <5212215+wood1986@users.noreply.github.com> Date: Mon, 11 Oct 2021 10:24:35 -0700 Subject: [PATCH] fix: plugin uses infrastructureLogger and outputs the report.html to compiler.outputFileSystem --- src/BundleAnalyzerPlugin.js | 15 ++++++++++----- src/bin/analyzer.js | 7 +++++-- src/viewer.js | 6 +++--- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/BundleAnalyzerPlugin.js b/src/BundleAnalyzerPlugin.js index 9e5c2842..a36db05a 100644 --- a/src/BundleAnalyzerPlugin.js +++ b/src/BundleAnalyzerPlugin.js @@ -6,6 +6,7 @@ const Logger = require('./Logger'); const viewer = require('./viewer'); const utils = require('./utils'); const {writeStats} = require('./statsUtils'); +const PLUGIN_NAME = 'webpack-bundle-analyzer'; class BundleAnalyzerPlugin { constructor(opts = {}) { @@ -28,13 +29,15 @@ class BundleAnalyzerPlugin { }; this.server = null; - this.logger = new Logger(this.opts.logLevel); } apply(compiler) { this.compiler = compiler; + this.logger = compiler?.getInfrastructureLogger(PLUGIN_NAME) || require('webpack/logging/runtime').getLogger(PLUGIN_NAME); + const done = (stats, callback) => { + this.fs = compiler.outputFileSystem; callback = callback || (() => {}); const actions = []; @@ -72,7 +75,7 @@ class BundleAnalyzerPlugin { }; if (compiler.hooks) { - compiler.hooks.done.tapAsync('webpack-bundle-analyzer', done); + compiler.hooks.done.tapAsync(PLUGIN_NAME, done); } else { compiler.plugin('done', done); } @@ -80,7 +83,7 @@ class BundleAnalyzerPlugin { async generateStatsFile(stats) { const statsFilepath = path.resolve(this.compiler.outputPath, this.opts.statsFilename); - await fs.promises.mkdir(path.dirname(statsFilepath), {recursive: true}); + await this.fs.promises.mkdir(path.dirname(statsFilepath), {recursive: true}); try { await writeStats(stats, statsFilepath); @@ -117,7 +120,8 @@ class BundleAnalyzerPlugin { reportFilename: path.resolve(this.compiler.outputPath, this.opts.reportFilename || 'report.json'), bundleDir: this.getBundleDirFromCompiler(), logger: this.logger, - excludeAssets: this.opts.excludeAssets + excludeAssets: this.opts.excludeAssets, + fs: this.fs }); } @@ -129,7 +133,8 @@ class BundleAnalyzerPlugin { bundleDir: this.getBundleDirFromCompiler(), logger: this.logger, defaultSizes: this.opts.defaultSizes, - excludeAssets: this.opts.excludeAssets + excludeAssets: this.opts.excludeAssets, + fs: this.fs }); } diff --git a/src/bin/analyzer.js b/src/bin/analyzer.js index 3a119732..43138fe1 100755 --- a/src/bin/analyzer.js +++ b/src/bin/analyzer.js @@ -4,6 +4,7 @@ const {resolve, dirname} = require('path'); const commander = require('commander'); const {magenta} = require('chalk'); +const fs = require('fs'); const analyzer = require('../analyzer'); const viewer = require('../viewer'); @@ -138,14 +139,16 @@ if (mode === 'server') { defaultSizes, bundleDir, excludeAssets, - logger: new Logger(logLevel) + logger: new Logger(logLevel), + fs }); } else if (mode === 'json') { viewer.generateJSONReport(bundleStats, { reportFilename: resolve(reportFilename || 'report.json'), bundleDir, excludeAssets, - logger: new Logger(logLevel) + logger: new Logger(logLevel), + fs }); } diff --git a/src/viewer.js b/src/viewer.js index 87304f54..811118c3 100644 --- a/src/viewer.js +++ b/src/viewer.js @@ -1,5 +1,4 @@ const path = require('path'); -const fs = require('fs'); const http = require('http'); const WebSocket = require('ws'); @@ -129,7 +128,8 @@ async function generateReport(bundleStats, opts) { bundleDir = null, logger = new Logger(), defaultSizes = 'parsed', - excludeAssets = null + excludeAssets = null, + fs } = opts || {}; const chartData = getChartData({logger, excludeAssets}, bundleStats, bundleDir); @@ -156,7 +156,7 @@ async function generateReport(bundleStats, opts) { } async function generateJSONReport(bundleStats, opts) { - const {reportFilename, bundleDir = null, logger = new Logger(), excludeAssets = null} = opts || {}; + const {reportFilename, bundleDir = null, logger = new Logger(), excludeAssets = null, fs} = opts || {}; const chartData = getChartData({logger, excludeAssets}, bundleStats, bundleDir);