import type { Server, Subprocess } from "bun";
import { describe, expect, test } from "bun:test";
import { bunEnv, bunExe, tempDirWithFiles } from "harness";
import { join } from "path";
function replaceHash(html: string) {
return html
.trim()
.split("\n")
.map(a => a.trim())
.filter(a => a.length > 0)
.join("\n")
.trim()
.replace(/chunk-[a-z0-9]+\.css/g, "chunk-HASH.css")
.replace(/chunk-[a-z0-9]+\.js/g, "chunk-HASH.js");
}
function extractHash(html: string, file_kind: "css" | "js") {
const re = file_kind === "css" ? /chunk-([a-z0-9]+)\.css/ : /chunk-([a-z0-9]+)\.js/;
return html.match(re)?.[1];
}
test("serve html", async () => {
const dir = tempDirWithFiles("html-css-js", {
"dashboard.html": /*html*/ `
Dashboard
Dashboard
This is a separate route to test multiple pages work
Click me: 0
Back to Home
`,
"dashboard.js": /*js*/ `
import './script.js';
// Additional dashboard-specific code could go here
console.log("How...dashing?")
`,
"index.html": /*html*/ `
Bun HTML Import Test
Hello from Bun!
Click me: 0
`,
"script.js": /*js*/ `
let count = 0;
const button = document.getElementById('counter');
button.addEventListener('click', () => {
count++;
button.textContent = \`Click me: \${count}\`;
});
`,
"styles.css": /*css*/ `
.container {
max-width: 800px;
margin: 2rem auto;
text-align: center;
font-family: system-ui, sans-serif;
}
button {
padding: 0.5rem 1rem;
font-size: 1.25rem;
border-radius: 0.25rem;
border: 2px solid #000;
background: #fff;
cursor: pointer;
transition: all 0.2s;
}
button:hover {
background: #000;
color: #fff;
}
`,
});
const {
subprocess: subprocess1,
port,
hostname,
} = await waitForServer(dir, {
"/": join(dir, "index.html"),
"/dashboard": join(dir, "dashboard.html"),
});
await using subprocess = subprocess1;
{
const html = await (await fetch(`http://${hostname}:${port}/`)).text();
const trimmed = html
.trim()
.split("\n")
.map(a => a.trim())
.filter(a => a.length > 0)
.join("\n")
.trim()
.replace(/chunk-[a-z0-9]+\.css/g, "chunk-HASH.css")
.replace(/chunk-[a-z0-9]+\.js/g, "chunk-HASH.js");
expect(trimmed).toMatchInlineSnapshot(`
"
Bun HTML Import Test
Hello from Bun!
Click me: 0
"
`);
}
{
const html = await (await fetch(`http://${hostname}:${port}/dashboard`)).text();
const jsSrc = new URL(
html.match(/
Dashboard
This is a separate route to test multiple pages work
Click me: 0
Back to Home