forked from webpack-contrib/webpack-bundle-analyzer
/
plugin.js
160 lines (132 loc) · 4.15 KB
/
plugin.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
const fs = require('fs');
const del = require('del');
const _ = require('lodash');
let nightmare;
describe('Plugin', function () {
this.timeout(3000);
before(function () {
const Nightmare = require('nightmare');
nightmare = Nightmare();
del.sync(`${__dirname}/output`);
});
beforeEach(async function () {
this.timeout(10000);
await nightmare.goto('about:blank');
});
afterEach(function () {
del.sync(`${__dirname}/output`);
});
it('should support webpack config with custom `jsonpFunction` name', async function () {
const config = makeWebpackConfig({
multipleChunks: true
});
config.output.jsonpFunction = 'somethingCompletelyDifferent';
await webpackCompile(config);
await expectValidReport({
parsedSize: 1343,
gzipSize: 360
});
});
it('should support webpack config with `multi` module', async function () {
const config = makeWebpackConfig();
config.entry.bundle = [
'./src/a.js',
'./src/b.js'
];
await webpackCompile(config);
const chartData = await getChartDataFromReport();
expect(chartData[0].groups).to.containSubset([{
label: 'multi ./src/a.js ./src/b.js',
path: './multi ./src/a.js ./src/b.js',
groups: undefined
}]);
});
describe('options', function () {
describe('excludeAssets', function () {
it('should filter out assets from the report', async function () {
const config = makeWebpackConfig({
multipleChunks: true,
analyzerOpts: {
excludeAssets: 'manifest'
}
});
await webpackCompile(config);
const chartData = await getChartDataFromReport();
expect(_.map(chartData, 'label')).to.deep.equal(['bundle.js']);
});
});
describe('bundleDirOption', function () {
it('should report parsedSize and gzipSize when bundleDirOption is not provided', async function () {
const config = makeWebpackConfig({
analyzerOpts: {
}
});
await webpackCompile(config);
const chartData = await getChartDataFromReport();
expect(
chartData[0].groups.every(group => (
('parsedSize' in group)
&& ('gzipSize' in group)
&& ('statSize' in group)
))
).to.be.true;
});
it('should report parsedSize and gzipSize when bundleDirOption is null', async function () {
const config = makeWebpackConfig({
analyzerOpts: {
bundleDirOption: null
}
});
await webpackCompile(config);
const chartData = await getChartDataFromReport();
expect(
chartData[0].groups.every(group => (
('parsedSize' in group)
&& ('gzipSize' in group)
&& ('statSize' in group)
))
).to.be.true;
});
it('should report statSize only bundleDirOption is false', async function () {
const config = makeWebpackConfig({
analyzerOpts: {
bundleDirOption: false
}
});
await webpackCompile(config);
const chartData = await getChartDataFromReport();
expect(
chartData[0].groups.every(group => (
!group.parsedSize
&& !group.gzipSize
&& group.statSize
))
).to.be.true;
});
});
});
});
async function expectValidReport(opts) {
const {
bundleFilename = 'bundle.js',
reportFilename = 'report.html',
bundleLabel = 'bundle.js',
statSize = 141,
parsedSize = 2821,
gzipSize = 770
} = opts || {};
expect(fs.existsSync(`${__dirname}/output/${bundleFilename}`), 'bundle file missing').to.be.true;
expect(fs.existsSync(`${__dirname}/output/${reportFilename}`), 'report file missing').to.be.true;
const chartData = await getChartDataFromReport(reportFilename);
expect(chartData[0]).to.containSubset({
label: bundleLabel,
statSize,
parsedSize,
gzipSize
});
}
async function getChartDataFromReport(reportFilename = 'report.html') {
return await nightmare
.goto(`file://${__dirname}/output/${reportFilename}`)
.evaluate(() => window.chartData);
}