fix: support optional static, routes and island dir in vite plugin (#3206)
This commit is contained in:
@@ -131,34 +131,36 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const entries = await fsAdapter.walk(
|
if (await fsAdapter.isDirectory(publicDir)) {
|
||||||
publicDir,
|
const entries = await fsAdapter.walk(
|
||||||
{
|
publicDir,
|
||||||
followSymlinks: false,
|
{
|
||||||
includeDirs: false,
|
followSymlinks: false,
|
||||||
includeFiles: true,
|
includeDirs: false,
|
||||||
skip: options.ignore,
|
includeFiles: true,
|
||||||
},
|
skip: options.ignore,
|
||||||
);
|
},
|
||||||
|
);
|
||||||
|
|
||||||
for await (const entry of entries) {
|
for await (const entry of entries) {
|
||||||
const relative = path.relative(publicDir, entry.path);
|
const relative = path.relative(publicDir, entry.path);
|
||||||
const filePath = path.join(clientOutDir, relative);
|
const filePath = path.join(clientOutDir, relative);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await Deno.mkdir(path.dirname(filePath), { recursive: true });
|
await Deno.mkdir(path.dirname(filePath), { recursive: true });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (!(err instanceof Deno.errors.AlreadyExists)) {
|
if (!(err instanceof Deno.errors.AlreadyExists)) {
|
||||||
throw err;
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
await Deno.copyFile(entry.path, filePath);
|
||||||
await Deno.copyFile(entry.path, filePath);
|
|
||||||
|
|
||||||
staticFiles.push({
|
staticFiles.push({
|
||||||
filePath,
|
filePath,
|
||||||
hash: null,
|
hash: null,
|
||||||
pathname: relative,
|
pathname: relative,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,12 +4,13 @@ import {
|
|||||||
withBrowser,
|
withBrowser,
|
||||||
withChildProcessServer,
|
withChildProcessServer,
|
||||||
} from "../../fresh/tests/test_utils.tsx";
|
} 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({
|
Deno.test({
|
||||||
name: "vite build - launches",
|
name: "vite build - launches",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await using res = await buildVite();
|
await using res = await buildVite(DEMO_DIR);
|
||||||
|
|
||||||
await withChildProcessServer(
|
await withChildProcessServer(
|
||||||
{
|
{
|
||||||
@@ -30,7 +31,7 @@ Deno.test({
|
|||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite build - serves static files",
|
name: "vite build - serves static files",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await using viteResult = await buildVite();
|
await using viteResult = await buildVite(DEMO_DIR);
|
||||||
|
|
||||||
await withChildProcessServer(
|
await withChildProcessServer(
|
||||||
{
|
{
|
||||||
@@ -51,7 +52,7 @@ Deno.test({
|
|||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite build - loads islands",
|
name: "vite build - loads islands",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await using res = await buildVite();
|
await using res = await buildVite(DEMO_DIR);
|
||||||
|
|
||||||
await withChildProcessServer(
|
await withChildProcessServer(
|
||||||
{
|
{
|
||||||
@@ -75,3 +76,69 @@ Deno.test({
|
|||||||
sanitizeOps: false,
|
sanitizeOps: false,
|
||||||
sanitizeResources: 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,
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,12 +1,17 @@
|
|||||||
import * as path from "@std/path";
|
import * as path from "@std/path";
|
||||||
import { expect } from "@std/expect";
|
import { expect } from "@std/expect";
|
||||||
import { waitForText, withBrowser } from "../../fresh/tests/test_utils.tsx";
|
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({
|
Deno.test({
|
||||||
name: "vite dev - launches",
|
name: "vite dev - launches",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await withDevServer(async (address) => {
|
await withDevServer(DEMO_DIR, async (address) => {
|
||||||
const res = await fetch(`${address}/tests/it_works`);
|
const res = await fetch(`${address}/tests/it_works`);
|
||||||
const text = await res.text();
|
const text = await res.text();
|
||||||
expect(text).toContain("it works");
|
expect(text).toContain("it works");
|
||||||
@@ -19,7 +24,7 @@ Deno.test({
|
|||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite dev - serves static files",
|
name: "vite dev - serves static files",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await withDevServer(async (address) => {
|
await withDevServer(DEMO_DIR, async (address) => {
|
||||||
const res = await fetch(`${address}/test_static/foo.txt`);
|
const res = await fetch(`${address}/test_static/foo.txt`);
|
||||||
const text = await res.text();
|
const text = await res.text();
|
||||||
expect(text).toContain("it works");
|
expect(text).toContain("it works");
|
||||||
@@ -32,7 +37,7 @@ Deno.test({
|
|||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite dev - loads islands",
|
name: "vite dev - loads islands",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await withDevServer(async (address) => {
|
await withDevServer(DEMO_DIR, async (address) => {
|
||||||
await withBrowser(async (page) => {
|
await withBrowser(async (page) => {
|
||||||
await page.goto(`${address}/tests/island_hooks`, {
|
await page.goto(`${address}/tests/island_hooks`, {
|
||||||
waitUntil: "networkidle2",
|
waitUntil: "networkidle2",
|
||||||
@@ -48,11 +53,53 @@ Deno.test({
|
|||||||
sanitizeOps: false,
|
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({
|
Deno.test({
|
||||||
name: "vite dev - can apply HMR to islands (hooks)",
|
name: "vite dev - can apply HMR to islands (hooks)",
|
||||||
ignore: true, // Test is very flaky
|
ignore: true, // Test is very flaky
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await withDevServer(async (address, dir) => {
|
await withDevServer(DEMO_DIR, async (address, dir) => {
|
||||||
await withBrowser(async (page) => {
|
await withBrowser(async (page) => {
|
||||||
await page.goto(`${address}/tests/island_hooks`, {
|
await page.goto(`${address}/tests/island_hooks`, {
|
||||||
waitUntil: "networkidle2",
|
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()],
|
||||||
|
});
|
||||||
@@ -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()],
|
||||||
|
});
|
||||||
@@ -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,6 +5,7 @@ import { withTmpDir } from "../../fresh/src/test_utils.ts";
|
|||||||
import { withChildProcessServer } from "../../fresh/tests/test_utils.tsx";
|
import { withChildProcessServer } from "../../fresh/tests/test_utils.tsx";
|
||||||
|
|
||||||
export const DEMO_DIR = path.join(import.meta.dirname!, "..", "demo");
|
export const DEMO_DIR = path.join(import.meta.dirname!, "..", "demo");
|
||||||
|
export const FIXTURE_DIR = path.join(import.meta.dirname!, "fixtures");
|
||||||
|
|
||||||
export async function updateFile(
|
export async function updateFile(
|
||||||
filePath: string,
|
filePath: string,
|
||||||
@@ -47,6 +48,7 @@ async function copyDir(from: string, to: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function withDevServer(
|
export async function withDevServer(
|
||||||
|
fixtureDir: string,
|
||||||
fn: (address: string, dir: string) => void | Promise<void>,
|
fn: (address: string, dir: string) => void | Promise<void>,
|
||||||
) {
|
) {
|
||||||
await using tmp = await withTmpDir({
|
await using tmp = await withTmpDir({
|
||||||
@@ -54,7 +56,7 @@ export async function withDevServer(
|
|||||||
prefix: "tmp_vite_",
|
prefix: "tmp_vite_",
|
||||||
});
|
});
|
||||||
|
|
||||||
await copyDir(DEMO_DIR, tmp.dir);
|
await copyDir(fixtureDir, tmp.dir);
|
||||||
|
|
||||||
await Deno.writeTextFile(
|
await Deno.writeTextFile(
|
||||||
path.join(tmp.dir, "vite.config.ts"),
|
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({
|
const tmp = await withTmpDir({
|
||||||
dir: path.join(import.meta.dirname!, ".."),
|
dir: path.join(import.meta.dirname!, ".."),
|
||||||
prefix: "tmp_vite_",
|
prefix: "tmp_vite_",
|
||||||
});
|
});
|
||||||
|
|
||||||
const builder = await createBuilder({
|
const builder = await createBuilder({
|
||||||
root: DEMO_DIR,
|
root: fixtureDir,
|
||||||
build: {
|
build: {
|
||||||
emptyOutDir: true,
|
emptyOutDir: true,
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user