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