Remove cleanOldFiles function and unlink dist directory at build start
This commit is contained in:
parent
e9b90eabc4
commit
ae63818fe1
1 changed files with 1 additions and 24 deletions
25
index.ts
25
index.ts
|
@ -1,14 +1,10 @@
|
|||
import { unlink } from "node:fs/promises";
|
||||
import { watch } from "node:fs";
|
||||
import { Glob } from "bun";
|
||||
import type { BuildArtifact } from "bun";
|
||||
import { join } from "node:path";
|
||||
|
||||
const glob = new Glob("**/*");
|
||||
|
||||
const NOWATCH = process.env.NOWATCH === "1";
|
||||
|
||||
const build = async () => {
|
||||
try { await unlink("dist"); } catch {}
|
||||
const start = Date.now();
|
||||
const built = await Bun.build({
|
||||
entrypoints: ["./src/index.html"],
|
||||
|
@ -20,29 +16,10 @@ const build = async () => {
|
|||
...(NOWATCH ? { minify: true } : {}),
|
||||
});
|
||||
const end = Date.now();
|
||||
if (!NOWATCH) await cleanOldFiles(built.outputs);
|
||||
console.log("Did build", built.success, built.logs);
|
||||
console.log("Build time:", end - start, "ms");
|
||||
};
|
||||
|
||||
const cleanOldFiles = async (outputs: BuildArtifact[]) => {
|
||||
let distFiles: string[] = [];
|
||||
for await (const file of glob.scan("./dist")) {
|
||||
distFiles.push(file);
|
||||
}
|
||||
|
||||
for await (const output of outputs) {
|
||||
const arr = output.path.split("\\");
|
||||
const file = arr[arr.length - 1];
|
||||
|
||||
distFiles = distFiles.filter((item) => item !== file);
|
||||
}
|
||||
|
||||
return await Promise.all(
|
||||
Array.from(distFiles).map((file) => unlink(join("dist", file))),
|
||||
);
|
||||
};
|
||||
|
||||
if (!NOWATCH) {
|
||||
watch(import.meta.dir, { recursive: true }, async (_, file) => {
|
||||
if (!file?.startsWith("dist")) return await build();
|
||||
|
|
Loading…
Add table
Reference in a new issue