www: port fresh.deno.dev to vite (#3227)

This PR ports fresh.deno.dev to use our vite plugin.
This commit is contained in:
Marvin Hagemeister
2025-08-29 16:41:16 +02:00
committed by GitHub
parent 8f8a6174ec
commit 4fe0199c9f
10 changed files with 207 additions and 352 deletions
+1
View File
@@ -0,0 +1 @@
import "./assets/styles.css";
+8 -3
View File
@@ -1,7 +1,12 @@
{
"tasks": {
"start": "deno run -A --watch=static/,routes/,../src,../docs dev.ts",
"build": "deno run -A dev.ts build",
"preview": "deno serve -A _fresh/server.js"
"start": "deno serve -A _fresh/server.js",
"dev": "vite",
"build": "vite build"
},
"imports": {
"@tailwindcss/vite": "npm:@tailwindcss/vite@^4.1.12",
"vite": "npm:vite@^7.1.3",
"vite-plugin-inspect": "npm:vite-plugin-inspect@^11.3.2"
}
}
+79 -66
View File
@@ -1,87 +1,100 @@
import VERSIONS from "../versions.json" with { type: "json" };
import { app } from "./main.ts";
import {
buildProd,
withBrowserApp,
withBrowser,
withChildProcessServer,
} from "../packages/fresh/tests/test_utils.tsx";
import { expect } from "@std/expect";
import { retry } from "@std/async/retry";
import { buildVite } from "../packages/plugin-vite/tests/test_utils.ts";
const applyCache = await buildProd({ root: import.meta.dirname! });
applyCache(app);
const handler = app.handler();
const result = await buildVite(import.meta.dirname!);
Deno.test("CORS should not set on GET /fresh-badge.svg", async () => {
const req = new Request("http://localhost/fresh-badge.svg");
const resp = await handler(req);
await resp?.body?.cancel();
expect(resp.headers.get("cross-origin-resource-policy")).toEqual(null);
await withChildProcessServer(
{
cwd: result.tmp,
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
},
async (address) => {
const resp = await fetch(`${address}/fresh-badge.svg`);
await resp?.body?.cancel();
expect(resp.headers.get("cross-origin-resource-policy")).toEqual(null);
},
);
});
Deno.test({
name: "shows version selector",
fn: async () => {
await retry(async () => {
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/docs`);
await page.waitForSelector("#version");
await withChildProcessServer(
{
cwd: result.tmp,
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
},
async (address) => {
await withBrowser(async (page) => {
await page.goto(`${address}/docs`);
await page.waitForSelector("#version");
// Check that we redirected to the first page
await page.waitForFunction(() => {
const url = new URL(window.location.href);
return url.pathname === "/docs/introduction";
});
// Check that we redirected to the first page
await page.waitForFunction(() => {
const url = new URL(window.location.href);
return url.pathname === "/docs/introduction";
});
// Wait for version selector to be enabled
await page.waitForSelector("#version:not([disabled])");
// Wait for version selector to be enabled
await page.waitForSelector("#version:not([disabled])");
const options = await page
.locator<HTMLSelectElement>("#version")
.evaluate((el) => {
return Array.from(el.options).map((option) => ({
value: option.value,
label: option.textContent,
}));
const options = await page
.locator<HTMLSelectElement>("#version")
.evaluate((el) => {
return Array.from(el.options).map((option) => ({
value: option.value,
label: option.textContent,
}));
});
expect(options).toEqual([
{
value: "canary",
label: "canary",
},
{
value: "latest",
label: VERSIONS[0],
},
]);
const selectValue = await page
.locator<HTMLSelectElement>("#version")
.evaluate((el) => el.value);
expect(selectValue).toEqual("latest");
// Go to canary page
await page.evaluate(() => {
const el = document.querySelector(
"#version",
) as HTMLSelectElement;
el.value = "canary";
el.dispatchEvent(new Event("change"));
});
await new Promise((r) => setTimeout(r, 1000));
await page.waitForSelector("#version:not([disabled])");
const selectValue2 = await page
.locator<HTMLSelectElement>("#version")
.evaluate((el) => el.value);
expect(selectValue2).toEqual("canary");
await page.waitForFunction(() => {
const url = new URL(window.location.href);
return url.pathname === "/docs/canary/introduction";
});
});
expect(options).toEqual([
{
value: "canary",
label: "canary",
},
{
value: "latest",
label: VERSIONS[0],
},
]);
const selectValue = await page
.locator<HTMLSelectElement>("#version")
.evaluate((el) => el.value);
expect(selectValue).toEqual("latest");
// Go to canary page
await page.evaluate(() => {
const el = document.querySelector("#version") as HTMLSelectElement;
el.value = "canary";
el.dispatchEvent(new Event("change"));
});
await new Promise((r) => setTimeout(r, 1000));
await page.waitForSelector("#version:not([disabled])");
const selectValue2 = await page
.locator<HTMLSelectElement>("#version")
.evaluate((el) => el.value);
expect(selectValue2).toEqual("canary");
await page.waitForFunction(() => {
const url = new URL(window.location.href);
return url.pathname === "/docs/canary/introduction";
});
});
},
);
});
},
});
-1
View File
@@ -31,7 +31,6 @@ export default define.page(function App({ Component, state, url }) {
type="font/woff2"
crossorigin="anonymous"
/>
<link rel="stylesheet" href={asset("/styles.css")} />
{url.pathname === "/"
? <link rel="stylesheet" href={asset("/prism.css")} />
: null}
+4 -1
View File
@@ -120,7 +120,10 @@ export const handler = define.handlers<Data>({
}
// Parse markdown front matter
const url = new URL(`../../../${entry.file}`, import.meta.url);
// deno-lint-ignore no-explicit-any
const url = (import.meta as any).env.PROD
? new URL(`../${entry.file}`, import.meta.url)
: new URL(`../../../${entry.file}`, import.meta.url);
const fileContent = await Deno.readTextFile(url);
const { body, attrs } = frontMatter<Record<string, unknown>>(fileContent);
+50
View File
@@ -0,0 +1,50 @@
import { defineConfig, type Plugin } from "vite";
import { fresh } from "@fresh/plugin-vite";
import tailwindcss from "@tailwindcss/vite";
import inspect from "vite-plugin-inspect";
import { copy } from "@std/fs";
import * as path from "@std/path";
export default defineConfig({
plugins: [
fresh(),
tailwindcss(),
inspect(),
copyDocs(),
],
});
function copyDocs(): Plugin {
let serverOutDir = "";
return {
name: "fresh:copy-docs",
configResolved(config) {
serverOutDir = config.environments.ssr.build.outDir;
},
async writeBundle() {
const branches = ["canary", "latest"];
for (const branch of branches) {
const source = path.join(import.meta.dirname!, "..", "docs", branch);
const target = path.join(serverOutDir, "docs", branch);
try {
await Deno.remove(target, { recursive: true });
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
throw err;
}
}
try {
await Deno.mkdir(path.dirname(target), { recursive: true });
} catch {
// Ignore
}
await copy(source, target);
}
},
};
}