Skip to content

Commit ec825e0

Browse files
committed
fix: read bundles via compiler outputFileSystem
1 parent 7dc7ec6 commit ec825e0

4 files changed

Lines changed: 48 additions & 26 deletions

File tree

src/BundleAnalyzerPlugin.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ class BundleAnalyzerPlugin {
181181
reportTitle: this.opts.reportTitle,
182182
compressionAlgorithm: this.opts.compressionAlgorithm,
183183
bundleDir: this.getBundleDirFromCompiler(),
184+
bundleDirFs: this.getBundleDirFsFromCompiler(),
184185
logger: this.logger,
185186
defaultSizes: this.opts.defaultSizes,
186187
excludeAssets: this.opts.excludeAssets,
@@ -202,6 +203,7 @@ class BundleAnalyzerPlugin {
202203
),
203204
compressionAlgorithm: this.opts.compressionAlgorithm,
204205
bundleDir: this.getBundleDirFromCompiler(),
206+
bundleDirFs: this.getBundleDirFsFromCompiler(),
205207
logger: this.logger,
206208
excludeAssets: this.opts.excludeAssets,
207209
});
@@ -222,40 +224,45 @@ class BundleAnalyzerPlugin {
222224
reportTitle: this.opts.reportTitle,
223225
compressionAlgorithm: this.opts.compressionAlgorithm,
224226
bundleDir: this.getBundleDirFromCompiler(),
227+
bundleDirFs: this.getBundleDirFsFromCompiler(),
225228
logger: this.logger,
226229
defaultSizes: this.opts.defaultSizes,
227230
excludeAssets: this.opts.excludeAssets,
228231
});
229232
}
230233

