-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.js
93 lines (81 loc) · 2.83 KB
/
build.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
import chalk from "chalk";
import { build } from "esbuild";
import { file } from "gzip-size";
import prettyBytes from "pretty-bytes";
let arrPromises, promise, newTime, oldTime;
export default async (source = "src/api.ts", globalName, mode) => {
const fileSize = async (inputFile = "lib/api.es.js") => `${prettyBytes(await file(inputFile))}`;
const outputs = [
{
outfile: "lib/api.es.js",
format: "esm",
target: ["es2020"],
},
{
outfile: "lib/api.js",
format: "iife",
target: ["es2020"],
},
{
outfile: "lib/api.cjs.js",
platform: "node",
target: ["es2020"],
format: "cjs",
},
];
let buildData = {
entryPoints: [source],
color: true,
bundle: true,
minify: true,
sourcemap: false,
globalName,
tsconfig: "./tsconfig.json",
logLevel: "info",
};
// Build code
arrPromises = await Promise.all(
outputs.map((output) => {
return build({
...buildData,
...output,
incremental: true,
});
})
);
newTime = Date.now();
if (mode == "watch") {
const { watch } = await import("chokidar");
const watcher = watch(["src/**/*"]);
console.log(chalk`Watching {gray ${globalName}/${source}} and surrounding files for changes... \n`);
watcher.on("change", () => {
oldTime = Date.now();
(async () => {
let timeStart = Date.now();
for (let i = 0; i < arrPromises.length; i++) {
oldTime = Date.now();
let { outfile } = outputs[i];
promise = arrPromises[i];
arrPromises[i] = await promise.rebuild();
newTime = Date.now();
console.log(chalk`Built {red ${outfile}} in {yellow ${newTime - oldTime}ms}, file size {green ${await fileSize(outfile)}} Gzipped`);
}
let timeEnd = Date.now();
console.log(chalk`Total build time is: {red ${timeEnd - timeStart}ms}\n`);
})();
});
} else {
let timeStart = Date.now();
for (let i = 0; i < arrPromises.length; i++) {
oldTime = Date.now();
let { outfile } = outputs[i];
promise = arrPromises[i];
await promise.rebuild();
promise.rebuild.dispose();
newTime = Date.now();
console.log(chalk`Built {red ${outfile}} in {yellow ${newTime - oldTime}ms}, file size {green ${await fileSize(outfile)}} Gzipped`);
}
let timeEnd = Date.now();
console.log(chalk`Total build time is: {red ${timeEnd - timeStart}ms}`);
}
};