diff --git a/test/bake/bake-harness.ts b/test/bake/bake-harness.ts index 3eb6ded89261..c3350388f18b 100644 --- a/test/bake/bake-harness.ts +++ b/test/bake/bake-harness.ts @@ -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. @@ -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 });", diff --git a/test/bake/dev/html.test.ts b/test/bake/dev/html.test.ts index c79ed5d0222a..25ec986e76ee 100644 --- a/test/bake/dev/html.test.ts +++ b/test/bake/dev/html.test.ts @@ -370,3 +370,19 @@ devTest("error report endpoint blanks stray non-text bytes in reported frames", await dev.fetch("/").expect.toInclude("

Frame Bytes

"); }, }); + +devTest("multiple html files are served at the routes bun derives from their paths", { + files: { + "index.html": emptyHtmlFile({ body: "

home

" }), + "about.html": emptyHtmlFile({ body: "

about

" }), + "docs/index.html": emptyHtmlFile({ body: "

docs

" }), + "docs/guide.html": emptyHtmlFile({ body: "

guide

" }), + }, + async test(dev) { + await dev.fetch("/").expect.toInclude("

home

"); + await dev.fetch("/about").expect.toInclude("

about

"); + await dev.fetch("/docs").expect.toInclude("

docs

"); + await dev.fetch("/docs/guide").expect.toInclude("

guide

"); + await dev.fetch("/docs/index").expect404(); + }, +});