Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions test/bake/bake-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ export interface DevServerTest {

/** Starting files */
files?: FileObject;
/** Manually specify which html files to serve */
/**
* Manually specify which html files to serve. Defaults to every `*.html` in
* `files`. A single file is served at `/*`; several are served at the routes
* `bun ./a.html ./b.html` would give them (`index.html` at `/`,
* `docs/index.html` at `/docs`, `docs/guide.html` at `/docs/guide`).
*/
htmlFiles?: string[];
/**
* Framework to use. Consider `minimalFramework` if possible.
Expand Down Expand Up @@ -1779,23 +1784,23 @@ class OutputLineStream extends EventEmitter {
}
}

/** Same routes as `bun ./index.html ./docs/index.html ./docs/guide.html`: "/", "/docs", "/docs/guide". */
function htmlFileRoute(posixRelativePath: string) {
const segments = posixRelativePath.replace(/\.html$/, "").split("/");
if (segments.at(-1) === "index") segments.pop();
return "/" + segments.join("/");
}

/** `htmlFiles` are relative to the directory the script is written to, in the platform's separator. */
export function indexHtmlScript(htmlFiles: string[]) {
const files = htmlFiles.map(file => file.replaceAll(path.sep, "/"));
return [
...htmlFiles.map((file, i) => `import html${i} from ${JSON.stringify("./" + file.replaceAll(path.sep, "/"))};`),
...files.map((file, i) => `import html${i} from ${JSON.stringify("./" + file)};`),
"export default {",
" static: {",
...(htmlFiles.length === 1
...(files.length === 1
? [` '/*': html0,`]
: htmlFiles.map(
(file, i) =>
` ${JSON.stringify(
"/" +
file
.replace(/\.html$/, "")
.replace("/index", "")
.replace(/\/$/, ""),
)}: html${i},`,
)),
: files.map((file, i) => ` ${JSON.stringify(htmlFileRoute(file))}: html${i},`)),
" },",
" fetch(req) {",
" return new Response('Not Found', { status: 404 });",
Expand Down
16 changes: 16 additions & 0 deletions test/bake/dev/html.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,19 @@ devTest("error report endpoint blanks stray non-text bytes in reported frames",
await dev.fetch("/").expect.toInclude("<h1>Frame Bytes</h1>");
},
});

devTest("multiple html files are served at the routes bun derives from their paths", {
files: {
"index.html": emptyHtmlFile({ body: "<h1>home</h1>" }),
"about.html": emptyHtmlFile({ body: "<h1>about</h1>" }),
"docs/index.html": emptyHtmlFile({ body: "<h1>docs</h1>" }),
"docs/guide.html": emptyHtmlFile({ body: "<h1>guide</h1>" }),
},
async test(dev) {
await dev.fetch("/").expect.toInclude("<h1>home</h1>");
await dev.fetch("/about").expect.toInclude("<h1>about</h1>");
await dev.fetch("/docs").expect.toInclude("<h1>docs</h1>");
await dev.fetch("/docs/guide").expect.toInclude("<h1>guide</h1>");
await dev.fetch("/docs/index").expect404();
},
});