docs: update for vite (#3308)

Update docs for vite
This commit is contained in:
Marvin Hagemeister
2025-09-02 10:45:27 +02:00
committed by GitHub
parent 6ddc0fbbcb
commit 9e58af7a5c
18 changed files with 372 additions and 318 deletions
-93
View File
@@ -1,93 +0,0 @@
---
description: |
The Builder class is used to generate optimized assets for production.
---
The `Builder` class in Fresh is where you'll do everything related to builds.
You'll typically find it being created inside your project's `dev.ts` file.
```ts dev.ts
import { Builder } from "fresh/dev";
const builder = new Builder({ target: "safari12" });
if (Deno.args.includes("build")) {
// This creates a production build
await builder.build();
} else {
// This starts a development server with live reload
await builder.listen(() => import("./main.ts"));
}
```
## Options
You can customize the builder by passing options.
```ts dev.ts
const builder = new Builder({
// Browser target for generated code. Maps to https://esbuild.github.io/api/#target
target?: string | string[];
// The root directory of the project. All other paths will be resolved
// against this if they're relative. (Default: `Deno.cwd()`)
root?: string;
// The path to your server entry point. (Default: `<root>/main.ts`)
serverEntry?: string;
// 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 island directory. (Default: `<root>/islands`)
islandDir?: string;
// Path to routes directory. (Default: `<root>/routes`)
routeDir?: string;
// File paths which should be ignored
ignore?: RegExp[];
// Optionally generate production source maps
// See https://esbuild.github.io/api/#source-maps
sourceMap?: {
kind?: boolean | 'linked' | 'inline' | 'external' | 'both';
sourceRoot?: string;
sourcesContent?: boolean;
};
})
```
## Registering islands
The builder is where you'll register files that contain islands. This is the
same API that Fresh uses internally.
```ts dev.ts
const builder = new Builder();
// Path to local island
builder.registerIsland("path/to/my/Island.tsx");
// File urls work too
builder.registerIsland("file:///path/to/my/Island.tsx");
// Also islands from jsr
builder.registerIsland("jsr:@marvinh-test/fresh-island");
```
## Adding build plugins
The `Builder` has a very simple processing mechanism for static files.
```ts dev.ts
builder.onTransformStaticFile({
pluginName: "My cool plugin",
filter: /\.css$/,
}, (args) => {
// Prepend `body { background: red }` to every `.css` file
const code = `body { background: red } ${args.text}`;
return {
content: code,
map: undefined, // Optional: source maps
};
});
```
> [info]: Only static files in `static/` or the value you set `staticDir` to
> will be processed. The builder won't process anything else.
+4 -16
View File
@@ -6,21 +6,9 @@ description: |
Use the `.fsRoutes()` helper on the [`App`](/docs/canary/concepts/app) instance
to specify where file based routes should be inserted. It adds routes based on
the structure in the `routes/` folder in your project (or any other folder you
have set in `dev.ts`). When you add a new file there, it will register a new
route automatically.
```ts main.ts
import { Builder } from "fresh/dev";
// Optionally set a custom route dir (will be `<root>/routes` by default)
const builder = new Builder({ routeDir: "path/to/routes" });
if (Deno.args.includes("build")) {
await builder.build();
} else {
await builder.listen(() => import("./main.ts"));
}
```
have specified when instantiating the
[`fresh()` vite plugin](/docs/canary/advanced/vite). in `vite.config.ts`). When
you add a new file there, it will register a new route automatically.
```ts main.ts
import { App, staticFiles } from "fresh";
@@ -40,7 +28,7 @@ Example project structure:
<project root>
├── deno.json
├── main.ts
├── dev.ts
├── vite.config.ts
└── routes
   ├── (marketing) # Route group, used to group related routes
   │ ├── _layout.tsx # Apply layout to all routes in this directory
+4
View File
@@ -29,9 +29,11 @@ An island can be used anywhere like a regular Preact component. Fresh will take
care of making it interactive on the client.
```tsx main.tsx
import { App, staticFiles } from "fresh";
import MyIsland from "./islands/my-island.tsx";
const app = new App()
.use(staticFiles())
.get("/", (ctx) => ctx.render(<MyIsland />));
```
@@ -72,9 +74,11 @@ A powerful feature of Fresh is that you can pass server-rendered JSX to an
island via props.
```tsx routes/index.tsx
import { staticFiles } from "fresh";
import MyIsland from "../islands/my-island.tsx";
const app = new App()
.use(staticFiles())
.get("/", (ctx) => {
return ctx.render(
<MyIsland jsx={<h1>hello</h1>}>
+10 -30
View File
@@ -26,16 +26,16 @@ template which can be conditioned based on state and params. Note that any state
set by middleware is available via `props.state`.
```tsx routes/sub/_layout.tsx
import { PageProps } from "$fresh/server.ts";
import { define } from "../../utils.ts";
export default function Layout({ Component, state }: PageProps) {
export default define.layout({ Component, state }) => {
// do something with state here
return (
<div class="layout">
<Component />
</div>
);
}
})
```
## Async layouts
@@ -44,33 +44,12 @@ In case you need to fetch data asynchronously before rendering the layout, you
can use an async layout to do so.
```tsx routes/sub/_layout.tsx
import { FreshContext } from "$fresh/server.ts";
import { define } from "../../utils.ts";
export default async function Layout(req: Request, ctx: FreshContext) {
export default define.layout(async (ctx) => {
// do something with state here
const data = await loadData();
return (
<div class="layout">
<p>{data.greeting}</p>
<ctx.Component />
</div>
);
}
```
### Define helper
To make it a little quicker to write async layouts, Fresh ships with a
`defineLayout` helper which automatically infers the correct types for the
function arguments.
```tsx
import { defineLayout } from "$fresh/server.ts";
export default defineLayout(async (req, ctx) => {
const data = await loadData();
return (
<div class="layout">
<p>{data.greeting}</p>
@@ -99,21 +78,22 @@ To make `routes/sub/special.tsx` opt out of rendering layouts we can set
`skipInheritedLayouts: true`.
```tsx routes/sub/special.tsx
import { RouteConfig } from "$fresh/server.ts";
import { type RouteConfig } from "fresh";
import { define } from "../../utils.ts";
export const config: RouteConfig = {
skipInheritedLayouts: true, // Skip already inherited layouts
};
export default function MyPage() {
export default define.layout(() => {
return <p>Hello world</p>;
}
});
```
You can skip already inherited layouts inside a layout file:
```tsx routes/special/_layout.tsx
import { LayoutConfig } from "$fresh/server.ts";
import { type LayoutConfig } from "fresh";
export const config: LayoutConfig = {
skipInheritedLayouts: true, // Skip already inherited layouts
-9
View File
@@ -16,15 +16,6 @@ const app = new App()
.use(staticFiles());
```
The path to the static file directory can be customized on the
[`Builder` class](/docs/canary/concepts/builder) inside your `dev.ts` file.
```ts dev.ts
import { Builder } from "fresh/dev";
const builder = new Builder({ staticDir: "path/to/staticDir" });
```
## Caching headers
By default, Fresh adds caching headers for the `src` and `srcset` attributes on