-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrun.js
61 lines (53 loc) · 1.7 KB
/
run.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
import { dirname } from "path";
import { fileURLToPath } from "url";
import { writeFileSync } from "fs";
import { mkdir } from "fs/promises";
import * as esbuild from "esbuild";
import * as terser from "terser";
import { gzipSync, brotliCompressSync } from "zlib";
const __dirname = dirname(fileURLToPath(import.meta.url));
const buildSuite = async (suite) => {
const outfile = `${__dirname}/dist/${suite}.js`;
const res = esbuild.buildSync({
entryPoints: [`${__dirname}/${suite}.ts`],
outfile,
bundle: true,
minify: false,
write: false,
});
if (res.errors.length) {
console.error(res.errors);
process.exit(1);
}
const rawText = res.outputFiles[0].text;
writeFileSync(outfile, rawText);
const text = (await terser.minify(rawText, {})).code;
writeFileSync(outfile.replace(".js", ".min.js"), text);
return {
rawSize: text.length,
gzipSize: gzipSync(text).length,
brotliSize: brotliCompressSync(text).length,
};
};
function diff(baseline, checked) {
return {
rawSize: checked.rawSize - baseline.rawSize,
gzipSize: checked.gzipSize - baseline.gzipSize,
brotliSize: checked.brotliSize - baseline.brotliSize,
};
}
async function runSuite(dir) {
await mkdir(`${__dirname}/dist/${dir}`).catch(() => {});
const baseline = await buildSuite(`${dir}/raw`);
console.log(`\n${dir}\n---`);
console.log("\nbaseline", baseline);
for (const suite of ["zod", "superstruct", "banditypes"]) {
const stats = await buildSuite(`${dir}/${suite}`);
console.log(`${suite}\n `, stats);
console.log(`${suite}:diff\n `, diff(baseline, stats));
}
}
await mkdir(`${__dirname}/dist`).catch(() => {});
runSuite("full");
runSuite("basic");
runSuite("minimal");