fix: support optional static, routes and island dir in vite plugin (#3206)

This commit is contained in:
Marvin Hagemeister
2025-08-08 09:23:38 +02:00
committed by GitHub
parent 0664476e46
commit 22ebb2dbfa
12 changed files with 187 additions and 36 deletions
@@ -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,
});
}
}
}
+71 -4
View File
@@ -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,
});
+52 -5
View File
@@ -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",
@@ -0,0 +1,3 @@
import { App } from "@fresh/core";
export const app = new App().fsRoutes();
@@ -0,0 +1,3 @@
export default function Hello() {
return <h1>ok</h1>;
}
@@ -0,0 +1,6 @@
import { defineConfig } from "vite";
import { fresh } from "@fresh/plugin-vite";
export default defineConfig({
plugins: [fresh()],
});
+3
View File
@@ -0,0 +1,3 @@
import { App } from "@fresh/core";
export const app = new App().get("/", () => new Response("ok"));
@@ -0,0 +1,6 @@
import { defineConfig } from "vite";
import { fresh } from "@fresh/plugin-vite";
export default defineConfig({
plugins: [fresh()],
});
+3
View File
@@ -0,0 +1,3 @@
import { App } from "@fresh/core";
export const app = new App().get("/ok", () => new Response("ok")).fsRoutes();
@@ -0,0 +1,3 @@
export default function Hello() {
return <h1>ok</h1>;
}
@@ -0,0 +1,6 @@
import { defineConfig } from "vite";
import { fresh } from "@fresh/plugin-vite";
export default defineConfig({
plugins: [fresh()],
});
+5 -3
View File
@@ -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<void>,
) {
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,
},