-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.html
265 lines (212 loc) · 7 KB
/
client.html
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>Package sizes</title>
<style>
html,
body {
background: radial-gradient(ellipse at center, #ffe6ad 0%, #c98e18 100%);
height: 100%;
width: 100%;
font-family: Arial;
font-size: 14px;
}
svg {
font-family: Sans-serif;
font-size: 10px;
}
aside {
position: fixed;
}
.slice {
cursor: pointer;
}
.slice .main-arc {
stroke: #fff;
stroke-width: 1px;
}
.slice .hidden-arc {
fill: none;
}
.slice text {
pointer-events: none;
dominant-baseline: middle;
text-anchor: middle;
}
</style>
</head>
<body>
<aside>
<h3>Code size breakdown by bundle (via source maps)</h3>
<p>
<select id="bundles"></select>
</p>
<p>Click on colored slices to zoom in. Click the middle circle to zoom out.</p>
<p>
<a href="https://github.com/lhorie/bundle-analyzer#ways-to-decrease-file-size">Click here to read about ways to reduce file size</a>
</p>
</aside>
<script src='https://d3js.org/d3.v4.min.js'></script>
<script>
// data
let options = {}
let [
selected = '',
x0 = 0,
x1 = 1,
y0 = 0,
y1 = 1
] = location.hash
.slice(1)
.split('/')
.filter(s => s !== '')
.map((s, i) => i === 0 ? s : Number(s));
let data;
// controller
function unhash(s) {
return s.replace(/-[a-f0-9]+?\.js/, '');
}
const source = new EventSource("/_sse");
source.addEventListener("message", function (event) {
data = JSON.parse(event.data);
bundles.textContent = '';
options = {};
data.forEach((d, i) => {
const option = document.createElement('option');
const name = option.textContent = option.value = unhash(d.name);
options[name] = i;
if (name === selected) option.selected = true;
bundles.appendChild(option);
});
if (!options[selected]) selected = unhash(data[0].name);
redraw(data, selected);
}, false);
const bundles = document.getElementById('bundles');
bundles.addEventListener('change', e => {
selected = e.target.value;
x0 = y0 = 0;
x1 = y1 = 1;
redraw(data, selected);
});
// view
function redraw(data, selected) {
history.pushState(null, null, `#${selected}/${x0}/${x1}/${y0}/${y1}`);
let root = data[options[selected]];
const width = window.innerWidth,
height = window.innerHeight,
maxRadius = (Math.min(width, height) / 2) - 5;
const formatNumber = d3.format(',d');
const x = d3.scaleLinear()
.range([0, 2 * Math.PI])
.clamp(true);
const y = d3.scaleSqrt()
.range([maxRadius * .1, maxRadius]);
const color = d3.scaleOrdinal(d3.schemeCategory20);
const partition = d3.partition();
const arc = d3.arc()
.startAngle(d => x(d.x0))
.endAngle(d => x(d.x1))
.innerRadius(d => Math.max(0, y(d.y0)))
.outerRadius(d => Math.max(0, y(d.y1)));
const middleArcLine = d => {
const halfPi = Math.PI / 2;
const angles = [x(d.x0) - halfPi, x(d.x1) - halfPi];
const r = Math.max(0, (y(d.y0) + y(d.y1)) / 2);
const middleAngle = (angles[1] + angles[0]) / 2;
const invertDirection = middleAngle > 0 && middleAngle < Math.PI; // On lower quadrants write text ccw
if (invertDirection) { angles.reverse(); }
const path = d3.path();
path.arc(0, 0, r, angles[0], angles[1], invertDirection);
return path.toString();
};
const textFits = d => {
const CHAR_SPACE = 4;
const deltaAngle = x(d.x1) - x(d.x0);
const r = Math.max(0, (y(d.y0) + y(d.y1)) / 2);
const perimeter = r * deltaAngle;
return label(d).length * CHAR_SPACE < perimeter;
};
d3.select("svg").remove();
const svg = d3.select('body').append('svg')
.style('width', '100vw')
.style('height', '100vh')
.attr('viewBox', `${-width / 2} ${-height / 2} ${width} ${height}`)
.on('click', () => focusOn({ x0, x1, y0, y1 })); // Reset zoom on canvas click
root = d3.hierarchy(root);
root.sum(d => d.size);
const slice = svg.selectAll('g.slice')
.data(partition(root).descendants());
slice.exit().remove();
const newSlice = slice.enter()
.append('g').attr('class', 'slice')
.on('click', d => {
d3.event.stopPropagation();
focusOn(d);
});
newSlice.append('title')
.text(label);
newSlice.append('path')
.attr('class', 'main-arc')
.style('fill', d => color((d.children ? d : d.parent).data.name))
.attr('d', arc);
newSlice.append('path')
.attr('class', 'hidden-arc')
.attr('id', (_, i) => `hiddenArc${i}`)
.attr('d', middleArcLine);
const text = newSlice.append('text')
.attr('display', d => textFits(d) ? null : 'none');
// Add white contour
text.append('textPath')
.attr('startOffset', '50%')
.attr('xlink:href', (_, i) => `#hiddenArc${i}`)
.text(label)
.style('fill', 'none')
.style('stroke', '#fff')
.style('stroke-width', 2)
.style('stroke-linejoin', 'round');
text.append('textPath')
.attr('startOffset', '50%')
.attr('xlink:href', (_, i) => `#hiddenArc${i}`)
.text(label);
function focusOn(d = { x0: 0, x1: 1, y0: 0, y1: 1 }) {
// Reset to top-level if no data point specified
({ x0 = 0, x1 = 1, y0 = 0, y1 = 1, duration = 500 } = d);
history.pushState(null, null, `#${selected}/${x0}/${x1}/${y0}/${y1}`);
const transition = svg.transition()
.duration(duration)
.tween('scale', () => {
const xd = d3.interpolate(x.domain(), [d.x0, d.x1]),
yd = d3.interpolate(y.domain(), [d.y0, 1]);
return t => { x.domain(xd(t)); y.domain(yd(t)); };
});
transition.selectAll('path.main-arc')
.attrTween('d', d => () => arc(d));
transition.selectAll('path.hidden-arc')
.attrTween('d', d => () => middleArcLine(d));
transition.selectAll('text')
.attrTween('display', d => () => textFits(d) ? null : 'none');
moveStackToFront(d);
//
function moveStackToFront(elD) {
svg.selectAll('.slice').filter(d => d === elD)
.each(function (d) {
this.parentNode.appendChild(this);
if (d.parent) { moveStackToFront(d.parent); }
})
}
}
function label(d) {
return `${d.data.name} - ${format(sizeOf(d.data))}`;
}
function sizeOf(data) {
return data.size || data.children.reduce((n, c) => n + sizeOf(c), 0);
}
function format(n) {
return n.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
}
focusOn({ x0, x1, y0, y1, duration: 0 });
};
</script>
</body>
</html>