Skip to content

Commit 36d30b5

Browse files
committed
fix: pass statsOptions to analyzer modes
1 parent a2ae1ef commit 36d30b5

2 files changed

Lines changed: 70 additions & 3 deletions

File tree

src/BundleAnalyzerPlugin.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,17 @@ class BundleAnalyzerPlugin {
111111
}
112112

113113
if (this.opts.analyzerMode === "server") {
114-
actions.push(() => this.startAnalyzerServer(stats.toJson()));
114+
actions.push(() =>
115+
this.startAnalyzerServer(stats.toJson(this.opts.statsOptions)),
116+
);
115117
} else if (this.opts.analyzerMode === "static") {
116-
actions.push(() => this.generateStaticReport(stats.toJson()));
118+
actions.push(() =>
119+
this.generateStaticReport(stats.toJson(this.opts.statsOptions)),
120+
);
117121
} else if (this.opts.analyzerMode === "json") {
118-
actions.push(() => this.generateJSONReport(stats.toJson()));
122+
actions.push(() =>
123+
this.generateJSONReport(stats.toJson(this.opts.statsOptions)),
124+
);
119125
}
120126

121127
if (actions.length) {

test/plugin.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,67 @@ describe("Plugin options", () => {
2020
expect(() => new BundleAnalyzerPlugin()).not.toThrow();
2121
});
2222
});
23+
24+
describe("statsOptions", () => {
25+
async function runPluginDoneHook(plugin, stats) {
26+
let doneHook;
27+
const compiler = {
28+
hooks: {
29+
done: {
30+
tapAsync: jest.fn((name, callback) => {
31+
doneHook = callback;
32+
}),
33+
},
34+
},
35+
outputFileSystem: {
36+
constructor: {
37+
name: "MemoryFileSystem",
38+
},
39+
},
40+
outputPath: path.resolve(__dirname, "./output"),
41+
};
42+
43+
plugin.apply(compiler);
44+
45+
await new Promise((resolve, reject) => {
46+
doneHook(stats, (error) => {
47+
if (error) {
48+
reject(error);
49+
} else {
50+
resolve();
51+
}
52+
});
53+
});
54+
}
55+
56+
it.each([
57+
["server", "startAnalyzerServer"],
58+
["static", "generateStaticReport"],
59+
["json", "generateJSONReport"],
60+
])(
61+
"should pass statsOptions to stats.toJson in %s mode",
62+
async (analyzerMode, methodName) => {
63+
const statsOptions = { all: false, assets: true };
64+
const statsJson = { assets: [] };
65+
const stats = {
66+
toJson: jest.fn(() => statsJson),
67+
};
68+
const plugin = new BundleAnalyzerPlugin({
69+
analyzerMode,
70+
openAnalyzer: false,
71+
statsOptions,
72+
});
73+
const analyzerMethod = jest
74+
.spyOn(plugin, methodName)
75+
.mockResolvedValue();
76+
77+
await runPluginDoneHook(plugin, stats);
78+
79+
expect(stats.toJson).toHaveBeenCalledWith(statsOptions);
80+
expect(analyzerMethod).toHaveBeenCalledWith(statsJson);
81+
},
82+
);
83+
});
2384
});
2485

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

0 commit comments

Comments
 (0)