-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.ts
More file actions
45 lines (36 loc) · 1.25 KB
/
build.ts
File metadata and controls
45 lines (36 loc) · 1.25 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
/// <reference types='bun-types' />
import { existsSync, rmSync } from "node:fs";
import { join, resolve } from "node:path/posix";
import { transpileDeclaration } from "typescript";
import tsconfig from "../tsconfig.json";
// Constants
const ROOTDIR = resolve(import.meta.dir, "..");
const SOURCEDIR = `${ROOTDIR}/src`;
const OUTDIR = join(ROOTDIR, tsconfig.compilerOptions.declarationDir);
// Remove old content
if (existsSync(OUTDIR)) rmSync(OUTDIR, { recursive: true });
// Transpile files concurrently
const transpiler = new Bun.Transpiler({
loader: "ts",
target: "node",
// Lighter output
minifyWhitespace: true,
treeShaking: true,
});
for (const path of new Bun.Glob("**/*.ts").scanSync(SOURCEDIR)) {
const srcPath = `${SOURCEDIR}/${path}`;
const pathExtStart = path.lastIndexOf(".");
const outPathNoExt = `${OUTDIR}/${path.substring(0, pathExtStart >>> 0)}`;
Bun.file(srcPath)
.text()
.then((buf) => {
const res = transpiler.transformSync(buf);
if (res.length !== 0) {
Bun.write(`${outPathNoExt}.js`, res.replace(/const /g, "let "));
}
Bun.write(
`${outPathNoExt}.d.ts`,
transpileDeclaration(buf, tsconfig as any).outputText
);
});
}