Skip to content

Commit 22ebb2d

Browse files
fix: support optional static, routes and island dir in vite plugin (#3206)
1 parent 0664476 commit 22ebb2d

12 files changed

Lines changed: 189 additions & 38 deletions

File tree

packages/plugin-vite/src/plugins/server_snapshot.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -131,34 +131,36 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
131131
}
132132
}
133133

134-
const entries = await fsAdapter.walk(
135-
publicDir,
136-
{
137-
followSymlinks: false,
138-
includeDirs: false,
139-
includeFiles: true,
140-
skip: options.ignore,
141-
},
142-
);
143-
144-
for await (const entry of entries) {
145-
const relative = path.relative(publicDir, entry.path);
146-
const filePath = path.join(clientOutDir, relative);
147-
148-
try {
149-
await Deno.mkdir(path.dirname(filePath), { recursive: true });
150-
} catch (err) {
151-
if (!(err instanceof Deno.errors.AlreadyExists)) {
152-
throw err;
134+
if (await fsAdapter.isDirectory(publicDir)) {
135+
const entries = await fsAdapter.walk(
136+
publicDir,
137+
{
138+
followSymlinks: false,
139+
includeDirs: false,
140+
includeFiles: true,
141+
skip: options.ignore,
142+
},
143+
);
144+
145+
for await (const entry of entries) {
146+
const relative = path.relative(publicDir, entry.path);
147+
const filePath = path.join(clientOutDir, relative);
148+
149+
try {
150+
await Deno.mkdir(path.dirname(filePath), { recursive: true });
151+
} catch (err) {
152+
if (!(err instanceof Deno.errors.AlreadyExists)) {
153+
throw err;
154+
}
153155
}
154-
}
155-
await Deno.copyFile(entry.path, filePath);
156+
await Deno.copyFile(entry.path, filePath);
156157

157-
staticFiles.push({
158-
filePath,
159-
hash: null,
160-
pathname: relative,
161-
});
158+
staticFiles.push({
159+
filePath,
160+
hash: null,
161+
pathname: relative,
162+
});
163+
}
162164
}
163165
}
164166

packages/plugin-vite/tests/build_test.ts

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import {
44
withBrowser,
55
withChildProcessServer,
66
} from "../../fresh/tests/test_utils.tsx";
7-
import { buildVite } from "./test_utils.ts";
7+
import { buildVite, DEMO_DIR, FIXTURE_DIR } from "./test_utils.ts";
8+
import * as path from "@std/path";
89

910
Deno.test({
1011
name: "vite build - launches",
1112
fn: async () => {
12-
await using res = await buildVite();
13+
await using res = await buildVite(DEMO_DIR);
1314

1415
await withChildProcessServer(
1516
{
@@ -30,7 +31,7 @@ Deno.test({
3031
Deno.test({
3132
name: "vite build - serves static files",
3233
fn: async () => {
33-
await using viteResult = await buildVite();
34+
await using viteResult = await buildVite(DEMO_DIR);
3435

3536
await withChildProcessServer(
3637
{
@@ -51,7 +52,7 @@ Deno.test({
5152
Deno.test({
5253
name: "vite build - loads islands",
5354
fn: async () => {
54-
await using res = await buildVite();
55+
await using res = await buildVite(DEMO_DIR);
5556

5657
await withChildProcessServer(
5758
{
@@ -75,3 +76,69 @@ Deno.test({
7576
sanitizeOps: false,
7677
sanitizeResources: false,
7778
});
79+
80+
Deno.test({
81+
name: "vite build - without static/ dir",
82+
fn: async () => {
83+
const fixture = path.join(FIXTURE_DIR, "no_static");
84+
await using res = await buildVite(fixture);
85+
86+
await withChildProcessServer(
87+
{
88+
cwd: res.tmp,
89+
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
90+
},
91+
async (address) => {
92+
const res = await fetch(`${address}/ok`);
93+
const text = await res.text();
94+
expect(text).toEqual("ok");
95+
},
96+
);
97+
},
98+
sanitizeOps: false,
99+
sanitizeResources: false,
100+
});
101+
102+
Deno.test({
103+
name: "vite build - without islands/ dir",
104+
fn: async () => {
105+
const fixture = path.join(FIXTURE_DIR, "no_islands");
106+
await using res = await buildVite(fixture);
107+
108+
await withChildProcessServer(
109+
{
110+
cwd: res.tmp,
111+
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
112+
},
113+
async (address) => {
114+
const res = await fetch(`${address}`);
115+
const text = await res.text();
116+
expect(text).toContain("ok");
117+
},
118+
);
119+
},
120+
sanitizeOps: false,
121+
sanitizeResources: false,
122+
});
123+
124+
Deno.test({
125+
name: "vite build - without routes/ dir",
126+
fn: async () => {
127+
const fixture = path.join(FIXTURE_DIR, "no_routes");
128+
await using res = await buildVite(fixture);
129+
130+
await withChildProcessServer(
131+
{
132+
cwd: res.tmp,
133+
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
134+
},
135+
async (address) => {
136+
const res = await fetch(`${address}`);
137+
const text = await res.text();
138+
expect(text).toEqual("ok");
139+
},
140+
);
141+
},
142+
sanitizeOps: false,
143+
sanitizeResources: false,
144+
});

packages/plugin-vite/tests/dev_server_test.ts

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import * as path from "@std/path";
22
import { expect } from "@std/expect";
33
import { waitForText, withBrowser } from "../../fresh/tests/test_utils.tsx";
4-
import { updateFile, withDevServer } from "./test_utils.ts";
4+
import {
5+
DEMO_DIR,
6+
FIXTURE_DIR,
7+
updateFile,
8+
withDevServer,
9+
} from "./test_utils.ts";
510

611
Deno.test({
712
name: "vite dev - launches",
813
fn: async () => {
9-
await withDevServer(async (address) => {
14+
await withDevServer(DEMO_DIR, async (address) => {
1015
const res = await fetch(`${address}/tests/it_works`);
1116
const text = await res.text();
1217
expect(text).toContain("it works");
@@ -19,7 +24,7 @@ Deno.test({
1924
Deno.test({
2025
name: "vite dev - serves static files",
2126
fn: async () => {
22-
await withDevServer(async (address) => {
27+
await withDevServer(DEMO_DIR, async (address) => {
2328
const res = await fetch(`${address}/test_static/foo.txt`);
2429
const text = await res.text();
2530
expect(text).toContain("it works");
@@ -32,7 +37,7 @@ Deno.test({
3237
Deno.test({
3338
name: "vite dev - loads islands",
3439
fn: async () => {
35-
await withDevServer(async (address) => {
40+
await withDevServer(DEMO_DIR, async (address) => {
3641
await withBrowser(async (page) => {
3742
await page.goto(`${address}/tests/island_hooks`, {
3843
waitUntil: "networkidle2",
@@ -48,11 +53,53 @@ Deno.test({
4853
sanitizeOps: false,
4954
});
5055

56+
Deno.test({
57+
name: "vite dev - starts without static/ dir",
58+
fn: async () => {
59+
const fixture = path.join(FIXTURE_DIR, "no_static");
60+
await withDevServer(fixture, async (address) => {
61+
const res = await fetch(`${address}/`);
62+
const text = await res.text();
63+
expect(text).toContain("ok");
64+
});
65+
},
66+
sanitizeResources: false,
67+
sanitizeOps: false,
68+
});
69+
70+
Deno.test({
71+
name: "vite dev - starts without islands/ dir",
72+
fn: async () => {
73+
const fixture = path.join(FIXTURE_DIR, "no_islands");
74+
await withDevServer(fixture, async (address) => {
75+
const res = await fetch(`${address}/`);
76+
const text = await res.text();
77+
expect(text).toContain("ok");
78+
});
79+
},
80+
sanitizeResources: false,
81+
sanitizeOps: false,
82+
});
83+
84+
Deno.test({
85+
name: "vite dev - starts without routes/ dir",
86+
fn: async () => {
87+
const fixture = path.join(FIXTURE_DIR, "no_routes");
88+
await withDevServer(fixture, async (address) => {
89+
const res = await fetch(`${address}/`);
90+
const text = await res.text();
91+
expect(text).toContain("ok");
92+
});
93+
},
94+
sanitizeResources: false,
95+
sanitizeOps: false,
96+
});
97+
5198
Deno.test({
5299
name: "vite dev - can apply HMR to islands (hooks)",
53100
ignore: true, // Test is very flaky
54101
fn: async () => {
55-
await withDevServer(async (address, dir) => {
102+
await withDevServer(DEMO_DIR, async (address, dir) => {
56103
await withBrowser(async (page) => {
57104
await page.goto(`${address}/tests/island_hooks`, {
58105
waitUntil: "networkidle2",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { App } from "@fresh/core";
2+
3+
export const app = new App().fsRoutes();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Hello() {
2+
return <h1>ok</h1>;
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { defineConfig } from "vite";
2+
import { fresh } from "@fresh/plugin-vite";
3+
4+
export default defineConfig({
5+
plugins: [fresh()],
6+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { App } from "@fresh/core";
2+
3+
export const app = new App().get("/", () => new Response("ok"));
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { defineConfig } from "vite";
2+
import { fresh } from "@fresh/plugin-vite";
3+
4+
export default defineConfig({
5+
plugins: [fresh()],
6+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { App } from "@fresh/core";
2+
3+
export const app = new App().get("/ok", () => new Response("ok")).fsRoutes();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Hello() {
2+
return <h1>ok</h1>;
3+
}

0 commit comments

Comments
 (0)