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
@@ -40,8 +40,10 @@ const builder = new Builder({
|
||||
// Where to write generated files when doing a production build.
|
||||
// (default: `<root>/_fresh/`)
|
||||
outDir?: string;
|
||||
// Path to static file directory. (Default: `<root>/static/`)
|
||||
staticDir?: string;
|
||||
// Path to static file directory, or an array of directories.
|
||||
// When multiple directories are specified they are searched in order
|
||||
// and the first match wins. (Default: `<root>/static/`)
|
||||
staticDir?: string | string[];
|
||||
// Path to island directory. (Default: `<root>/islands`)
|
||||
islandDir?: string;
|
||||
// Path to routes directory. (Default: `<root>/routes`)
|
||||
@@ -93,8 +95,23 @@ builder.onTransformStaticFile({
|
||||
});
|
||||
```
|
||||
|
||||
> [info]: Only static files in `static/` or the value you set `staticDir` to
|
||||
> will be processed. The builder won't process anything else.
|
||||
> [info]: Only static files in `static/` or the directories you set `staticDir`
|
||||
> to will be processed. The builder won't process anything else.
|
||||
|
||||
### Multiple static directories
|
||||
|
||||
You can pass an array to `staticDir` to serve files from multiple directories.
|
||||
When the same filename exists in more than one directory, the first directory in
|
||||
the array takes precedence.
|
||||
|
||||
```ts dev.ts
|
||||
const builder = new Builder({
|
||||
staticDir: ["static", "generated"],
|
||||
});
|
||||
```
|
||||
|
||||
This is useful when you have a build step that generates assets into a separate
|
||||
directory and you want to keep them apart from hand-authored static files.
|
||||
|
||||
## Testing
|
||||
|
||||
|
||||
@@ -27,6 +27,10 @@ export default defineConfig({
|
||||
islandsDir: "./islands",
|
||||
// Path to routes directory. Default: ./routes
|
||||
routeDir: "./routes",
|
||||
// Static file directory or directories. Default: "static"
|
||||
// When multiple directories are given, they are searched in
|
||||
// order and the first match wins.
|
||||
staticDir: ["static", "generated"],
|
||||
// Optional regex to ignore folders when crawling the routes and
|
||||
// island directory.
|
||||
ignore: [/[\\/]+some-folder[\\/]+/],
|
||||
|
||||
@@ -47,6 +47,31 @@ pipeline, optimizes it, and adds a content hash to the filename for cache
|
||||
busting. Keeping these files outside `static/` ensures they're only included
|
||||
once in your build output.
|
||||
|
||||
## Multiple static directories
|
||||
|
||||
You can serve files from more than one directory by passing an array to the
|
||||
`staticDir` option. When the same filename exists in multiple directories, the
|
||||
first directory in the array takes precedence.
|
||||
|
||||
```ts vite.config.ts
|
||||
import { defineConfig } from "vite";
|
||||
import { fresh } from "@fresh/plugin-vite";
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [
|
||||
fresh({
|
||||
staticDir: ["static", "generated"],
|
||||
}),
|
||||
],
|
||||
});
|
||||
```
|
||||
|
||||
This is useful when you have a build step that generates assets into a separate
|
||||
directory and you want to keep them apart from hand-authored static files.
|
||||
|
||||
> [info]: If you're using the [Builder](/docs/advanced/builder) API instead of
|
||||
> Vite, the same `staticDir` option accepts a string or an array of strings.
|
||||
|
||||
## Caching headers
|
||||
|
||||
By default, Fresh adds caching headers for the `src` and `srcset` attributes on
|
||||
|
||||
Reference in New Issue
Block a user