/
measure-webpack-plugin.js
178 lines (159 loc) · 5.25 KB
/
measure-webpack-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
const NormalModule = require('webpack/lib/NormalModule');
const { RawSource } = require('webpack').sources;
const htmltpl = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>measureRes</title>
<style>td { border: 1px solid black }</style>
</head>
<body>
<h3>展示耗时排名前100, 全局变量 measureResult, formated 进行自定义分析</h3>
</body>
<script>
const measureResult = {measureResult}
const formated = {formated}
const lifetime = {lifetime}
const render = (columns, data) => {
const table = document.createElement('table')
const title = document.createElement('tr')
for (let column of columns) {
const td = document.createElement('td')
td.innerHTML = column.name
title.appendChild(td)
}
const content = document.createDocumentFragment()
for (let item of data) {
const tr = document.createElement('tr')
for (let column of columns) {
const td = document.createElement('td')
td.innerHTML = column.render(item)
tr.appendChild(td)
}
content.appendChild(tr)
}
table.appendChild(title)
table.appendChild(content)
document.body.appendChild(table)
}
const renderLifetime = (title, data) => {
if (title.length !== data.length) console.log('liftime长度不匹配')
let str = '总耗时: ' + data.reduce((a, b) => a + b) + ' '
title.forEach((t, i) => str += t + ' : ' + data[i] + ' ')
str += '所有模块build总耗时: ' + formated.reduce((a, b) => a + b.build, 0)
const content = document.createElement('h3')
content.innerHTML = str
document.body.appendChild(content)
}
renderLifetime(['make', 'clean', 'seal', 'createAsset'], lifetime.slice(1).map((x, i) => x - lifetime[i]))
render([{
name: 'id',
render: r => r.id
}, {
name: 'build总耗时',
render: r => r.build
}, {
name: '执行loader',
render: r => r.load
}, {
name: 'parse结果+创建hash',
render: r => r.parse
}, {
name: '创建snapshot',
render: r => r.snapshot
}, {
name: 'loader详情',
render: r => r.loaders.map(i => i.loader.replace(/.*node_modules/, '')).join(',')
}], formated.slice(0, 100))
</script>
</html>
`;
class MeasureWebpackPlugin {
constructor(opts = {}) {
const { formatId = id => id.replace(/.*\/(.*)/, '$1') } = opts;
this.formatId = formatId;
}
apply(compiler) {
const pluginname = 'measure-webpack-plugin';
compiler.options.parallelism = 1;
const lifetime = [];
const pushLifetime = (desc) => {
lifetime.push(Date.now());
console.log('lifetime', desc);
};
compiler.hooks.thisCompilation.tap(pluginname, (compilation) => {
const moduleHooks = NormalModule.getCompilationHooks(compilation);
const measureResult = {};
// build phase measurement
compilation.hooks.buildModule.tap(pluginname, (module) => {
if (module.resource) {
const id = this.formatId(module.resource);
if (!measureResult[id]) {
measureResult[id] = {};
}
}
});
moduleHooks.beforeLoaders.tap(pluginname, (loaders, module) => {
if (module.resource) {
const id = this.formatId(module.resource);
measureResult[id].loaders = loaders;
measureResult[id].load = Date.now();
}
});
moduleHooks.beforeParse.tap(pluginname, (module) => {
if (module.resource) {
const id = this.formatId(module.resource);
measureResult[id].parse = Date.now();
}
});
moduleHooks.beforeSnapshot.tap(pluginname, (module) => {
if (module.resource) {
const id = this.formatId(module.resource);
measureResult[id].snapshot = Date.now();
}
});
compilation.hooks.succeedModule.tap(pluginname, (module) => {
if (module.resource) {
const id = this.formatId(module.resource);
measureResult[id].done = Date.now();
}
});
// structure and emit
compilation.hooks.processAssets.tap(pluginname, () => {
pushLifetime('process assets');
const formated = Object.entries(measureResult).map(i => ({
id: i[0], loaders: i[1].loaders, build: i[1].done - i[1].load, load: i[1].parse - i[1].load, parse: i[1].snapshot - i[1].parse, snapshot: i[1].done - i[1].snapshot,
})).sort((a, b) => b.build - a.build);
compilation.emitAsset('measureResult.html', new RawSource(htmltpl
.replace('{measureResult}', JSON.stringify(measureResult))
.replace('{formated}', JSON.stringify(formated))
.replace('{lifetime}', JSON.stringify(lifetime))));
});
// seal
compilation.hooks.seal.tap(pluginname, () => {
pushLifetime('seal start');
});
compilation.hooks.beforeChunkAssets.tap(pluginname, () => {
pushLifetime('create assets start');
});
});
// make
compiler.hooks.make.intercept({
name: pluginname,
call() {
pushLifetime('make start');
},
done() {
pushLifetime('make end');
},
});
compiler.cache.hooks.beginIdle.intercept({
name: pluginname,
done() {
console.log('\x1B[32m', '请访问 /measureResult.html 查看分析结果');
},
});
}
}
module.exports = MeasureWebpackPlugin;