feat: support multiple staticDir entries (#3759)
## Summary - `staticDir` now accepts `string | string[]` in both the **Vite plugin** (`FreshViteConfig`) and the **Builder API** (`BuildOptions`) - When multiple directories are specified, they are searched in order and the first match wins - **Vite plugin**: first dir maps to Vite's native `publicDir`; extra dirs are served manually in the dev server and walked during production builds - **Builder API**: both `MemoryBuildCache` (dev) and `DiskBuildCache` (prod) iterate over all directories - Updated tailwind v3 plugin to use `config.root` instead of `path.dirname(config.staticDir)` - Docs updated: `static-files.md`, `vite.md`, `builder.md` Closes #3105 --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
c0a866d027
commit
990aeb03f3
@@ -55,11 +55,13 @@ export type {
|
||||
* ```
|
||||
*/
|
||||
export function fresh(config?: FreshViteConfig): Plugin[] {
|
||||
const rawStaticDir = config?.staticDir ?? "static";
|
||||
const fConfig: ResolvedFreshViteConfig = {
|
||||
serverEntry: config?.serverEntry ?? "main.ts",
|
||||
clientEntry: config?.clientEntry ?? "client.ts",
|
||||
islandsDir: config?.islandsDir ?? "islands",
|
||||
routeDir: config?.routeDir ?? "routes",
|
||||
staticDir: Array.isArray(rawStaticDir) ? rawStaticDir : [rawStaticDir],
|
||||
ignore: config?.ignore ?? [TEST_FILE_PATTERN],
|
||||
islandSpecifiers: new Map(),
|
||||
namer: new UniqueNamer(),
|
||||
@@ -111,7 +113,7 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
|
||||
noDiscovery: true,
|
||||
},
|
||||
|
||||
publicDir: pathWithRoot("static", config.root),
|
||||
publicDir: pathWithRoot(fConfig.staticDir[0], config.root),
|
||||
|
||||
builder: {
|
||||
async buildApp(builder) {
|
||||
@@ -208,6 +210,9 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
|
||||
|
||||
fConfig.islandsDir = pathWithRoot(fConfig.islandsDir, vConfig.root);
|
||||
fConfig.routeDir = pathWithRoot(fConfig.routeDir, vConfig.root);
|
||||
fConfig.staticDir = fConfig.staticDir.map((d) =>
|
||||
pathWithRoot(d, vConfig.root)
|
||||
);
|
||||
|
||||
config?.islandSpecifiers?.map((spec) => {
|
||||
const specName = specToName(spec);
|
||||
@@ -233,7 +238,7 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
|
||||
clientEntryPlugin(fConfig),
|
||||
...clientSnapshot(fConfig),
|
||||
buildIdPlugin(),
|
||||
...devServer(),
|
||||
...devServer(fConfig),
|
||||
prefresh({
|
||||
include: [/\.[cm]?[tj]sx?$/],
|
||||
exclude: [/node_modules/, /[\\/]+deno[\\/]+npm[\\/]+/],
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
import type { DevEnvironment, Plugin } from "vite";
|
||||
import * as path from "@std/path";
|
||||
import { contentType as getStdContentType } from "@std/media-types/content-type";
|
||||
import { ASSET_CACHE_BUST_KEY } from "fresh/internal";
|
||||
import { createRequest, sendResponse } from "@remix-run/node-fetch-server";
|
||||
import { hashCode } from "../shared.ts";
|
||||
import type { ResolvedFreshViteConfig } from "../utils.ts";
|
||||
|
||||
export function devServer(): Plugin[] {
|
||||
function getContentType(ext: string): string {
|
||||
return getStdContentType(ext) ?? "application/octet-stream";
|
||||
}
|
||||
|
||||
export function devServer(freshConfig: ResolvedFreshViteConfig): Plugin[] {
|
||||
let publicDir = "";
|
||||
return [
|
||||
{
|
||||
@@ -53,30 +59,55 @@ export function devServer(): Plugin[] {
|
||||
|
||||
const decodedPathname = decodeURIComponent(url.pathname.slice(1));
|
||||
|
||||
// Check if it's a static file first
|
||||
const staticFilePath = path.join(publicDir, decodedPathname);
|
||||
try {
|
||||
const stat = await Deno.stat(staticFilePath);
|
||||
if (stat.isFile) {
|
||||
return next();
|
||||
// Check if it's a static file first.
|
||||
// Vite handles publicDir (the first static dir) natively,
|
||||
// but we also need to check extra static dirs.
|
||||
const extraStaticDirs = freshConfig.staticDir.slice(1);
|
||||
const allStaticDirs = [publicDir, ...extraStaticDirs];
|
||||
let handledStatic = false;
|
||||
for (const dir of allStaticDirs) {
|
||||
const staticFilePath = path.join(dir, decodedPathname);
|
||||
try {
|
||||
const stat = await Deno.stat(staticFilePath);
|
||||
if (stat.isFile) {
|
||||
if (dir === publicDir) {
|
||||
// Vite serves publicDir files natively
|
||||
return next();
|
||||
}
|
||||
// Serve files from extra dirs manually
|
||||
const file = await Deno.open(staticFilePath, { read: true });
|
||||
const { ext } = path.parse(staticFilePath);
|
||||
const contentType = getContentType(ext);
|
||||
nodeRes.setHeader("Content-Type", contentType);
|
||||
// deno-lint-ignore no-explicit-any
|
||||
for await (const chunk of file.readable as any) {
|
||||
nodeRes.write(chunk);
|
||||
}
|
||||
nodeRes.end();
|
||||
handledStatic = true;
|
||||
break;
|
||||
}
|
||||
} catch {
|
||||
// Ignore
|
||||
}
|
||||
} catch {
|
||||
// Ignore
|
||||
}
|
||||
if (handledStatic) return;
|
||||
|
||||
// Check if it's a static/index.html file
|
||||
const staticFilePathIndex = path.join(
|
||||
publicDir,
|
||||
decodedPathname,
|
||||
"index.html",
|
||||
);
|
||||
try {
|
||||
const content = await Deno.readTextFile(staticFilePathIndex);
|
||||
nodeRes.setHeader("Content-Type", "text/html; charset=utf-8");
|
||||
nodeRes.end(content);
|
||||
return;
|
||||
} catch {
|
||||
// Ignore
|
||||
for (const dir of allStaticDirs) {
|
||||
const staticFilePathIndex = path.join(
|
||||
dir,
|
||||
decodedPathname,
|
||||
"index.html",
|
||||
);
|
||||
try {
|
||||
const content = await Deno.readTextFile(staticFilePathIndex);
|
||||
nodeRes.setHeader("Content-Type", "text/html; charset=utf-8");
|
||||
nodeRes.end(content);
|
||||
return;
|
||||
} catch {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
@@ -32,7 +32,6 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
|
||||
let clientOutDir = "";
|
||||
let serverOutDir = "";
|
||||
let root = "";
|
||||
let publicDir = "";
|
||||
|
||||
const islands = new Map<string, { name: string; chunk: string | null }>();
|
||||
const islandsByFile = new Set<string>();
|
||||
@@ -56,7 +55,6 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
|
||||
configResolved(config) {
|
||||
root = config.root;
|
||||
|
||||
publicDir = pathWithRoot(config.publicDir, config.root);
|
||||
clientOutDir = pathWithRoot(
|
||||
config.environments.client.build.outDir,
|
||||
config.root,
|
||||
@@ -274,9 +272,14 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
|
||||
}
|
||||
}
|
||||
|
||||
if (await fsAdapter.isDirectory(publicDir)) {
|
||||
// Walk all static directories. First directory wins for
|
||||
// duplicate pathnames.
|
||||
const seenStaticPaths = new Set<string>();
|
||||
for (const dir of options.staticDir) {
|
||||
if (!(await fsAdapter.isDirectory(dir))) continue;
|
||||
|
||||
const entries = await fsAdapter.walk(
|
||||
publicDir,
|
||||
dir,
|
||||
{
|
||||
followSymlinks: false,
|
||||
includeDirs: false,
|
||||
@@ -286,11 +289,16 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
|
||||
);
|
||||
|
||||
for await (const entry of entries) {
|
||||
const relative = path.relative(publicDir, entry.path);
|
||||
const relative = path.relative(dir, entry.path);
|
||||
if (seenStaticPaths.has(relative)) continue;
|
||||
seenStaticPaths.add(relative);
|
||||
|
||||
const filePath = path.join(clientOutDir, relative);
|
||||
|
||||
try {
|
||||
await Deno.mkdir(path.dirname(filePath), { recursive: true });
|
||||
await Deno.mkdir(path.dirname(filePath), {
|
||||
recursive: true,
|
||||
});
|
||||
} catch (err) {
|
||||
if (!(err instanceof Deno.errors.AlreadyExists)) {
|
||||
throw err;
|
||||
@@ -306,15 +314,18 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
|
||||
|
||||
if (path.basename(relative) === "index.html") {
|
||||
const htmlRelative = path.relative(
|
||||
publicDir,
|
||||
dir,
|
||||
path.dirname(entry.path),
|
||||
);
|
||||
|
||||
staticFiles.push({
|
||||
filePath,
|
||||
hash: null,
|
||||
pathname: htmlRelative,
|
||||
});
|
||||
if (!seenStaticPaths.has(htmlRelative)) {
|
||||
seenStaticPaths.add(htmlRelative);
|
||||
staticFiles.push({
|
||||
filePath,
|
||||
hash: null,
|
||||
pathname: htmlRelative,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +45,12 @@ export interface FreshViteConfig {
|
||||
islandsDir?: string;
|
||||
/** Path to routes directory. Default: `./routes` */
|
||||
routeDir?: string;
|
||||
/**
|
||||
* The directory (or directories) to serve static files from.
|
||||
* When multiple directories are specified, they are searched in order
|
||||
* and the first match wins. Default: `"static"`
|
||||
*/
|
||||
staticDir?: string | string[];
|
||||
/**
|
||||
* Ignore file paths matching any of the provided regexes when
|
||||
* crawling the islands and routes directories.
|
||||
@@ -67,6 +73,10 @@ export interface FreshViteConfig {
|
||||
|
||||
export type ResolvedFreshViteConfig =
|
||||
& Required<
|
||||
Omit<FreshViteConfig, "islandSpecifiers">
|
||||
Omit<FreshViteConfig, "islandSpecifiers" | "staticDir">
|
||||
>
|
||||
& { islandSpecifiers: Map<string, string>; namer: UniqueNamer };
|
||||
& {
|
||||
staticDir: string[];
|
||||
islandSpecifiers: Map<string, string>;
|
||||
namer: UniqueNamer;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user