Skip to content

Commit e4e9aa5

Browse files
committed
fix: plugin uses infrastructureLogger and outputs the report.html to compiler.outputFileSystem
1 parent 8dce252 commit e4e9aa5

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

src/BundleAnalyzerPlugin.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const Logger = require('./Logger');
66
const viewer = require('./viewer');
77
const utils = require('./utils');
88
const {writeStats} = require('./statsUtils');
9+
const PLUGIN_NAME = 'webpack-bundle-analyzer';
910

1011
class BundleAnalyzerPlugin {
1112
constructor(opts = {}) {
@@ -28,13 +29,15 @@ class BundleAnalyzerPlugin {
2829
};
2930

3031
this.server = null;
31-
this.logger = new Logger(this.opts.logLevel);
3232
}
3333

3434
apply(compiler) {
3535
this.compiler = compiler;
3636

37+
this.logger = compiler?.getInfrastructureLogger(PLUGIN_NAME) || require('webpack/logging/runtime').getLogger(PLUGIN_NAME);
38+
3739
const done = (stats, callback) => {
40+
this.fs = compiler.outputFileSystem;
3841
callback = callback || (() => {});
3942

4043
const actions = [];
@@ -72,15 +75,15 @@ class BundleAnalyzerPlugin {
7275
};
7376

7477
if (compiler.hooks) {
75-
compiler.hooks.done.tapAsync('webpack-bundle-analyzer', done);
78+
compiler.hooks.done.tapAsync(PLUGIN_NAME, done);
7679
} else {
7780
compiler.plugin('done', done);
7881
}
7982
}
8083

8184
async generateStatsFile(stats) {
8285
const statsFilepath = path.resolve(this.compiler.outputPath, this.opts.statsFilename);
83-
await fs.promises.mkdir(path.dirname(statsFilepath), {recursive: true});
86+
await this.fs.promises.mkdir(path.dirname(statsFilepath), {recursive: true});
8487

8588
try {
8689
await writeStats(stats, statsFilepath);
@@ -117,7 +120,8 @@ class BundleAnalyzerPlugin {
117120
reportFilename: path.resolve(this.compiler.outputPath, this.opts.reportFilename || 'report.json'),
118121
bundleDir: this.getBundleDirFromCompiler(),
119122
logger: this.logger,
120-
excludeAssets: this.opts.excludeAssets
123+
excludeAssets: this.opts.excludeAssets,
124+
fs: this.fs
121125
});
122126
}
123127

@@ -129,7 +133,8 @@ class BundleAnalyzerPlugin {
129133
bundleDir: this.getBundleDirFromCompiler(),
130134
logger: this.logger,
131135
defaultSizes: this.opts.defaultSizes,
132-
excludeAssets: this.opts.excludeAssets
136+
excludeAssets: this.opts.excludeAssets,
137+
fs: this.fs
133138
});
134139
}
135140

src/bin/analyzer.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const {resolve, dirname} = require('path');
44

55
const commander = require('commander');
66
const {magenta} = require('chalk');
7+
const fs = require('fs');
78

89
const analyzer = require('../analyzer');
910
const viewer = require('../viewer');
@@ -138,14 +139,16 @@ if (mode === 'server') {
138139
defaultSizes,
139140
bundleDir,
140141
excludeAssets,
141-
logger: new Logger(logLevel)
142+
logger: new Logger(logLevel),
143+
fs
142144
});
143145
} else if (mode === 'json') {
144146
viewer.generateJSONReport(bundleStats, {
145147
reportFilename: resolve(reportFilename || 'report.json'),
146148
bundleDir,
147149
excludeAssets,
148-
logger: new Logger(logLevel)
150+
logger: new Logger(logLevel),
151+
fs
149152
});
150153
}
151154

src/viewer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const path = require('path');
2-
const fs = require('fs');
32
const http = require('http');
43

54
const WebSocket = require('ws');
@@ -129,7 +128,8 @@ async function generateReport(bundleStats, opts) {
129128
bundleDir = null,
130129
logger = new Logger(),
131130
defaultSizes = 'parsed',
132-
excludeAssets = null
131+
excludeAssets = null,
132+
fs
133133
} = opts || {};
134134

135135
const chartData = getChartData({logger, excludeAssets}, bundleStats, bundleDir);
@@ -156,7 +156,7 @@ async function generateReport(bundleStats, opts) {
156156
}
157157

158158
async function generateJSONReport(bundleStats, opts) {
159-
const {reportFilename, bundleDir = null, logger = new Logger(), excludeAssets = null} = opts || {};
159+
const {reportFilename, bundleDir = null, logger = new Logger(), excludeAssets = null, fs} = opts || {};
160160

161161
const chartData = getChartData({logger, excludeAssets}, bundleStats, bundleDir);
162162

0 commit comments

Comments
 (0)