forked from clerk/javascript
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundle-check.mjs
257 lines (219 loc) · 7.31 KB
/
bundle-check.mjs
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
//@ts-check
import fs from 'node:fs';
import http from 'node:http';
import path from 'node:path';
import { pipeline } from 'node:stream';
import zlib from 'node:zlib';
import { chromium } from 'playwright';
/**
* This script generates a CLI report detailing the gzipped size of JavaScript resources loaded by `clerk-js` for a
* given configuration. This is useful to ensure that the total amount of loaded JavaScript does not exceed the
* anticipated amount for a particular invocation.
*
* Note: The publishable key embedded in this script isn't anything special; any publishable key will do, so feel free
* to replace it with another key should the current one stop working.
*/
/**
* The base HTML file that each case will be executed against. Loads `clerk-js` from the local server in addition to
* creating an instance of `VirtualRouter`.
* @param {string} script The case-specific code to execute, typically to mount a component like `SignIn`.
*/
function template(script) {
return `
<!DOCTYPE html>
<html>
<body>
<div id="app"></div>
<script
type="text/javascript"
src="/clerk.browser.js"
data-clerk-publishable-key="pk_test_Zmx1ZW50LWxhYnJhZG9yLTM0LmNsZXJrLmFjY291bnRzLmRldiQ"
></script>
<script type="text/javascript">
class VirtualRouter {
name = 'VirtualRouter';
mode = 'virtual';
#url;
#listeners = new Set();
constructor(path) {
const origin = typeof window === 'undefined' ? 'https://clerk.dummy' : window.location.origin;
this.#url = new URL(path ?? '/', origin);
}
push(path) {
const newUrl = new URL(this.#url.toString());
newUrl.pathname = path;
this.#url = newUrl;
this.emit();
}
replace(path) {
this.push(path);
}
shallowPush(path) {
this.push(path);
}
pathname() {
return this.#url.pathname;
}
searchParams() {
return this.#url.searchParams;
}
subscribe(listener) {
this.#listeners.add(listener);
return () => this.#listeners.delete(listener);
}
emit() {
this.#listeners.forEach(listener => listener(this.#url));
}
getSnapshot() {
return this.#url;
}
}
window.VIRTUAL_ROUTER = new VirtualRouter('/');
</script>
<script type="text/javascript">${script}</script>
</body>
</html>
`;
}
function signIn() {
const script = `
window.Clerk.load({ router: window.VIRTUAL_ROUTER }).then(() => {
window.Clerk.mountSignIn(document.getElementById("app"), {});
});
`;
return template(script);
}
function signUp() {
const script = `
window.Clerk.load({ router: window.VIRTUAL_ROUTER }).then(() => {
window.Clerk.mountSignUp(document.getElementById("app"), {});
});
`;
return template(script);
}
/**
* Map of URL routes to functions that return the HTML content of the page. This script will generate a report for each
* of the routes defined here.
*/
const routes = {
'/sign-in': signIn(),
'/sign-up': signUp(),
};
const SERVER_ROOT = path.resolve('./dist');
const server = http
.createServer((req, res) => {
const onError = err => {
if (err) {
res.end();
console.error(err);
}
};
// This should never happen, and is only here to appease TypeScript. `req.url` is always defined for incoming
// messages received by the HTTP server.
if (!req.url) {
throw new Error('Unable to determine URL from request.');
}
if (req.url in routes) {
res.writeHead(200, { 'content-type': 'text/html' });
res.end(routes[req.url]);
return;
}
const filePath = path.resolve(SERVER_ROOT, `.${req.url}`);
// This is here to prevent GitHub from complaining about a security vulnerability.
if (!filePath.startsWith(SERVER_ROOT)) {
res.writeHead(403, { 'content-type': 'text/plain' });
res.end('403 Forbidden\n');
console.error(`Attempted to access ${filePath}, which is outsite of SERVER_ROOT directory ${SERVER_ROOT}.`);
return;
}
const extname = path.extname(filePath);
if (fs.existsSync(filePath) && (extname === '.js' || extname === '.css')) {
const contentType = extname === '.js' ? 'text/javascript' : 'text/css';
res.writeHead(200, { 'content-encoding': 'gzip', 'content-type': contentType, vary: 'Accept-Encoding' });
// We specifically use gzip here since that's the bundle size we really care about.
pipeline(fs.createReadStream(filePath), zlib.createGzip(), res, onError);
} else {
res.writeHead(404, { 'content-type': 'text/plain' });
res.end('404 Not Found\n');
}
})
.listen(4000);
const byteFormatter = Intl.NumberFormat('en', {
notation: 'compact',
style: 'unit',
unit: 'byte',
unitDisplay: 'narrow',
});
/**
* Format bytes into a human-readable string with appropriate units
* @param {number} bytes
*/
function formatFileSize(bytes) {
return byteFormatter.format(bytes);
}
/**
* Generate and print a table detailing the scripts loaded and their response sizes.
* @param {string} url
* @param {{ url: string; sizes: { responseBodySize: number; }; }[]} responses
*/
function report(url, responses) {
// Start with a new line for readability
console.log('\n' + url);
// We only care about JavaScript files loaded from localhost. This removes stuff like favicon and API calls.
const matchingFiles = responses.filter(r => {
return r.url.startsWith('http://localhost:4000') && r.url.endsWith('.js');
});
const data = Object.fromEntries(
matchingFiles.map(r => {
const [, path] = r.url.split('4000/');
return [path, { size: formatFileSize(r.sizes.responseBodySize) }];
}),
);
// Calculate a total size of all matching resources.
data['(total)'] = {
size: formatFileSize(
matchingFiles.reduce((a, b) => {
return a + b.sizes.responseBodySize;
}, 0),
),
};
console.table(data);
}
/**
* Loads the given `url` in `browser`, capturing all HTTP requests that occur.
* @param {import('playwright').Browser} browser
* @param {string} url
*/
async function getResponseSizes(browser, url) {
const page = await browser.newPage();
/** @type {Promise<{ url: string, sizes: { responseBodySize: number } }>[]} */
const promises = [];
page.on('response', res => {
promises.push(
res
.request()
.sizes()
.then(sizes => {
return { url: res.url(), sizes };
}),
);
return res;
});
await page.goto(`http://localhost:4000${url}`);
// Instead of waiting for a specific element, we simply wait for the network to be idle. This is because there aren't
// any elements that exist reliably in every test case.
await page.waitForLoadState('networkidle');
const sizes = await Promise.all(promises);
await page.close();
return sizes;
}
(async () => {
const browser = await chromium.launch();
const testUrls = Object.keys(routes);
for (const url of testUrls) {
const sizes = await getResponseSizes(browser, url);
report(url, sizes);
}
await browser.close();
server.close();
})();