-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ts
51 lines (44 loc) · 1.12 KB
/
build.ts
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
import fs from "node:fs";
import path from "node:path";
import esbuild from "esbuild";
import type { Format, BuildOptions } from "esbuild";
const extensionPatterns = [
// Standard .ts & .tsx files
/^.+\.test\.tsx?$/gi,
// Stories
/^.+\.stories\.tsx?$/gi,
// Type definitions
/^.+\.d\.ts$/gi,
];
const entryPoints = fs.readdirSync("src").reduce<string[]>((final, each) => {
const ignore = extensionPatterns.map(p => p.test(each)).includes(true);
if (!ignore) {
final.push(path.resolve("src", each));
}
return final;
}, []);
async function build(format: Exclude<Format, "iife">): Promise<void> {
const outdir = `dist/${format}`;
const options: BuildOptions = {
target: ["esnext"],
format,
platform: "browser",
entryPoints,
outdir,
treeShaking: true,
sourcemap: "inline",
};
const result = await esbuild.build(options);
for (const error of result.errors) {
console.error(error.text);
}
for (const warning of result.warnings) {
console.warn(warning.text);
}
if (result.errors.length !== 0) {
process.exit(1);
}
process.exit(0);
}
build("esm");
build("cjs");