Skip to content

Commit ee41868

Browse files
committed
feat(kit): add assets
1 parent b469337 commit ee41868

File tree

9 files changed

+67
-7
lines changed

9 files changed

+67
-7
lines changed

apps/docs/app/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ export const GET: WaveKitHandler = async (c) => {
9999
.div({ style: "margin-top: 6rem;" }, (heroDiv) => {
100100
heroDiv
101101
.h1("The minimalist Bun web framework.")
102-
.p("WaveKit is a tiny framework built on top of Bun HTTP.")
102+
.p(
103+
"WaveKit is a tiny framework built on top of Bun HTTP. Bleeding edge.",
104+
)
103105
.h2("Features")
104106
.ul((ul) => {
105107
ul.li("Next.js style routing")

apps/docs/public/logo.svg

Lines changed: 1 addition & 0 deletions
Loading

apps/docs/src/components/navbar.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { wave } from "@wavekit/wave";
22

33
const Logo = wave.li((li) =>
4-
li.a({ href: "/wavekit", style: "font-weight: semibold;" }, "WaveKit"),
4+
li.a({ href: "/wavekit", style: "font-weight: semibold;" }, (a) => {
5+
a.img({
6+
src: "/logo.svg",
7+
alt: "Wavekit logo",
8+
height: "48",
9+
width: "128",
10+
});
11+
}),
512
);
613

714
const NavItem = ({

packages/kit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"files": ["dist"],
1414
"scripts": {
1515
"build": "bun run build:types && bun run build:code",
16-
"build:code": "bun build src/index.ts --outdir ./dist",
16+
"build:code": "bun build src/index.ts --outdir ./dist --target bun",
1717
"build:types": "tsc --emitDeclarationOnly",
1818
"test": "bun test"
1919
},

packages/kit/src/server.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import { expect, it } from "bun:test";
22
import path from "node:path";
33
import { createWaveKit } from "./server";
44

5-
const TEST_DIR = path.join(__dirname, "..", "test", "app");
5+
const TEST_ROUTES_DIR = path.join(__dirname, "..", "test", "app");
6+
const TEST_PUBLIC_DIR = path.join(__dirname, "..", "test", "public");
67

78
it("should create wavekit config", async () => {
8-
const { routes } = await createWaveKit({ routesDir: TEST_DIR });
9+
const { routes } = await createWaveKit({
10+
routesDir: TEST_ROUTES_DIR,
11+
publicDir: TEST_PUBLIC_DIR,
12+
});
913
expect(routes["/html.test"]).toHaveProperty("GET");
1014
});

packages/kit/src/server.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { readdir } from "node:fs/promises";
12
import path from "node:path";
23
import type { BunRequest, RouterTypes, Server } from "bun";
34
import { WaveKitResponse } from "./response";
@@ -8,6 +9,8 @@ const defaultRoutesDir = isDev
89
? path.join(process.cwd(), "app")
910
: path.join(process.cwd(), "build", "app");
1011

12+
const defaultPublicDir = path.join(process.cwd(), "public");
13+
1114
const defaultOutDir = path.join(process.cwd(), "build");
1215

1316
export type WaveKitContext = {
@@ -36,15 +39,38 @@ export type WaveKitHooks = {
3639
afterHandler?: (response: WaveKitResponse) => Promise<WaveKitResponse>;
3740
};
3841

42+
async function createAssetsHandlers({
43+
assetsDir,
44+
}: { assetsDir: string }): Promise<
45+
Record<string, RouterTypes.RouteHandlerObject<string>>
46+
> {
47+
const files = await readdir(assetsDir, { recursive: true });
48+
const iterableHandlers = files.map((filePath) => {
49+
const file = Bun.file(path.join(assetsDir, filePath));
50+
const fileName = `/${path.basename(filePath)}`;
51+
const handler = {
52+
async GET() {
53+
return new WaveKitResponse(file.stream(), {
54+
headers: { "Content-Type": file.type },
55+
});
56+
},
57+
};
58+
return [fileName, handler];
59+
});
60+
return Object.fromEntries(iterableHandlers);
61+
}
62+
3963
export type CreateWaveKitProps = {
4064
base?: string | undefined;
4165
routesDir?: string | undefined;
66+
publicDir?: string | undefined;
4267
hooks?: WaveKitHooks;
4368
};
4469

4570
export async function createWaveKit({
4671
base,
4772
routesDir,
73+
publicDir,
4874
hooks,
4975
}: CreateWaveKitProps = {}): Promise<WaveKit> {
5076
const safeBase = (base ?? "/") === "/" ? "" : (base ?? "");
@@ -100,14 +126,20 @@ export async function createWaveKit({
100126
}
101127
return [safeBase + path, contextHandler];
102128
});
129+
const assetsHandlers = await createAssetsHandlers({
130+
assetsDir: publicDir ?? defaultPublicDir,
131+
});
103132
const filteredRoutes = Object.fromEntries(
104133
(await Promise.all(routesWithHandlers)).filter(Boolean) as [
105134
string,
106135
RouterTypes.RouteHandlerObject<string>,
107136
][],
108137
);
109138
return {
110-
routes: filteredRoutes,
139+
routes: {
140+
...filteredRoutes,
141+
...assetsHandlers,
142+
},
111143
store: contextStore,
112144
};
113145
}
@@ -120,12 +152,14 @@ function sanitizeRoutePath(routePath: string) {
120152
export type SsgRenderProps = {
121153
base?: string | undefined;
122154
routesDir?: string | undefined;
155+
publicDir?: string | undefined;
123156
outDir?: string | undefined;
124157
};
125158

126159
export async function ssgRender({
127160
base,
128161
routesDir,
162+
publicDir,
129163
outDir,
130164
}: SsgRenderProps = {}) {
131165
const { routes } = await createWaveKit({ base, routesDir });
@@ -148,6 +182,16 @@ export async function ssgRender({
148182
}
149183
}),
150184
);
185+
// Copy assets from public directory to build folder
186+
const publicFiles = await readdir(publicDir ?? defaultPublicDir, {
187+
recursive: true,
188+
});
189+
for (const file of publicFiles) {
190+
const sourcePath = path.join(publicDir ?? defaultPublicDir, file);
191+
const destPath = path.join(outDir ?? defaultOutDir, base ?? "", file);
192+
await Bun.write(destPath, Bun.file(sourcePath));
193+
}
194+
151195
await server.stop();
152196
console.log(
153197
`Rendered ${renderedRoutes} route(s) to ${outDir ?? defaultOutDir}`,

packages/kit/test/public/logo.svg

Lines changed: 1 addition & 0 deletions
Loading

packages/kit/test/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const hooks: WaveKitHooks = {
1616

1717
createWaveKit({
1818
routesDir: path.join(process.cwd(), "test", "app"),
19+
publicDir: path.join(process.cwd(), "test", "public"),
1920
hooks,
2021
}).then(({ routes }) => {
2122
Bun.serve({ routes });

packages/wave/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"files": ["dist"],
1414
"scripts": {
1515
"build": "bun run build:types && bun run build:code",
16-
"build:code": "bun build src/index.ts --outdir ./dist",
16+
"build:code": "bun build src/index.ts --outdir ./dist --target bun",
1717
"build:types": "tsc --emitDeclarationOnly",
1818
"test": "bun test"
1919
},

0 commit comments

Comments
 (0)