-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.ts
35 lines (31 loc) · 884 Bytes
/
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
import fs from "node:fs/promises";
import * as rolldown from "rolldown";
const output = await rolldown.build({
input: "src/index.ts",
platform: "node",
output: {
minify: true,
},
treeshake: true,
write: false,
});
const isCheck = process.argv.includes("--check");
const mainFile = output.output.find((file) => file.fileName === "index.js");
if (!mainFile) {
throw new Error("index.js not found");
}
if (mainFile.type !== "chunk") {
throw new Error("index.js is not a chunk");
}
const outputPath = `${import.meta.dirname}/dist/index.js`;
if (isCheck) {
const expected = await fs.readFile(outputPath, "utf-8");
if (mainFile.code !== expected) {
console.error("The output is not up-to-date");
process.exit(1);
}
console.log("The output is up-to-date");
} else {
await fs.writeFile(outputPath, mainFile.code);
console.log("Build completed");
}