Skip to content

Commit 174ae3e

Browse files
committed
refactor(bundle): update scripts and split css
1 parent 12909fc commit 174ae3e

File tree

19 files changed

+342
-219
lines changed

19 files changed

+342
-219
lines changed

bun.lock

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"lockfileVersion": 0,
3+
"workspaces": {
4+
"": {
5+
"devDependencies": {
6+
"@types/bun": "^1.1.13",
7+
"mini-van-plate": "^0.6.1",
8+
},
9+
},
10+
},
11+
"packages": {
12+
"@types/bun": ["@types/[email protected]", "", { "dependencies": { "bun-types": "1.1.43" } }, "sha512-E+ue6NMcn4FXC5bDRE1W/BXUVs01h5Mt02qH8/8HGCox9akuh8KNOFdwvaQS9TDgT2RmUyJYFRRqA60WtTnm2g=="],
13+
14+
"@types/node": ["@types/[email protected]", "", { "dependencies": { "undici-types": "~5.26.4" } }, "sha512-scnD59RpYD91xngrQQLGkE+6UrHUPzeKZWhhjBSa3HSkwjbQc38+q3RoIVEwxQGRw3M+j5hpNAM+lgV3cVormg=="],
15+
16+
"@types/ws": ["@types/[email protected]", "", { "dependencies": { "@types/node": "*" } }, "sha512-osM/gWBTPKgHV8XkTunnegTRIsvF6owmf5w+JtAfOw472dptdm0dlGv4xCt6GwQRcC2XVOvvRE/0bAoQcL2QkA=="],
17+
18+
"bun-types": ["[email protected]", "", { "dependencies": { "@types/node": "~20.12.8", "@types/ws": "~8.5.10" } }, "sha512-W0wCtVH+bwFp7p3Zgs03CqxEDmXxEvmmUM/FBKgWIv9T8gyeotvIjIbHzuDScc2DphhRNtr7hJLCR5PspYL5qw=="],
19+
20+
"mini-van-plate": ["[email protected]", "", {}, "sha512-i4kCD5xzkdtQiVnBSN4xFHru6QGPPWJALAm4IB0IRPawIHUK5J/Zv2bx94Nev1YABKN1zJgwxqvuSygoltrQIg=="],
21+
22+
"undici-types": ["[email protected]", "", {}, "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA=="],
23+
}
24+
}

bun.lockb

