-
-
Notifications
You must be signed in to change notification settings - Fork 489
/
Copy pathanalyzer.js
326 lines (279 loc) · 10.8 KB
/
analyzer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
const chai = require('chai');
chai.use(require('chai-subset'));
const {expect} = chai;
const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const del = require('del');
const childProcess = require('child_process');
const puppeteer = require('puppeteer');
const {getChunkToInitialByEntrypoint} = require('../src/analyzer');
let browser;
describe('Analyzer', function () {
jest.setTimeout(15000);
beforeAll(async function () {
browser = await puppeteer.launch();
del.sync(`${__dirname}/output`);
});
beforeEach(async function () {
jest.setTimeout(15000);
});
afterEach(function () {
del.sync(`${__dirname}/output`);
});
afterAll(async function () {
await browser.close();
});
it('should support stats files with all the information in `children` array', async function () {
generateReportFrom('with-children-array.json');
await expectValidReport();
});
it('should generate report containing worker bundles', async function () {
generateReportFrom('with-worker-loader/stats.json');
const chartData = await getChartData();
expect(chartData[1]).to.containSubset({
label: 'bundle.worker.js'
});
});
it('should generate report for array webpack.config.js', async function () {
generateReportFrom('with-array-config/stats.json');
const chartData = await getChartData();
expect(chartData).to.have.lengthOf(2);
expect(chartData[0]).to.containSubset({
label: 'config-1-main.js'
});
expect(chartData[1]).to.containSubset({
label: 'config-2-main.js'
});
});
it('should generate report when worker bundles have dynamic imports', async function () {
generateReportFrom('with-worker-loader-dynamic-import/stats.json');
const chartData = await getChartData();
expect(chartData[1]).to.containSubset({
label: '1.bundle.worker.js'
});
});
it('should support stats files with modules inside `chunks` array', async function () {
generateReportFrom('with-modules-in-chunks/stats.json');
const chartData = await getChartData();
expect(chartData).to.containSubset(
require('./stats/with-modules-in-chunks/expected-chart-data')
);
});
it('should record accurate byte lengths for sources with special chars', async function () {
generateReportFrom('with-special-chars/stats.json');
const chartData = await getChartData();
expect(chartData).to.containSubset(
require('./stats/with-special-chars/expected-chart-data')
);
});
it('should support bundles with invalid dynamic require calls', async function () {
generateReportFrom('with-invalid-dynamic-require.json');
await expectValidReport({statSize: 136});
});
it('should use information about concatenated modules generated by webpack 4', async function () {
generateReportFrom('with-module-concatenation-info/stats.json');
const chartData = await getChartData();
expect(chartData[0].groups[0]).to.containSubset(
require('./stats/with-module-concatenation-info/expected-chart-data')
);
});
it("should not filter out modules that we could't find during parsing", async function () {
generateReportFrom('with-missing-parsed-module/stats.json');
const chartData = await getChartData();
let unparsedModules = 0;
forEachChartItem(chartData, item => {
if (typeof item.parsedSize !== 'number') {
unparsedModules++;
}
});
expect(unparsedModules).to.equal(1);
});
it('should gracefully parse invalid chunks', async function () {
generateReportFrom('with-invalid-chunk/stats.json');
const chartData = await getChartData();
const invalidChunk = _.find(chartData, {label: 'invalid-chunk.js'});
expect(invalidChunk.groups).to.containSubset([
{
id: 1,
label: 'invalid.js',
path: './invalid.js',
statSize: 24
}
]);
expect(invalidChunk.statSize).to.equal(24);
expect(invalidChunk.parsedSize).to.equal(30);
});
it('should gracefully process missing chunks', async function () {
generateReportFrom('with-missing-chunk/stats.json');
const chartData = await getChartData();
const invalidChunk = _.find(chartData, {label: 'invalid-chunk.js'});
expect(invalidChunk).to.exist;
expect(invalidChunk.statSize).to.equal(24);
forEachChartItem([invalidChunk], item => {
expect(typeof item.statSize).to.equal('number');
expect(item.parsedSize).to.be.undefined;
});
const validChunk = _.find(chartData, {label: 'valid-chunk.js'});
forEachChartItem([validChunk], item => {
expect(typeof item.statSize).to.equal('number');
expect(typeof item.parsedSize).to.equal('number');
});
});
it('should gracefully process missing chunks', async function () {
generateReportFrom('with-missing-module-chunks/stats.json');
const chartData = await getChartData();
const invalidChunk = _.find(chartData, {label: 'invalid-chunk.js'});
expect(invalidChunk).to.exist;
expect(invalidChunk.statSize).to.equal(568);
forEachChartItem([invalidChunk], item => {
expect(typeof item.statSize).to.equal('number');
expect(item.parsedSize).to.be.undefined;
});
const validChunk = _.find(chartData, {label: 'valid-chunk.js'});
forEachChartItem([validChunk], item => {
expect(typeof item.statSize).to.equal('number');
expect(typeof item.parsedSize).to.equal('number');
});
});
it('should support stats files with js modules chunk', async function () {
generateReportFrom('with-modules-chunk.json');
await expectValidReport({bundleLabel: 'bundle.mjs'});
});
it('should properly parse extremely optimized bundle from webpack 5', async function () {
generateReportFrom('extremely-optimized-webpack-5-bundle/stats.json');
const chartData = await getChartData();
expect(chartData).to.containSubset(
require('./stats/extremely-optimized-webpack-5-bundle/expected-chart-data')
);
});
it('should properly parse webpack 5 bundle with single entry', async function () {
generateReportFrom('webpack-5-bundle-with-single-entry/stats.json');
const chartData = await getChartData();
expect(chartData).to.containSubset(
require('./stats/webpack-5-bundle-with-single-entry/expected-chart-data')
);
});
it('should properly parse webpack 5 bundle with multiple entries', async function () {
generateReportFrom('webpack-5-bundle-with-multiple-entries/stats.json');
const chartData = await getChartData();
expect(chartData).to.containSubset(
require('./stats/webpack-5-bundle-with-multiple-entries/expected-chart-data')
);
});
it('should support generating JSON output for the report', async function () {
generateJSONReportFrom('with-modules-in-chunks/stats.json');
const chartData = require(path.resolve(__dirname, 'output/report.json'));
expect(chartData).to.containSubset(require('./stats/with-modules-in-chunks/expected-chart-data'));
});
it('should support stats files with non-asset asset', async function () {
generateReportFrom('with-non-asset-asset/stats.json');
await expectValidReport({bundleLabel: 'bundle.js'});
});
describe('options', function () {
describe('title', function () {
it('should take the --title option', async function () {
const reportTitle = 'A string report title';
generateReportFrom('with-modules-chunk.json', `--title "${reportTitle}"`);
const generatedReportTitle = await getTitleFromReport();
expect(generatedReportTitle).to.equal(reportTitle);
});
it('should take the -t option', async function () {
const reportTitle = 'A string report title';
generateReportFrom('with-modules-chunk.json', `-t "${reportTitle}"`);
const generatedReportTitle = await getTitleFromReport();
expect(generatedReportTitle).to.equal(reportTitle);
});
it('should use a suitable default title', async function () {
generateReportFrom('with-modules-chunk.json');
const generatedReportTitle = await getTitleFromReport();
expect(generatedReportTitle).to.match(/^webpack-bundle-analyzer \[.* at \d{2}:\d{2}\]/u);
});
});
});
});
describe('getChunkToInitialByEntrypoint', function () {
it('should map chunks correctly to entrypoints', function () {
const bundleStats = {
entrypoints: {
'A': {
name: 'A',
assets: [
{
name: 'chunkA.js'
}
]
},
'B': {
name: 'B',
assets: [
{
name: 'chunkA.js'
},
{
name: 'chunkB.js'
}
]
}
}
};
expect(JSON.stringify(getChunkToInitialByEntrypoint(bundleStats))).to.equal(JSON.stringify({
'chunkA.js': {'A': true, 'B': true},
'chunkB.js': {'B': true}
}));
});
it('should handle when bundlestats is null or undefined ', function () {
expect(JSON.stringify(getChunkToInitialByEntrypoint(null))).to.equal(JSON.stringify({}));
expect(JSON.stringify(getChunkToInitialByEntrypoint(undefined))).to.equal(JSON.stringify({}));
});
it('should handle when bundlestats is emoty', function () {
const bundleStatsWithoutEntryPoints = {};
expect(JSON.stringify(getChunkToInitialByEntrypoint(bundleStatsWithoutEntryPoints))).to.equal(JSON.stringify({}));
});
it('should handle when entrypoints is empty', function () {
const bundleStatsEmptyEntryPoint = {entrypoints: {}};
expect(JSON.stringify(getChunkToInitialByEntrypoint(bundleStatsEmptyEntryPoint))).to.equal(JSON.stringify({}));
});
});
function generateJSONReportFrom(statsFilename) {
childProcess.execSync(`../lib/bin/analyzer.js -m json -r output/report.json stats/${statsFilename}`, {
cwd: __dirname
});
}
function generateReportFrom(statsFilename, additionalOptions = '') {
childProcess.execSync(
`../lib/bin/analyzer.js ${additionalOptions} -m static -r output/report.html -O stats/${statsFilename}`,
{
cwd: __dirname
});
}
async function getTitleFromReport() {
const page = await browser.newPage();
await page.goto(`file://${__dirname}/output/report.html`);
return await page.title();
}
async function getChartData() {
const page = await browser.newPage();
await page.goto(`file://${__dirname}/output/report.html`);
return await page.evaluate(() => window.chartData);
}
function forEachChartItem(chartData, cb) {
for (const item of chartData) {
cb(item);
if (item.groups) {
forEachChartItem(item.groups, cb);
}
}
}
async function expectValidReport(opts) {
const {
bundleLabel = 'bundle.js',
statSize = 141
} = opts || {};
expect(fs.existsSync(`${__dirname}/output/report.html`)).to.be.true;
const chartData = await getChartData();
expect(chartData[0]).to.containSubset({
label: bundleLabel,
statSize
});
}