Skip to content

Commit 814bf39

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

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

src/BundleAnalyzerPlugin.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const fs = require('fs');
21
const path = require('path');
32
const {bold} = require('chalk');
43

54
const Logger = require('./Logger');
65
const viewer = require('./viewer');
76
const utils = require('./utils');
87
const {writeStats} = require('./statsUtils');
8+
const PLUGIN_NAME = 'webpack-bundle-analyzer';
99

1010
class BundleAnalyzerPlugin {
1111
constructor(opts = {}) {
@@ -28,13 +28,18 @@ class BundleAnalyzerPlugin {
2828
};
2929

3030
this.server = null;
31-
this.logger = new Logger(this.opts.logLevel);
3231
}
3332

3433
apply(compiler) {
34+
const isFromWebpackDevServer = process.env.WEBPACK_SERVE === 'true';
35+
3536
this.compiler = compiler;
3637

38+
this.logger = require('webpack/lib/logging/runtime')?.getLogger(PLUGIN_NAME)
39+
|| new Logger(this.opts.logLevel);
40+
3741
const done = (stats, callback) => {
42+
this.fs = isFromWebpackDevServer ? compiler.outputFileSystem : require('fs');
3843
callback = callback || (() => {});
3944

4045
const actions = [];
@@ -72,15 +77,15 @@ class BundleAnalyzerPlugin {
7277
};
7378

7479
if (compiler.hooks) {
75-
compiler.hooks.done.tapAsync('webpack-bundle-analyzer', done);
80+
compiler.hooks.done.tapAsync(PLUGIN_NAME, done);
7681
} else {
7782
compiler.plugin('done', done);
7883
}
7984
}
8085

8186
async generateStatsFile(stats) {
8287
const statsFilepath = path.resolve(this.compiler.outputPath, this.opts.statsFilename);
83-
await fs.promises.mkdir(path.dirname(statsFilepath), {recursive: true});
88+
await this.fs.promises.mkdir(path.dirname(statsFilepath), {recursive: true});
8489

8590
try {
8691
await writeStats(stats, statsFilepath);
@@ -117,7 +122,8 @@ class BundleAnalyzerPlugin {
117122
reportFilename: path.resolve(this.compiler.outputPath, this.opts.reportFilename || 'report.json'),
118123
bundleDir: this.getBundleDirFromCompiler(),
119124
logger: this.logger,
120-
excludeAssets: this.opts.excludeAssets
125+
excludeAssets: this.opts.excludeAssets,
126+
fs: this.fs
121127
});
122128
}
123129

@@ -129,7 +135,8 @@ class BundleAnalyzerPlugin {
129135
bundleDir: this.getBundleDirFromCompiler(),
130136
logger: this.logger,
131137
defaultSizes: this.opts.defaultSizes,
132-
excludeAssets: this.opts.excludeAssets
138+
excludeAssets: this.opts.excludeAssets,
139+
fs: this.fs
133140
});
134141
}
135142

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)