-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
84 lines (73 loc) · 2.77 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
/* eslint no-console: 0 */
const exec = require('child_process').exec;
const fs = require('fs');
const report = require('./report');
const validOptions = require('./validOptions');
const colors = require('colors');
/**
* PerformanceBudgetPlugin constructor
*
* @param {any} [options={}]
*/
function PerformanceBudgetPlugin(options = {}) {
this.validOptions = Object.keys(options);
if (this.validOptions.length === 0) {
this.validOptions.push('domInteractive', 'domContentLoaded', 'domComplete');
}
this.validOptions = this.validOptions.filter((option) => option !== 'metricsSummary' && option !== 'numberOfRebuilds');
this.validOptions = this.validOptions.filter(option => validOptions.indexOf(option) > -1);
this.validOptions = this.validOptions.map((option) => {
return {
name: option,
value: options[option] || Infinity,
arr: []
};
});
this.metricsSummary = options.metricsSummary || false;
this.numberOfRebuilds = options.numberOfRebuilds || 2;
}
PerformanceBudgetPlugin.prototype.apply = function apply(compiler) {
let count = 0;
compiler.plugin('done', () => {
if (!compiler.options.devServer) {
throw Error('[performance-budget-plugin]: requires webpack-dev-server');
}
const host = compiler.options.devServer.host;
const port = compiler.options.devServer.port;
const url = `http://${ host }:${ port }`;
const commandJSON = `node node_modules/phantomas/bin/phantomas.js ${ url } --reporter=json:pretty > performance-budget.json`;
count++;
if (count < this.numberOfRebuilds + 1) {
console.log('\n');
console.log(colors.bgYellow.black(`[performance-budget-plugin]: Will output metrics after ${ this.numberOfRebuilds } rebuilds`));
return;
}
exec(commandJSON, (err) => {
if (err) {
console.log('\n\n');
console.error(colors.bgRed.black('[performance-budget-plugin]: Unable to execute perfomance analysis'));
return;
}
fs.readFile('performance-budget.json', (error, jsondata) => {
try {
const result = JSON.parse(jsondata);
report(result, this.validOptions);
if (this.metricsSummary) {
console.log('\n\n');
console.log(colors.bgYellow.black('############### METRICS SUMMARY #################'));
}
if (this.metricsSummary === 'minimal' || this.metricsSummary === true) {
console.log(result.metrics);
} else if (this.metricsSummary === 'verbose') {
console.log(result);
}
fs.unlink('performance-budget.json');
} catch (e) {
console.error(colors.bgRed.black('[performance-budget-plugin]: Unable to parse performance metrics'));
}
}
);
});
});
};
module.exports = PerformanceBudgetPlugin;