-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundle.js
72 lines (59 loc) · 1.69 KB
/
bundle.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
const es = require("esbuild");
const fs = require("fs");
const zlib = require("zlib");
const files = [];
es.buildSync({
entryPoints: ["./index.js"],
format: "esm",
outfile: "./index.esm.js",
// target: "es2020",
bundle: true,
legalComments: "none",
treeShaking: true,
minify: true,
});
fs.copyFileSync("./index.d.ts", "./index.esm.d.ts");
// log the file size of bundled file `./browser.js`
const file = __dirname + "/index.esm.js";
files.push(getGzippedSize(file));
// copy index.d.ts to index.esm.d.ts
console.table(files);
/**
* Format bytes as human-readable text.
*
* @param bytes Number of bytes.
* @param si True to use metric (SI) units, aka powers of 1000. False to use
* binary (IEC), aka powers of 1024.
* @param dp Number of decimal places to display.
*
* @return Formatted string.
*/
function humanFileSize(bytes, si = false, dp = 1) {
const thresh = si ? 1000 : 1024;
if (Math.abs(bytes) < thresh) {
return bytes + " B";
}
const units = si
? ["kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
: ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
let u = -1;
const r = 10 ** dp;
do {
bytes /= thresh;
++u;
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
return bytes.toFixed(dp) + " " + units[u];
}
/**
* Get gzip size of file
*/
function getGzippedSize(file) {
const stats = fs.statSync(file);
const gzip = zlib.gzipSync(fs.readFileSync(file));
const gzipSize = gzip.length;
return {
file: file.replace(__dirname + "/", ""),
size: humanFileSize(stats.size),
gzip: humanFileSize(gzipSize)
};
}