forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-size.mjs
More file actions
64 lines (55 loc) · 1.74 KB
/
Copy pathcheck-size.mjs
File metadata and controls
64 lines (55 loc) · 1.74 KB
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
import { readFile } from 'node:fs/promises';
import { gzipSync } from 'node:zlib';
import { build } from 'esbuild';
const budgets = {
'dist/index.js': 8 * 1024,
'dist/index.cjs': 8 * 1024,
};
let failed = false;
for (const [file, budget] of Object.entries(budgets)) {
const contents = await readFile(file);
const gzipBytes = gzipSync(contents, { level: 9 }).byteLength;
const status = gzipBytes <= budget ? 'PASS' : 'FAIL';
console.log(
`${status} ${file}: ${formatBytes(gzipBytes)} gzip (budget ${formatBytes(budget)})`,
);
failed ||= gzipBytes > budget;
}
// Bundle a consumer that imports one small helper from the package root. This
// catches regressions where the sideEffects contract or a barrel export causes
// the rest of the SDK to be retained by consumers.
const treeShaken = await build({
stdin: {
contents: `
import { resolveLilySdkConfig } from './dist/index.js';
console.log(resolveLilySdkConfig({ apiKey: 'test-key' }));
`,
resolveDir: process.cwd(),
sourcefile: 'tree-shaking-check.mjs',
},
bundle: true,
format: 'esm',
minify: true,
platform: 'node',
target: 'node20',
treeShaking: true,
write: false,
});
const treeShakenBudget = 2 * 1024;
const treeShakenGzipBytes = gzipSync(treeShaken.outputFiles[0].contents, {
level: 9,
}).byteLength;
const treeShakenStatus =
treeShakenGzipBytes <= treeShakenBudget ? 'PASS' : 'FAIL';
console.log(
`${treeShakenStatus} tree-shaken ESM consumer: ${formatBytes(treeShakenGzipBytes)} gzip ` +
`(budget ${formatBytes(treeShakenBudget)})`,
);
failed ||= treeShakenGzipBytes > treeShakenBudget;
if (failed) {
console.error('Bundle size budget exceeded.');
process.exitCode = 1;
}
function formatBytes(bytes) {
return `${bytes} B`;
}