-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ts
More file actions
146 lines (131 loc) · 3.89 KB
/
build.ts
File metadata and controls
146 lines (131 loc) · 3.89 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import dts from "bun-plugin-dts";
import { writeFileSync, readFileSync, readdirSync, existsSync } from "fs";
import { resolve, relative } from "path";
import { execSync } from "child_process";
import type { BuildConfig } from "bun";
import { join, relative } from "path";
const baseDir = "./src";
const typesDir = resolve(baseDir, "types");
const classesDir = resolve(baseDir, "classes");
const wrappersDir = resolve(baseDir, "wrappers");
generateIndexes(baseDir, [
{ dir: "types", isTypes: true },
{ dir: "classes" },
{ dir: "wrappers" },
]);
// Get Git commit count
const count = parseInt(
execSync("git rev-list --count HEAD").toString().trim(),
10
);
// Compute MAJOR.MINOR.PATCH
const MAJOR = Math.floor(count / 10000);
const MINOR = Math.floor((count / 100) % 100);
const PATCH = count % 100;
const version = `${MAJOR}.${MINOR}.${PATCH}`;
// Update package.json
const pkgPath = "./package.json";
const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
pkg.version = version;
writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n");
const minify = true;
const formats: Array<BuildConfig["format"]> = ["cjs", "esm"];
for (const format of formats) {
Bun.build({
entrypoints: ["./src/index.ts"],
outdir: `./dist/`,
minify: minify,
target: "browser",
format: format,
plugins: [dts()],
splitting: true,
naming: {
entry: `[name].${format}.js`,
chunk: "[name]-[hash].js",
asset: "[name]-[hash][ext]",
},
}).catch((err) => {
console.error(err);
process.exit(1);
});
}
type IndexConfig = {
dir: string;
isTypes?: boolean;
subDirs?: string[];
};
function generateIndexes(baseDir: string, configs: IndexConfig[]) {
function walk(dir: string): string[] {
let results: string[] = [];
for (const file of readdirSync(dir, { withFileTypes: true })) {
const fullPath = join(dir, file.name);
if (file.isDirectory()) {
results = results.concat(walk(fullPath));
} else {
results.push(fullPath);
}
}
return results;
}
let baseExports: string[] = [];
for (const { dir, isTypes = false, subDirs = [] } of configs) {
const absDir = join(baseDir, dir);
const files = walk(absDir);
const imports = files
.map((file: string) => {
if (file.endsWith("index.ts")) return;
if (file.endsWith(".ts")) {
let relPath =
"./" +
relative(absDir, file).replace(/\\/g, "/").replace(/\.ts$/, "");
return `export ${isTypes ? "type " : ""}* from "${relPath}";`;
}
})
.filter(Boolean)
.join("\n");
const indexFileName = `index.ts`;
writeFileSync(join(absDir, indexFileName), `/**
* Automatically generated by the bundler. DO NOT EDIT!
*/\n
${imports}`);
// Add export from this index to baseDir index
const relPathToBase =
"./" +
relative(baseDir, join(absDir, indexFileName))
.replace(/\\/g, "/")
.replace(/\.ts$/, "");
baseExports.push(
`export ${isTypes ? "type " : ""}* from "${relPathToBase}";`
);
if (subDirs.length > 0) {
let exports: string[] = [];
subDirs.forEach((subDir) => {
const indexPath = join(baseDir, subDir, "index.ts");
if (existsSync(indexPath)) {
const relPath =
"./" +
relative(absDir, indexPath)
.replace(/\\/g, "/")
.replace(/\.ts$/, "");
exports.push(`export ${isTypes ? "type " : ""}* from "${relPath}";`);
}
});
if (exports.length > 0) {
writeFileSync(
join(absDir, "index.ts"),
`/**
* Automatically generated by the bundler. DO NOT EDIT!
*/\n
${exports.join("\n")}`
);
}
}
}
// Write index.ts in baseDir
if (baseExports.length > 0) {
writeFileSync(join(baseDir, "index.ts"), `/**
* Automatically generated by the bundler. DO NOT EDIT!
*/\n
${baseExports.join("\n")}`);
}
}