/
plugin.js
129 lines (105 loc) · 3.34 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
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(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 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();
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']);
});
});
});
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 getChartDataFromReport(reportFilename = 'report.html') {
return await nightmare.goto(`file://${__dirname}/output/${reportFilename}`).evaluate(() => window.chartData);
}
});