-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
157 lines (138 loc) · 4.5 KB
/
index.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
// @ts-check
/**
* @typedef {Object} Config
* @property {boolean} bail - True to throw an error for each failed audit. False to use warnings instead. Defaults to false.
* @property {boolean | string} optimizationBailout - Optional optimizationBailout results exported to a file. Set to true of filename.
* @property {GimbalOptions} options - Gimbal options
*/
/**
* @typedef {Object} GimbalOptions
* @prop {string} buildDir - Build directory relative to outputPath
* @prop {boolean} [comment] - Output comments
* @prop {boolean} [verbose] - Verbose output for debugging
* @prop {boolean} [checkThresholds] - Check custom thresholds. Defaults to true
* @prop {boolean} [size] - Run size audit
* @prop {boolean} [calculateUnusedSource] - Run unused source audit
* @prop {boolean} [heapSnapshot] - Run heap size audit
* @prop {boolean} [lighthouse] - Run lighthouse audit
*/
/**
* @typedef {Object} GimbalResults
* @prop {GimbalJobResult[]} data - Audit Jobs
* @prop {boolean} [success] - False if at least one audit job was unsuccessful
*/
/**
* @typedef {Object} GimbalJobResult
* @prop {GimbalJobReport[]} data - Audit Jobs
* @prop {string} label - Job name
* @prop {boolean} [success] - False if at least one report was unsuccessful
*/
/**
* @typedef {Object} GimbalJobReport
* @prop {string} label - Report name
* @prop {string} value - Reported value
* @prop {string} threshold - Threshold value
* @prop {boolean} [success] - Success
*/
const fs = require('fs');
module.exports = class GimbalPlugin {
constructor(options) {
const defaults = {
bail: false,
optimizationBailout: false,
options: {
buildDir: '',
comment: true,
verbose: false,
checkThresholds: true,
size: true,
calculateUnusedSource: true,
heapSnapshot: true,
lighthouse: true,
},
};
const cfg = Object.assign({}, defaults, options);
/** @type {Config} */
this.cfg = cfg;
}
async executeAudits(context, compilation, cb) {
if (process.env.NODE_ENV !== 'production') {
// We only want Gimbal to run in prod
cb();
return;
}
const { audit } = require('@modus/gimbal');
const reportProgress = context && context.reportProgress;
if (reportProgress) reportProgress(0, 'Starting performance audit');
const results = await audit(
Object.assign(
{
cwd: compilation.compiler.outputPath,
buildDir: '',
comment: true,
verbose: false,
checkThresholds: true,
},
this.cfg.options,
),
);
if (reportProgress) reportProgress(1, 'Performance audit complete');
if (results.success === false) {
this.processMessages(compilation, results);
}
cb();
}
/**
* @param {GimbalResults} results - Gimbal audit results
*/
processMessages(compilation, results) {
const failed = results.data.filter(result => !result.success);
failed.forEach(category => {
category.data
.filter(result => !result.success)
.forEach(failure => {
const msg = `[Gimbal: ${category.label}] ${failure.label}: ${failure.value} (threshold ${
failure.threshold
}).`;
if (this.cfg.bail) {
compilation.errors.push(msg);
} else {
compilation.warnings.push(msg);
}
});
});
}
apply(compiler) {
compiler.hooks.emit.tapAsync(
{
name: 'GimbalPlugin',
context: true,
},
this.executeAudits.bind(this),
);
if (this.cfg.optimizationBailout) {
compiler.hooks.done.tapAsync(
{
name: 'GimbalPlugin',
context: true,
},
stats => {
const optimizationBailout = this.cfg.optimizationBailout;
const bailout = stats
.toJson({ optimizationBailout: true, maxModules: Infinity })
.modules.map(mod =>
Array.isArray(mod.optimizationBailout) && mod.optimizationBailout.length
? [mod.name, mod.optimizationBailout, mod.chunks]
: false,
)
.filter(Boolean)
.filter(([name]) => !name.match(/node_modules|\(webpack\)|\(ignored\)|^multi/))
.filter(([, modules]) => !modules.toString().match(/import\(\)|HMR/));
const fileName = typeof optimizationBailout === 'string' ? optimizationBailout : 'bailout.json';
// @ts-ignore
fs.writeFile(fileName, JSON.stringify(bailout), 'utf8');
},
);
}
}
};