231234
getBundleDirFromCompiler() {
232-
const { outputFileSystem } = /** @type {Compiler} */ (this.compiler);
235+
const compiler = /** @type {Compiler} */ (this.compiler);
236+
const outputFs = /** @type {OutputFileSystem | undefined} */ (
237+
compiler.outputFileSystem
238+
);
233239

234-
// TypeScript thinks `outputFileSystem` can be null, but in practice it's always set
235-
// when this method is called
236-
const outputFs = /** @type {OutputFileSystem} */ (outputFileSystem);
240+
if (!outputFs || outputFs === fs) {
241+
return compiler.outputPath;
242+
}
237243

238-
// Detect `memfs` (used by webpack-dev-server 4+) by checking for the `__vol` property
239-
// Related: #471
240-
if (/** @type {{ __vol?: unknown }} */ (outputFs).__vol) {
241-
return null;
244+
if (typeof outputFs.readFileSync === "function") {
245+
return compiler.outputPath;
242246
}
243247

244-
const outputFileSystemConstructor = outputFs.constructor;
248+
return null;
249+
}
250+
251+
getBundleDirFsFromCompiler() {
252+
const compiler = /** @type {Compiler} */ (this.compiler);
253+
const outputFs = /** @type {OutputFileSystem | undefined} */ (
254+
compiler.outputFileSystem
255+
);
245256

246-
if (typeof outputFileSystemConstructor === "undefined") {
247-
return /** @type {Compiler} */ (this.compiler).outputPath;
257+
if (!outputFs || outputFs === fs) {
258+
return null;
248259
}
249-
switch (outputFileSystemConstructor.name) {
250-
case "MemoryFileSystem":
251-
return null;
252-
// Detect AsyncMFS used by Nuxt 2.5 that replaces webpack's MFS during development
253-
// Related: #274
254-
case "AsyncMFS":
255-
return null;
256-
default:
257-
return /** @type {Compiler} */ (this.compiler).outputPath;
260+
261+
if (typeof outputFs.readFileSync !== "function") {
262+
return null;
258263
}
264+
265+
return outputFs;
259266
}
260267
}
261268

src/analyzer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ function isEntryModule(statsModule) {
180180
* @property {Logger} logger logger
181181
* @property {CompressionAlgorithm} compressionAlgorithm compression algorithm
182182
* @property {ExcludeAssets} excludeAssets exclude assets
183+
* @property {{ readFileSync: (path: string, encoding: string) => string } | null} bundleDirFs filesystem for reading bundle files
183184
*/
184185

185186
/** @typedef {import("./tree/Module").ModuleChartData} ModuleChartData */
@@ -216,6 +217,7 @@ function getViewerData(bundleStats, bundleDir, opts) {
216217
logger = new Logger(),
217218
compressionAlgorithm = "gzip",
218219
excludeAssets = null,
220+
bundleDirFs = fs,
219221
} = opts || {};
220222

221223
const isAssetIncluded = createAssetsFilter(excludeAssets);
@@ -286,6 +288,7 @@ function getViewerData(bundleStats, bundleDir, opts) {
286288
try {
287289
bundleInfo = parseBundle(assetFile, {
288290
sourceType: statAsset.info.javascriptModule ? "module" : "script",
291+
fs: bundleDirFs || fs,
289292
});
290293
} catch (err) {
291294
const msg =

src/parseUtils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,13 @@ function isAsyncWebWorkerChunkExpression(node) {
323323

324324
/**
325325
* @param {string} bundlePath bundle path
326-
* @param {{ sourceType: "script" | "module" }} opts options
326+
* @param {{ sourceType: "script" | "module", fs?: { readFileSync: (path: string, encoding: string) => string } }} opts options
327327
* @returns {{ modules: Modules, src: string, runtimeSrc: string }} parsed result
328328
*/
329329
module.exports.parseBundle = function parseBundle(bundlePath, opts) {
330-
const { sourceType = "script" } = opts || {};
330+
const { sourceType = "script", fs: bundleFs = fs } = opts || {};
331331

332-
const content = fs.readFileSync(bundlePath, "utf8");
332+
const content = bundleFs.readFileSync(bundlePath, "utf8");
333333
const ast = acorn.parse(content, {
334334
sourceType,
335335
ecmaVersion: "latest",

src/viewer.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const { open } = require("./utils");
2121
/** @typedef {import("./BundleAnalyzerPlugin").ExcludeAssets} ExcludeAssets */
2222
/** @typedef {import("./analyzer").ViewerDataOptions} ViewerDataOptions */
2323
/** @typedef {import("./analyzer").ChartData} ChartData */
24+
/** @typedef {{ readFileSync: (path: string, encoding: string) => string }} BundleDirFs */
2425

2526
const projectRoot = path.resolve(__dirname, "..");
2627

@@ -107,6 +108,7 @@ function getChartData(analyzerOpts, bundleStats, bundleDir) {
107108
* @property {string} host host
108109
* @property {boolean} openBrowser true when need to open browser, otherwise false
109110
* @property {string | null} bundleDir bundle dir
111+
* @property {BundleDirFs | null} bundleDirFs filesystem for reading bundle files
110112
* @property {Logger} logger logger
111113
* @property {Sizes} defaultSizes default sizes
112114
* @property {CompressionAlgorithm} compressionAlgorithm compression algorithm
@@ -128,6 +130,7 @@ async function startServer(bundleStats, opts) {
128130
host = "127.0.0.1",
129131
openBrowser = true,
130132
bundleDir = null,
133+
bundleDirFs = null,
131134
logger = new Logger(),
132135
defaultSizes = "parsed",
133136
compressionAlgorithm,
@@ -136,7 +139,12 @@ async function startServer(bundleStats, opts) {
136139
analyzerUrl,
137140
} = opts || {};
138141

139-
const analyzerOpts = { logger, excludeAssets, compressionAlgorithm };
142+
const analyzerOpts = {
143+
logger,
144+
excludeAssets,
145+
compressionAlgorithm,
146+
bundleDirFs,
147+
};
140148

141149
let chartData = getChartData(analyzerOpts, bundleStats, bundleDir);
142150

@@ -240,6 +248,7 @@ async function startServer(bundleStats, opts) {
240248
* @property {string} reportFilename report filename
241249
* @property {ReportTitle} reportTitle report title
242250
* @property {string | null} bundleDir bundle dir
251+
* @property {BundleDirFs | null} bundleDirFs filesystem for reading bundle files
243252
* @property {Logger} logger logger
244253
* @property {Sizes} defaultSizes default sizes
245254
* @property {CompressionAlgorithm} compressionAlgorithm compression algorithm
@@ -257,14 +266,15 @@ async function generateReport(bundleStats, opts) {
257266
reportFilename,
258267
reportTitle,
259268
bundleDir = null,
269+
bundleDirFs = null,
260270
logger = new Logger(),
261271
defaultSizes = "parsed",
262272
compressionAlgorithm,
263273
excludeAssets = null,
264274
} = opts || {};
265275

266276
const chartData = getChartData(
267-
{ logger, excludeAssets, compressionAlgorithm },
277+
{ logger, excludeAssets, compressionAlgorithm, bundleDirFs },
268278
bundleStats,
269279
bundleDir,
270280
);
@@ -302,6 +312,7 @@ async function generateReport(bundleStats, opts) {
302312
* @typedef {object} GenerateJSONReportOptions
303313
* @property {string} reportFilename report filename
304314
* @property {string | null} bundleDir bundle dir
315+
* @property {BundleDirFs | null} bundleDirFs filesystem for reading bundle files
305316
* @property {Logger} logger logger
306317
* @property {ExcludeAssets} excludeAssets exclude assets
307318
* @property {CompressionAlgorithm} compressionAlgorithm compression algorithm
@@ -316,13 +327,14 @@ async function generateJSONReport(bundleStats, opts) {
316327
const {
317328
reportFilename,
318329
bundleDir = null,
330+
bundleDirFs = null,
319331
logger = new Logger(),
320332
excludeAssets = null,
321333
compressionAlgorithm,
322334
} = opts || {};
323335

324336
const chartData = getChartData(
325-
{ logger, excludeAssets, compressionAlgorithm },
337+
{ logger, excludeAssets, compressionAlgorithm, bundleDirFs },
326338
bundleStats,
327339
bundleDir,
328340
);

0 commit comments

Comments
 (0)