Skip to content

Commit bdb4e2f

Browse files
committed
fix: request analyzer stats fields
1 parent a2ae1ef commit bdb4e2f

3 files changed

Lines changed: 125 additions & 3 deletions

File tree

.changeset/fair-forks-tap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack-bundle-analyzer": patch
3+
---
4+
5+
Ensure analyzer modes request the stats fields needed for bundle analysis.

src/BundleAnalyzerPlugin.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,32 @@ const viewer = require("./viewer");
3030
/** @typedef {string | (() => string)} ReportTitle */
3131
/** @typedef {(options: { listenHost: string, listenPort: number, boundAddress: string | AddressInfo | null }) => string} AnalyzerUrl */
3232

33+
/** @type {StatsOptions} */
34+
const analyzerStatsOptions = {
35+
all: false,
36+
assets: true,
37+
cachedAssets: true,
38+
cachedModules: true,
39+
cached: true,
40+
children: true,
41+
chunks: true,
42+
chunkModules: true,
43+
chunkModulesSpace: Number.POSITIVE_INFINITY,
44+
depth: true,
45+
entrypoints: true,
46+
errors: false,
47+
errorsCount: false,
48+
ids: true,
49+
modules: true,
50+
modulesSpace: Number.POSITIVE_INFINITY,
51+
nestedModules: true,
52+
nestedModulesSpace: Number.POSITIVE_INFINITY,
53+
runtimeModules: false,
54+
source: false,
55+
warnings: false,
56+
warningsCount: false,
57+
};
58+
3359
/**
3460
* @typedef {object} Options
3561
* @property {Mode=} analyzerMode analyzer mode
@@ -111,11 +137,17 @@ class BundleAnalyzerPlugin {
111137
}
112138

113139
if (this.opts.analyzerMode === "server") {
114-
actions.push(() => this.startAnalyzerServer(stats.toJson()));
140+
actions.push(() =>
141+
this.startAnalyzerServer(stats.toJson(analyzerStatsOptions)),
142+
);
115143
} else if (this.opts.analyzerMode === "static") {
116-
actions.push(() => this.generateStaticReport(stats.toJson()));
144+
actions.push(() =>
145+
this.generateStaticReport(stats.toJson(analyzerStatsOptions)),
146+
);
117147
} else if (this.opts.analyzerMode === "json") {
118-
actions.push(() => this.generateJSONReport(stats.toJson()));
148+
actions.push(() =>
149+
this.generateJSONReport(stats.toJson(analyzerStatsOptions)),
150+
);
119151
}
120152

121153
if (actions.length) {

test/plugin.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,91 @@ describe("Plugin options", () => {
2020
expect(() => new BundleAnalyzerPlugin()).not.toThrow();
2121
});
2222
});
23+
24+
describe("statsOptions", () => {
25+
const expectedAnalyzerStatsOptions = {
26+
all: false,
27+
assets: true,
28+
cachedAssets: true,
29+
cachedModules: true,
30+
cached: true,
31+
children: true,
32+
chunks: true,
33+
chunkModules: true,
34+
chunkModulesSpace: Number.POSITIVE_INFINITY,
35+
depth: true,
36+
entrypoints: true,
37+
errors: false,
38+
errorsCount: false,
39+
ids: true,
40+
modules: true,
41+
modulesSpace: Number.POSITIVE_INFINITY,
42+
nestedModules: true,
43+
nestedModulesSpace: Number.POSITIVE_INFINITY,
44+
runtimeModules: false,
45+
source: false,
46+
warnings: false,
47+
warningsCount: false,
48+
};
49+
50+
async function runPluginDoneHook(plugin, stats) {
51+
let doneHook;
52+
const compiler = {
53+
hooks: {
54+
done: {
55+
tapAsync: jest.fn((name, callback) => {
56+
doneHook = callback;
57+
}),
58+
},
59+
},
60+
outputFileSystem: {
61+
constructor: {
62+
name: "MemoryFileSystem",
63+
},
64+
},
65+
outputPath: path.resolve(__dirname, "./output"),
66+
};
67+
68+
plugin.apply(compiler);
69+
70+
await new Promise((resolve, reject) => {
71+
doneHook(stats, (error) => {
72+
if (error) {
73+
reject(error);
74+
} else {
75+
resolve();
76+
}
77+
});
78+
});
79+
}
80+
81+
it.each([
82+
["server", "startAnalyzerServer"],
83+
["static", "generateStaticReport"],
84+
["json", "generateJSONReport"],
85+
])(
86+
"should use analyzer stats options in %s mode",
87+
async (analyzerMode, methodName) => {
88+
const statsJson = { assets: [] };
89+
const stats = {
90+
toJson: jest.fn(() => statsJson),
91+
};
92+
const plugin = new BundleAnalyzerPlugin({
93+
analyzerMode,
94+
openAnalyzer: false,
95+
statsOptions: { all: false, assets: false },
96+
});
97+
const analyzerMethod = jest
98+
.spyOn(plugin, methodName)
99+
.mockResolvedValue();
100+
101+
await runPluginDoneHook(plugin, stats);
102+
103+
expect(stats.toJson).toHaveBeenCalledWith(expectedAnalyzerStatsOptions);
104+
expect(analyzerMethod).toHaveBeenCalledWith(statsJson);
105+
},
106+
);
107+
});
23108
});
24109

25110
describe("Plugin", () => {

0 commit comments

Comments
 (0)