-3.01 KB
Binary file not shown.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
"type": "module",
55
"scripts": {
66
"fix": "biome check --fix --unsafe",
7-
"build": "bun scripts/ssg.ts",
8-
"build:watch": "bun --watch scripts/ssg.ts",
9-
"preview": "bun build:watch | bun scripts/preview.ts"
7+
"dev": "bun scripts/dev.ts",
8+
"preview": "bun scripts/dev.ts --preview",
9+
"background": "bun scripts/background.ts",
10+
"build": "bun scripts/ssg.ts"
1011
},
1112
"devDependencies": {
1213
"@types/bun": "^1.1.13",

scripts/background.ts

Lines changed: 12 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,15 @@
1-
import van from "mini-van-plate/van-plate";
1+
import { registerMiniVan } from "~/utils/van";
22

3-
const colors = {
4-
bg: "transparent",
5-
primary: "#BD93F9",
6-
secondary: "#FF79C6",
7-
} as const;
3+
async function main() {
4+
await generateBackground();
5+
}
86

9-
const size = {
10-
width: 256,
11-
height: 144,
12-
};
7+
export async function generateBackground() {
8+
await registerMiniVan();
9+
const { Background } = await import("~/components/background.js");
10+
await Bun.write("src/generated/background.svg", Background().render());
11+
}
1312

14-
const circles = {
15-
sizeA: 60,
16-
sizeB: 46,
17-
sizeC: 38,
18-
sizeD: 30,
19-
} as const;
20-
21-
export const Background = (props?: Record<string, unknown>) => {
22-
const { svg, circle, path, defs, linearGradient, stop, g } = van.tags(
23-
"http://www.w3.org/2000/svg",
24-
);
25-
26-
return svg(
27-
{
28-
xmlns: "http://www.w3.org/2000/svg",
29-
width: size.width,
30-
height: size.height,
31-
viewBox: `0 0 ${size.width} ${size.height}`,
32-
...props,
33-
},
34-
defs(
35-
linearGradient(
36-
{ id: "bg-grad1", x1: "0%", y1: "100%", x2: "100%", y2: "0%" },
37-
stop({ offset: "0%", "stop-color": colors.primary }),
38-
stop({ offset: "100%", "stop-color": colors.secondary }),
39-
),
40-
),
41-
g(
42-
{
43-
transform: `translate(${size.width / 2},${size.height / 2})`,
44-
opacity: 0.4,
45-
},
46-
circle({ r: circles.sizeA, fill: `${colors.primary}2F` }),
47-
path({
48-
d: `M0,-${circles.sizeA} a1,1 0 0,1 0,${circles.sizeA * 2}Z`,
49-
transform: "rotate(30 0,0)",
50-
fill: `${colors.primary}2F`,
51-
}),
52-
circle({
53-
r: (circles.sizeB + circles.sizeC) / 2,
54-
fill: "none",
55-
stroke: `${colors.primary}7F`,
56-
"stroke-width": circles.sizeB - circles.sizeC,
57-
}),
58-
circle({
59-
r: circles.sizeC,
60-
fill: "url(#bg-grad1)",
61-
transform: "rotate(180 0,0)",
62-
opacity: 0.8,
63-
}),
64-
circle({ r: circles.sizeD, fill: "url(#bg-grad1)" }),
65-
),
66-
);
67-
};
13+
if (import.meta.main) {
14+
main();
15+
}

scripts/dev.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { watch } from "node:fs";
2+
import { parseArgs } from "node:util";
3+
import type { BunFile } from "bun";
4+
5+
import { SRC_MAP } from "./ssg.js";
6+
7+
function watchCache(): void {
8+
function watchSrcAndBuild(src: string, buildFn: () => Promise<unknown>) {
9+
let time: Timer | undefined;
10+
return watch(src, { recursive: true }, () => {
11+
if (!time) clearTimeout(time);
12+
time = setTimeout(async () => {
13+
await buildFn();
14+
console.log(`Rebuilt ${src}`);
15+
}, 500);
16+
});
17+
}
18+
watchSrcAndBuild(
19+
SRC_MAP.script,
20+
async () => await Bun.$`bun scripts/ssg.ts --target script`,
21+
);
22+
watchSrcAndBuild(
23+
SRC_MAP.style,
24+
async () => await Bun.$`bun scripts/ssg.ts --target style`,
25+
);
26+
watchSrcAndBuild(
27+
SRC_MAP.main,
28+
async () => await Bun.$`bun scripts/ssg.ts --target sites`,
29+
);
30+
watchSrcAndBuild(
31+
SRC_MAP.public,
32+
async () => await Bun.$`bun scripts/ssg.ts --target public`,
33+
);
34+
}
35+
36+
async function main() {
37+
const { values } = parseArgs({
38+
args: Bun.argv,
39+
options: {
40+
preview: { type: "boolean" },
41+
port: { type: "string" },
42+
},
43+
strict: true,
44+
allowPositionals: true,
45+
});
46+
47+
await Bun.$`bun scripts/ssg.ts`;
48+
if (!values.preview) {
49+
watchCache();
50+
}
51+
52+
const server = Bun.serve({
53+
port: values.port ?? 5000,
54+
async fetch(req) {
55+
const url = new URL(req.url);
56+
57+
const file = await getPreviewFile(url.pathname);
58+
if (file) return new Response(file);
59+
60+
return new Response(await getPreviewFile("/404.html"), { status: 404 });
61+
},
62+
});
63+
64+
console.log(`Try visiting the server via http://localhost:${server.port}`);
65+
}
66+
67+
async function getPreviewFile(pathname: string): Promise<BunFile | null> {
68+
const trimedPath = pathname.replace(/^\/|\/$/g, "");
69+
70+
const file = Bun.file(`dist/${trimedPath}`);
71+
if (await file.exists()) return file;
72+
73+
const indexFile = Bun.file(`dist/${trimedPath}/index.html`);
74+
if (await indexFile.exists()) return indexFile;
75+
76+
return null;
77+
}
78+
79+
if (import.meta.main) {
80+
main();
81+
}

scripts/preview.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

scripts/ssg.ts

Lines changed: 81 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,86 @@
1+
import { parseArgs } from "node:util";
12
import { readFileSync } from "node:fs";
23
import { readdir } from "node:fs/promises";
3-
import { Background } from "./background.js";
44

55
import { minifyGlsl } from "~/utils/minify.js";
6+
import { registerMiniVan } from "~/utils/van.js";
7+
import "~/index.js";
68

7-
// Imports for watch only
8-
import "~/scripts/cool-cursor.js";
9-
import "~/styles/core.css";
10-
import "~/styles/resume.css";
11-
import "~/styles/footer.css";
12-
import "~/styles/header.css";
13-
import "~/styles/icon.css";
14-
import "~/styles/landing.css";
15-
import "~/styles/lazy-landing.css";
9+
export const SRC_MAP = {
10+
script: "src/scripts",
11+
style: "src/styles",
12+
main: "src",
13+
public: "public",
14+
} as const;
1615

17-
import "~/scripts/landing.js" with { type: "text" };
16+
const CACHE_MAP = {
17+
script: ".cache/scripts",
18+
style: ".cache/styles",
19+
} as const;
1820

19-
// Init dist folder
20-
await Bun.$`rm -rf dist`;
21+
export async function main() {
22+
const { values } = parseArgs({
23+
args: Bun.argv,
24+
options: {
25+
target: { type: "string" },
26+
},
27+
strict: true,
28+
allowPositionals: true,
29+
});
30+
if (values.target === "script") {
31+
await initOutput();
32+
await cacheScripts();
33+
} else if (values.target === "style") {
34+
await initOutput();
35+
await cacheStyles();
36+
} else if (values.target === "public") {
37+
await initOutput();
38+
await copyPublic();
39+
} else if (values.target === "sites") {
40+
await initOutput();
41+
await generateSites();
42+
} else {
43+
await cleanOutput();
44+
45+
await initOutput();
46+
47+
await cacheStyles();
48+
await cacheScripts();
2149

22-
// Build Clients
23-
await Bun.write("dist/assets/background.svg", Background().render());
24-
await Promise.all([buildScripts(), buildStyles()]);
25-
await Bun.$`mkdir -p dist`;
26-
await Bun.$`cp -rf .cache/scripts dist`;
27-
// HACK: Add postfix "src/styles" to solve Bun bundler path when the number of files is >= 9
28-
await Bun.$`cp -rf .cache/styles/src/styles dist`;
50+
await copyPublic();
51+
await generateSites();
52+
}
53+
}
2954

30-
const { fetchSite, pageMap } = await import("~/index.js");
55+
async function cleanOutput() {
56+
await Bun.$`rm -rf dist .cache`;
57+
}
3158

32-
await Promise.all([
33-
// Copy public
34-
Bun.$`cp -r public/* dist`,
35-
// Write Sites
36-
...Object.keys(pageMap).map((page) => writeSite(page)),
37-
]);
59+
async function initOutput() {
60+
await Bun.$`mkdir -p dist`;
61+
}
3862

39-
async function writeSite(pathname: string): Promise<number> {
40-
return await Bun.write(`dist${pathname}`, fetchSite(pathname).content);
63+
async function copyPublic() {
64+
await Bun.$`cp -r public/* dist`;
4165
}
4266

43-
export async function buildScripts() {
44-
const files = await readdir("src/scripts");
67+
async function generateSites() {
68+
await registerMiniVan();
69+
const { fetchSite, pageMap } = await import("~/index.js");
70+
71+
async function writeSite(pathname: string): Promise<number> {
72+
return await Bun.write(`dist${pathname}`, fetchSite(pathname).content);
73+
}
74+
await Promise.all([...Object.keys(pageMap).map((page) => writeSite(page))]);
75+
}
76+
77+
async function cacheScripts() {
78+
const files = await readdir(SRC_MAP.script);
4579
await Bun.build({
46-
entrypoints: files.map((f) => `src/scripts/${f}`),
47-
outdir: "./.cache/scripts",
80+
entrypoints: files.map((f) => `${SRC_MAP.script}/${f}`),
81+
outdir: CACHE_MAP.script,
4882
minify: true,
83+
splitting: true,
4984
plugins: [
5085
{
5186
name: "glsl",
@@ -58,14 +93,23 @@ export async function buildScripts() {
5893
},
5994
],
6095
});
96+
await Bun.$`cp -rf ${CACHE_MAP.script} dist`;
6197
}
6298

63-
export async function buildStyles() {
64-
const files = await readdir("src/styles");
99+
async function cacheStyles() {
100+
const files = await readdir(SRC_MAP.style);
65101
await Bun.build({
66-
entrypoints: files.map((f) => `./src/styles/${f}`),
67-
outdir: "./.cache/styles",
102+
entrypoints: files.map((f) => `${SRC_MAP.style}/${f}`),
103+
outdir: CACHE_MAP.style,
68104
experimentalCss: true,
105+
splitting: true,
69106
minify: true,
70107
});
108+
// await Bun.$`cp -rf .cache/styles dist`;
109+
// HACK: Add postfix "src/styles" to solve Bun bundler path when the number of files is >= 9
110+
await Bun.$`cp -rf ${CACHE_MAP.style}/src/styles dist`;
111+
}
112+
113+
if (import.meta.main) {
114+
await main();
71115
}

0 commit comments

Comments
 (0)