-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ts
More file actions
57 lines (50 loc) · 1.75 KB
/
Copy pathbuild.ts
File metadata and controls
57 lines (50 loc) · 1.75 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
import { existsSync } from "node:fs";
import { cp, rm } from "node:fs/promises";
import path from "node:path";
import tailwind from "bun-plugin-tailwind";
const outdir = path.join(process.cwd(), "dist");
const publicDir = path.join(process.cwd(), "public");
await rm(outdir, { recursive: true, force: true });
const entrypoints = [...new Bun.Glob("src/**/*.html").scanSync()];
const result = await Bun.build({
entrypoints,
outdir,
plugins: [tailwind],
minify: true,
target: "browser",
sourcemap: "none",
splitting: true,
define: {
"process.env.NODE_ENV": JSON.stringify("production"),
},
});
for (const output of result.outputs) {
console.log(
` ${path.relative(process.cwd(), output.path)} ${(output.size / 1024).toFixed(1)} KB`,
);
}
if (existsSync(publicDir)) {
await cp(publicDir, outdir, { recursive: true });
console.log(` copied public assets -> ${path.relative(process.cwd(), outdir)}`);
}
const contentSrc = path.join(process.cwd(), "src/content");
const contentOut = path.join(outdir, "content");
const mdFiles = [...new Bun.Glob("**/*.md").scanSync({ cwd: contentSrc })];
for (const file of mdFiles) {
const src = path.join(contentSrc, file);
const dest = path.join(contentOut, file);
await Bun.write(dest, Bun.file(src));
}
console.log(
` copied ${mdFiles.length} content files -> ${path.relative(process.cwd(), contentOut)}`,
);
const basePath = process.env.BASE_PATH;
if (basePath) {
const indexPath = path.join(outdir, "index.html");
let html = await Bun.file(indexPath).text();
html = html.replace("<head>", `<head><base href="${basePath}/">`);
await Bun.write(indexPath, html);
await Bun.write(path.join(outdir, "404.html"), html);
console.log(` injected <base href="${basePath}/">`);
console.log(` created 404.html for SPA routing`);
}