-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundle.js
114 lines (95 loc) · 2.77 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
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
const es = require("esbuild");
const fs = require("fs");
const zlib = require("zlib");
const files = [];
es.buildSync({
entryPoints: ["./index.ts"],
format: "iife",
outfile: "./browser.min.js",
target: "es6",
bundle: true,
legalComments: "none",
minify: true,
external: ["joi", "yup"],
globalName: "AbolishBrowser"
});
es.buildSync({
entryPoints: ["./index.ts"],
format: "esm",
outfile: "./esm.js",
// target: "es2020",
bundle: true,
legalComments: "none",
treeShaking: true,
minify: true,
external: ["joi", "yup"]
});
// copy `index.d.ts` to `index.esm.d.ts`
fs.copyFileSync(__dirname + "/index.d.ts", __dirname + "/esm.d.ts");
// log the file size of bundled file `./browser.js`
const file = __dirname + "/browser.min.js";
files.push(getGzippedSize(file));
const fileEsm = __dirname + "/esm.js";
files.push(getGzippedSize(fileEsm));
// copy index.d.ts to index.esm.d.ts
const folder = __dirname + `/validators`;
let validatorFolders = ["array", "date", "object", "string", "utils", "number"];
for (const f of validatorFolders) {
const from = folder + `/${f}/index.ts`;
const to = folder + `/${f}/index.min.js`;
const ff = f[0].toUpperCase() + f.slice(1);
const name = `Abolish${ff}Validators`;
es.buildSync({
entryPoints: [from],
target: "es6",
legalComments: "none",
format: "iife",
outfile: to,
bundle: true,
minify: true,
external: ["joi", "abolish", "yup"],
globalName: name
});
// log the file size of bundled file.
files.push(getGzippedSize(to));
}
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)
};
}