closes #3391 --- Generated PR message: Fix base path handling for assets in plugin-vite Fixes issue where Fresh apps mounted at non-root paths (e.g., /ui) in frameworks like Hono would fail to load assets correctly. Problem When mounting a Fresh app with app.mount('/ui', UI.fetch), the HTML would load but all assets (CSS, JS, images) would return 404 because asset paths were hardcoded to start with /assets/ instead of respecting the Vite base configuration. Solution - Modified packages/plugin-vite/src/plugins/server_entry.ts to read Vite's base config and apply it to asset paths - Added getAssetPath() helper function to construct proper asset URLs with base path - Updated both CSS and asset file registration to use the base path Testing Added comprehensive test case "vite build - base path asset handling" that: - Builds demo with custom base path /my-app/ - Verifies generated server.js contains correctly prefixed asset paths - Follows existing test patterns and utilities Usage Users can now properly mount Fresh apps by configuring both: ```ts // vite.config.ts export default defineConfig({ base: "/ui/", plugins: [fresh()], }); // main.ts const app = new App({ basePath: "/ui" }); // hono-app.ts app.mount('/ui', UI.fetch); // Assets now work correctly ``` Backward compatible - no changes needed for apps mounted at root path.
207 lines
4.5 KiB
TypeScript
207 lines
4.5 KiB
TypeScript
import { createBuilder } from "vite";
|
|
import * as path from "@std/path";
|
|
import { walk } from "@std/fs/walk";
|
|
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,
|
|
fn: (text: string) => string | Promise<string>,
|
|
) {
|
|
const original = await Deno.readTextFile(filePath);
|
|
const result = await fn(original);
|
|
await Deno.writeTextFile(filePath, result);
|
|
|
|
return {
|
|
async [Symbol.asyncDispose]() {
|
|
await Deno.writeTextFile(filePath, original);
|
|
},
|
|
};
|
|
}
|
|
|
|
async function copyDir(from: string, to: string) {
|
|
const entries = walk(from, {
|
|
includeFiles: true,
|
|
includeDirs: false,
|
|
skip: [/([\\/]+(_fresh|node_modules|vendor)[\\/]+|[\\/]+vite\.config\.ts)/],
|
|
});
|
|
|
|
for await (const entry of entries) {
|
|
if (entry.isFile) {
|
|
const relative = path.relative(from, entry.path);
|
|
const target = path.join(to, relative);
|
|
|
|
try {
|
|
await Deno.mkdir(path.dirname(target), { recursive: true });
|
|
} catch (err) {
|
|
if (!(err instanceof Deno.errors.NotFound)) {
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
await Deno.copyFile(entry.path, target);
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function prepareDevServer(fixtureDir: string) {
|
|
const tmp = await withTmpDir({
|
|
dir: path.join(import.meta.dirname!, ".."),
|
|
prefix: "tmp_vite_",
|
|
});
|
|
|
|
await copyDir(fixtureDir, tmp.dir);
|
|
|
|
await Deno.writeTextFile(
|
|
path.join(tmp.dir, "vite.config.ts"),
|
|
`import { defineConfig } from "vite";
|
|
import { fresh } from "@fresh/plugin-vite";
|
|
|
|
export default defineConfig({
|
|
plugins: [
|
|
fresh(),
|
|
],
|
|
});
|
|
`,
|
|
);
|
|
|
|
return tmp;
|
|
}
|
|
|
|
export async function launchDevServer(
|
|
dir: string,
|
|
fn: (address: string, dir: string) => void | Promise<void>,
|
|
env: Record<string, string> = {},
|
|
) {
|
|
await withChildProcessServer(
|
|
{
|
|
cwd: dir,
|
|
args: ["run", "-A", "--cached-only", "npm:vite", "--port", "0"],
|
|
env,
|
|
},
|
|
async (address) => await fn(address, dir),
|
|
);
|
|
}
|
|
|
|
export async function spawnDevServer(
|
|
dir: string,
|
|
env: Record<string, string> = {},
|
|
) {
|
|
const boot = Promise.withResolvers<void>();
|
|
const p = Promise.withResolvers<void>();
|
|
|
|
let serverAddress = "";
|
|
|
|
const server = withChildProcessServer(
|
|
{
|
|
cwd: dir,
|
|
args: ["run", "-A", "--cached-only", "npm:vite", "--port", "0"],
|
|
env,
|
|
},
|
|
async (address) => {
|
|
serverAddress = address;
|
|
boot.resolve();
|
|
await p.promise;
|
|
},
|
|
);
|
|
|
|
await boot.promise;
|
|
|
|
return {
|
|
dir,
|
|
promise: server,
|
|
address: () => {
|
|
return serverAddress;
|
|
},
|
|
async [Symbol.asyncDispose]() {
|
|
await p.resolve();
|
|
},
|
|
};
|
|
}
|
|
|
|
export async function withDevServer(
|
|
fixtureDir: string,
|
|
fn: (address: string, dir: string) => void | Promise<void>,
|
|
env: Record<string, string> = {},
|
|
) {
|
|
await using tmp = await prepareDevServer(fixtureDir);
|
|
await launchDevServer(tmp.dir, fn, env);
|
|
}
|
|
|
|
export async function buildVite(
|
|
fixtureDir: string,
|
|
options?: { base?: string },
|
|
) {
|
|
const tmp = await withTmpDir({
|
|
dir: path.join(import.meta.dirname!, ".."),
|
|
prefix: "tmp_vite_",
|
|
});
|
|
|
|
const builder = await createBuilder({
|
|
logLevel: "error",
|
|
root: fixtureDir,
|
|
base: options?.base,
|
|
build: {
|
|
emptyOutDir: true,
|
|
},
|
|
environments: {
|
|
ssr: {
|
|
build: {
|
|
outDir: path.join(tmp.dir, "_fresh", "server"),
|
|
},
|
|
},
|
|
client: {
|
|
build: {
|
|
outDir: path.join(tmp.dir, "_fresh", "client"),
|
|
},
|
|
},
|
|
},
|
|
});
|
|
await builder.buildApp();
|
|
|
|
return {
|
|
tmp: tmp.dir,
|
|
async [Symbol.asyncDispose]() {
|
|
return await tmp[Symbol.asyncDispose]();
|
|
},
|
|
};
|
|
}
|
|
|
|
export function usingEnv(name: string, value: string) {
|
|
const prev = Deno.env.get(name);
|
|
Deno.env.set(name, value);
|
|
return {
|
|
[Symbol.dispose]: () => {
|
|
if (prev === undefined) {
|
|
Deno.env.delete(name);
|
|
} else {
|
|
Deno.env.set(name, prev);
|
|
}
|
|
},
|
|
};
|
|
}
|
|
|
|
export interface ProdOptions {
|
|
cwd: string;
|
|
args?: string[];
|
|
bin?: string;
|
|
env?: Record<string, string>;
|
|
}
|
|
|
|
export async function launchProd(
|
|
options: ProdOptions,
|
|
fn: (address: string) => void | Promise<void>,
|
|
) {
|
|
return await withChildProcessServer(
|
|
{
|
|
cwd: options.cwd,
|
|
args: options.args ??
|
|
["serve", "-A", "--cached-only", "--port", "0", "_fresh/server.js"],
|
|
},
|
|
fn,
|
|
);
|
|
}
|