forked from webpack-contrib/webpack-bundle-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplugin.js
208 lines (175 loc) · 5.83 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
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
const fs = require('fs');
const del = require('del');
const _ = require('lodash');
const path = require('path');
const BundleAnalyzerPlugin = require('../lib/BundleAnalyzerPlugin');
describe('Plugin', function () {
describe('options', function () {
it('should be optional', function () {
expect(() => new BundleAnalyzerPlugin()).not.to.throw();
});
});
});
describe('Plugin', function () {
let nightmare;
this.timeout(15000);
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`);
});
forEachWebpackVersion(['4.44.2'], ({it, webpackCompile}) => {
// Webpack 5 doesn't support `jsonpFunction` option
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
});
});
});
forEachWebpackVersion(({it, webpackCompile}) => {
it('should allow to generate json report', async function () {
const config = makeWebpackConfig({
analyzerOpts: {
analyzerMode: 'json'
}
});
await webpackCompile(config);
const chartData = await getChartDataFromJSONReport();
expect(chartData).to.exist;
});
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();
const bundleGroup = chartData.find(group => group.label === 'bundle.js');
expect(bundleGroup.groups)
.to
.containSubset([
{
label: 'src',
path: './src',
groups: [
{
label: 'a.js',
path: './src/a.js'
},
{
label: 'b.js',
path: './src/b.js'
}
]
}
]);
});
});
describe('options', function () {
describe('excludeAssets', function () {
forEachWebpackVersion(({it, webpackCompile}) => {
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('reportTitle', function () {
it('should have a sensible default', async function () {
const config = makeWebpackConfig();
await webpackCompile(config, '4.44.2');
const generatedReportTitle = await getTitleFromReport();
expect(generatedReportTitle).to.match(/^umi-webpack-bundle-analyzer \[.* at \d{2}:\d{2}\]/u);
});
it('should support a string value', async function () {
const reportTitle = 'A string report title';
const config = makeWebpackConfig({
analyzerOpts: {
reportTitle
}
});
await webpackCompile(config, '4.44.2');
const generatedReportTitle = await getTitleFromReport();
expect(generatedReportTitle).to.equal(reportTitle);
});
it('should support a function value', async function () {
const reportTitleResult = 'A string report title';
const config = makeWebpackConfig({
analyzerOpts: {
reportTitle: () => reportTitleResult
}
});
await webpackCompile(config, '4.44.2');
const generatedReportTitle = await getTitleFromReport();
expect(generatedReportTitle).to.equal(reportTitleResult);
});
it('should propagate an error in a function', async function () {
const reportTitleError = new Error();
const config = makeWebpackConfig({
analyzerOpts: {
reportTitle: () => {throw reportTitleError}
}
});
let error = null;
try {
await webpackCompile(config, '4.44.2');
} catch (e) {
error = e;
}
expect(error).to.equal(reportTitleError);
});
});
});
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
});
}
function getChartDataFromJSONReport(reportFilename = 'report.json') {
return require(path.resolve(__dirname, `output/${reportFilename}`));
}
async function getTitleFromReport(reportFilename = 'report.html') {
return await nightmare.goto(`file://${__dirname}/output/${reportFilename}`).title();
}
async function getChartDataFromReport(reportFilename = 'report.html') {
return await nightmare.goto(`file://${__dirname}/output/${reportFilename}`).evaluate(() => window.chartData);
}
});