From 22ebb2dbfaab6f20201b8a7f64cbc25c218f7503 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 8 Aug 2025 09:23:38 +0200 Subject: [PATCH] fix: support optional static, routes and island dir in vite plugin (#3206) --- .../src/plugins/server_snapshot.ts | 50 +++++++------ packages/plugin-vite/tests/build_test.ts | 75 ++++++++++++++++++- packages/plugin-vite/tests/dev_server_test.ts | 57 ++++++++++++-- .../tests/fixtures/no_islands/main.ts | 3 + .../fixtures/no_islands/routes/index.tsx | 3 + .../tests/fixtures/no_islands/vite.config.ts | 6 ++ .../tests/fixtures/no_routes/main.ts | 3 + .../tests/fixtures/no_routes/vite.config.ts | 6 ++ .../tests/fixtures/no_static/main.ts | 3 + .../tests/fixtures/no_static/routes/index.tsx | 3 + .../tests/fixtures/no_static/vite.config.ts | 6 ++ packages/plugin-vite/tests/test_utils.ts | 8 +- 12 files changed, 187 insertions(+), 36 deletions(-) create mode 100644 packages/plugin-vite/tests/fixtures/no_islands/main.ts create mode 100644 packages/plugin-vite/tests/fixtures/no_islands/routes/index.tsx create mode 100644 packages/plugin-vite/tests/fixtures/no_islands/vite.config.ts create mode 100644 packages/plugin-vite/tests/fixtures/no_routes/main.ts create mode 100644 packages/plugin-vite/tests/fixtures/no_routes/vite.config.ts create mode 100644 packages/plugin-vite/tests/fixtures/no_static/main.ts create mode 100644 packages/plugin-vite/tests/fixtures/no_static/routes/index.tsx create mode 100644 packages/plugin-vite/tests/fixtures/no_static/vite.config.ts diff --git a/packages/plugin-vite/src/plugins/server_snapshot.ts b/packages/plugin-vite/src/plugins/server_snapshot.ts index f51fbf9b..970410a2 100644 --- a/packages/plugin-vite/src/plugins/server_snapshot.ts +++ b/packages/plugin-vite/src/plugins/server_snapshot.ts @@ -131,34 +131,36 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] { } } - const entries = await fsAdapter.walk( - publicDir, - { - followSymlinks: false, - includeDirs: false, - includeFiles: true, - skip: options.ignore, - }, - ); + if (await fsAdapter.isDirectory(publicDir)) { + const entries = await fsAdapter.walk( + publicDir, + { + followSymlinks: false, + includeDirs: false, + includeFiles: true, + skip: options.ignore, + }, + ); - for await (const entry of entries) { - const relative = path.relative(publicDir, entry.path); - const filePath = path.join(clientOutDir, relative); + for await (const entry of entries) { + const relative = path.relative(publicDir, entry.path); + const filePath = path.join(clientOutDir, relative); - try { - await Deno.mkdir(path.dirname(filePath), { recursive: true }); - } catch (err) { - if (!(err instanceof Deno.errors.AlreadyExists)) { - throw err; + try { + await Deno.mkdir(path.dirname(filePath), { recursive: true }); + } catch (err) { + if (!(err instanceof Deno.errors.AlreadyExists)) { + throw err; + } } - } - await Deno.copyFile(entry.path, filePath); + await Deno.copyFile(entry.path, filePath); - staticFiles.push({ - filePath, - hash: null, - pathname: relative, - }); + staticFiles.push({ + filePath, + hash: null, + pathname: relative, + }); + } } } diff --git a/packages/plugin-vite/tests/build_test.ts b/packages/plugin-vite/tests/build_test.ts index 39864c2f..38320545 100644 --- a/packages/plugin-vite/tests/build_test.ts +++ b/packages/plugin-vite/tests/build_test.ts @@ -4,12 +4,13 @@ import { withBrowser, withChildProcessServer, } from "../../fresh/tests/test_utils.tsx"; -import { buildVite } from "./test_utils.ts"; +import { buildVite, DEMO_DIR, FIXTURE_DIR } from "./test_utils.ts"; +import * as path from "@std/path"; Deno.test({ name: "vite build - launches", fn: async () => { - await using res = await buildVite(); + await using res = await buildVite(DEMO_DIR); await withChildProcessServer( { @@ -30,7 +31,7 @@ Deno.test({ Deno.test({ name: "vite build - serves static files", fn: async () => { - await using viteResult = await buildVite(); + await using viteResult = await buildVite(DEMO_DIR); await withChildProcessServer( { @@ -51,7 +52,7 @@ Deno.test({ Deno.test({ name: "vite build - loads islands", fn: async () => { - await using res = await buildVite(); + await using res = await buildVite(DEMO_DIR); await withChildProcessServer( { @@ -75,3 +76,69 @@ Deno.test({ sanitizeOps: false, sanitizeResources: false, }); + +Deno.test({ + name: "vite build - without static/ dir", + fn: async () => { + const fixture = path.join(FIXTURE_DIR, "no_static"); + await using res = await buildVite(fixture); + + await withChildProcessServer( + { + cwd: res.tmp, + args: ["serve", "-A", "--port", "0", "_fresh/server.js"], + }, + async (address) => { + const res = await fetch(`${address}/ok`); + const text = await res.text(); + expect(text).toEqual("ok"); + }, + ); + }, + sanitizeOps: false, + sanitizeResources: false, +}); + +Deno.test({ + name: "vite build - without islands/ dir", + fn: async () => { + const fixture = path.join(FIXTURE_DIR, "no_islands"); + await using res = await buildVite(fixture); + + await withChildProcessServer( + { + cwd: res.tmp, + args: ["serve", "-A", "--port", "0", "_fresh/server.js"], + }, + async (address) => { + const res = await fetch(`${address}`); + const text = await res.text(); + expect(text).toContain("ok"); + }, + ); + }, + sanitizeOps: false, + sanitizeResources: false, +}); + +Deno.test({ + name: "vite build - without routes/ dir", + fn: async () => { + const fixture = path.join(FIXTURE_DIR, "no_routes"); + await using res = await buildVite(fixture); + + await withChildProcessServer( + { + cwd: res.tmp, + args: ["serve", "-A", "--port", "0", "_fresh/server.js"], + }, + async (address) => { + const res = await fetch(`${address}`); + const text = await res.text(); + expect(text).toEqual("ok"); + }, + ); + }, + sanitizeOps: false, + sanitizeResources: false, +}); diff --git a/packages/plugin-vite/tests/dev_server_test.ts b/packages/plugin-vite/tests/dev_server_test.ts index f20c58f6..a8c89e51 100644 --- a/packages/plugin-vite/tests/dev_server_test.ts +++ b/packages/plugin-vite/tests/dev_server_test.ts @@ -1,12 +1,17 @@ import * as path from "@std/path"; import { expect } from "@std/expect"; import { waitForText, withBrowser } from "../../fresh/tests/test_utils.tsx"; -import { updateFile, withDevServer } from "./test_utils.ts"; +import { + DEMO_DIR, + FIXTURE_DIR, + updateFile, + withDevServer, +} from "./test_utils.ts"; Deno.test({ name: "vite dev - launches", fn: async () => { - await withDevServer(async (address) => { + await withDevServer(DEMO_DIR, async (address) => { const res = await fetch(`${address}/tests/it_works`); const text = await res.text(); expect(text).toContain("it works"); @@ -19,7 +24,7 @@ Deno.test({ Deno.test({ name: "vite dev - serves static files", fn: async () => { - await withDevServer(async (address) => { + await withDevServer(DEMO_DIR, async (address) => { const res = await fetch(`${address}/test_static/foo.txt`); const text = await res.text(); expect(text).toContain("it works"); @@ -32,7 +37,7 @@ Deno.test({ Deno.test({ name: "vite dev - loads islands", fn: async () => { - await withDevServer(async (address) => { + await withDevServer(DEMO_DIR, async (address) => { await withBrowser(async (page) => { await page.goto(`${address}/tests/island_hooks`, { waitUntil: "networkidle2", @@ -48,11 +53,53 @@ Deno.test({ sanitizeOps: false, }); +Deno.test({ + name: "vite dev - starts without static/ dir", + fn: async () => { + const fixture = path.join(FIXTURE_DIR, "no_static"); + await withDevServer(fixture, async (address) => { + const res = await fetch(`${address}/`); + const text = await res.text(); + expect(text).toContain("ok"); + }); + }, + sanitizeResources: false, + sanitizeOps: false, +}); + +Deno.test({ + name: "vite dev - starts without islands/ dir", + fn: async () => { + const fixture = path.join(FIXTURE_DIR, "no_islands"); + await withDevServer(fixture, async (address) => { + const res = await fetch(`${address}/`); + const text = await res.text(); + expect(text).toContain("ok"); + }); + }, + sanitizeResources: false, + sanitizeOps: false, +}); + +Deno.test({ + name: "vite dev - starts without routes/ dir", + fn: async () => { + const fixture = path.join(FIXTURE_DIR, "no_routes"); + await withDevServer(fixture, async (address) => { + const res = await fetch(`${address}/`); + const text = await res.text(); + expect(text).toContain("ok"); + }); + }, + sanitizeResources: false, + sanitizeOps: false, +}); + Deno.test({ name: "vite dev - can apply HMR to islands (hooks)", ignore: true, // Test is very flaky fn: async () => { - await withDevServer(async (address, dir) => { + await withDevServer(DEMO_DIR, async (address, dir) => { await withBrowser(async (page) => { await page.goto(`${address}/tests/island_hooks`, { waitUntil: "networkidle2", diff --git a/packages/plugin-vite/tests/fixtures/no_islands/main.ts b/packages/plugin-vite/tests/fixtures/no_islands/main.ts new file mode 100644 index 00000000..d98e91b1 --- /dev/null +++ b/packages/plugin-vite/tests/fixtures/no_islands/main.ts @@ -0,0 +1,3 @@ +import { App } from "@fresh/core"; + +export const app = new App().fsRoutes(); diff --git a/packages/plugin-vite/tests/fixtures/no_islands/routes/index.tsx b/packages/plugin-vite/tests/fixtures/no_islands/routes/index.tsx new file mode 100644 index 00000000..00fdc581 --- /dev/null +++ b/packages/plugin-vite/tests/fixtures/no_islands/routes/index.tsx @@ -0,0 +1,3 @@ +export default function Hello() { + return

ok

; +} diff --git a/packages/plugin-vite/tests/fixtures/no_islands/vite.config.ts b/packages/plugin-vite/tests/fixtures/no_islands/vite.config.ts new file mode 100644 index 00000000..727d49a2 --- /dev/null +++ b/packages/plugin-vite/tests/fixtures/no_islands/vite.config.ts @@ -0,0 +1,6 @@ +import { defineConfig } from "vite"; +import { fresh } from "@fresh/plugin-vite"; + +export default defineConfig({ + plugins: [fresh()], +}); diff --git a/packages/plugin-vite/tests/fixtures/no_routes/main.ts b/packages/plugin-vite/tests/fixtures/no_routes/main.ts new file mode 100644 index 00000000..3456f10a --- /dev/null +++ b/packages/plugin-vite/tests/fixtures/no_routes/main.ts @@ -0,0 +1,3 @@ +import { App } from "@fresh/core"; + +export const app = new App().get("/", () => new Response("ok")); diff --git a/packages/plugin-vite/tests/fixtures/no_routes/vite.config.ts b/packages/plugin-vite/tests/fixtures/no_routes/vite.config.ts new file mode 100644 index 00000000..727d49a2 --- /dev/null +++ b/packages/plugin-vite/tests/fixtures/no_routes/vite.config.ts @@ -0,0 +1,6 @@ +import { defineConfig } from "vite"; +import { fresh } from "@fresh/plugin-vite"; + +export default defineConfig({ + plugins: [fresh()], +}); diff --git a/packages/plugin-vite/tests/fixtures/no_static/main.ts b/packages/plugin-vite/tests/fixtures/no_static/main.ts new file mode 100644 index 00000000..6e1c5359 --- /dev/null +++ b/packages/plugin-vite/tests/fixtures/no_static/main.ts @@ -0,0 +1,3 @@ +import { App } from "@fresh/core"; + +export const app = new App().get("/ok", () => new Response("ok")).fsRoutes(); diff --git a/packages/plugin-vite/tests/fixtures/no_static/routes/index.tsx b/packages/plugin-vite/tests/fixtures/no_static/routes/index.tsx new file mode 100644 index 00000000..00fdc581 --- /dev/null +++ b/packages/plugin-vite/tests/fixtures/no_static/routes/index.tsx @@ -0,0 +1,3 @@ +export default function Hello() { + return

ok

; +} diff --git a/packages/plugin-vite/tests/fixtures/no_static/vite.config.ts b/packages/plugin-vite/tests/fixtures/no_static/vite.config.ts new file mode 100644 index 00000000..727d49a2 --- /dev/null +++ b/packages/plugin-vite/tests/fixtures/no_static/vite.config.ts @@ -0,0 +1,6 @@ +import { defineConfig } from "vite"; +import { fresh } from "@fresh/plugin-vite"; + +export default defineConfig({ + plugins: [fresh()], +}); diff --git a/packages/plugin-vite/tests/test_utils.ts b/packages/plugin-vite/tests/test_utils.ts index de3f1ee8..73e1cde9 100644 --- a/packages/plugin-vite/tests/test_utils.ts +++ b/packages/plugin-vite/tests/test_utils.ts @@ -5,6 +5,7 @@ import { withTmpDir } from "../../fresh/src/test_utils.ts"; import { withChildProcessServer } from "../../fresh/tests/test_utils.tsx"; export const DEMO_DIR = path.join(import.meta.dirname!, "..", "demo"); +export const FIXTURE_DIR = path.join(import.meta.dirname!, "fixtures"); export async function updateFile( filePath: string, @@ -47,6 +48,7 @@ async function copyDir(from: string, to: string) { } export async function withDevServer( + fixtureDir: string, fn: (address: string, dir: string) => void | Promise, ) { await using tmp = await withTmpDir({ @@ -54,7 +56,7 @@ export async function withDevServer( prefix: "tmp_vite_", }); - await copyDir(DEMO_DIR, tmp.dir); + await copyDir(fixtureDir, tmp.dir); await Deno.writeTextFile( path.join(tmp.dir, "vite.config.ts"), @@ -75,14 +77,14 @@ export default defineConfig({ ); } -export async function buildVite() { +export async function buildVite(fixtureDir: string) { const tmp = await withTmpDir({ dir: path.join(import.meta.dirname!, ".."), prefix: "tmp_vite_", }); const builder = await createBuilder({ - root: DEMO_DIR, + root: fixtureDir, build: { emptyOutDir: true, },