-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathbuild.ts
More file actions
65 lines (55 loc) · 1.71 KB
/
build.ts
File metadata and controls
65 lines (55 loc) · 1.71 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
/// <reference types="bun-types" />
import { generateDtsBundle } from "dts-bundle-generator";
import fs from "fs";
import path from "path";
import { dependencies, peerDependencies } from "./package.json";
const BUN_GLOBALS = path.join(path.dirname(require.resolve("bun-types/package.json")), "globals.d.ts");
const originalGlobals = fs.readFileSync(BUN_GLOBALS, "utf8");
const patchedGlobals = originalGlobals.replace(/import\("node:util"\)\.\w+/g, "any");
if (!fs.existsSync("./build")) {
fs.mkdirSync("./build");
}
const start = Date.now();
console.log("JSCompiling", "Building...");
fs.writeFileSync(BUN_GLOBALS, patchedGlobals);
try {
await Promise.all([
Bun.build({
entrypoints: ["./index.ts"],
external: Object.keys(dependencies).concat(Object.keys(peerDependencies)),
format: "esm",
minify: true,
outdir: "./build",
naming: "index.esm.js",
sourcemap: "external",
target: "browser"
}),
Bun.build({
entrypoints: ["./index.ts"],
external: Object.keys(dependencies).concat(Object.keys(peerDependencies)),
format: "cjs",
minify: true,
outdir: "./build",
naming: "index.cjs",
sourcemap: "external",
target: "node"
})
]);
console.log("JSCompiling", "Done!");
console.log("TypeCompiling", "Building...");
const typedContent = generateDtsBundle(
[
{
filePath: "./index.ts"
}
],
{
preferredConfigPath: "./tsconfig.build.json"
}
);
fs.writeFileSync("./build/index.d.ts", typedContent.join("\n"));
console.log("TypeCompiling", "Done!");
console.log("Build", `Build success, take ${Date.now() - start}ms`);
} finally {
fs.writeFileSync(BUN_GLOBALS, originalGlobals);
}