Skip to content

Commit 659d768

Browse files
docs: add builder docs (#3130)
1 parent 85c17fa commit 659d768

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

docs/canary/concepts/builder.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
description: |
3+
The Builder class is used to generate optimized assets for production.
4+
---
5+
6+
The `Builder` class in Fresh is where you'll do everything related to builds.
7+
You'll typically find it being created inside your project's `dev.ts` file.
8+
9+
```ts dev.ts
10+
import { Builder } from "fresh/dev";
11+
12+
const builder = new Builder({ target: "safari12" });
13+
14+
if (Deno.args.includes("build")) {
15+
// This creates a production build
16+
await builder.build();
17+
} else {
18+
// This starts a development server with live reload
19+
await builder.listen(() => import("./main.ts"));
20+
}
21+
```
22+
23+
## Options
24+
25+
You can customize the builder by passing options.
26+
27+
```ts
28+
const builder = new Builder({
29+
// Browser target for generated code. Maps to https://esbuild.github.io/api/#target
30+
target?: string | string[];
31+
// The root directory of the project. All other paths will be resolved
32+
// against this if they're relative. (Default: `Deno.cwd()`)
33+
root?: string;
34+
// Where to write generated files when doing a production build.
35+
// (default: `<root>/_fresh/`)
36+
outDir?: string;
37+
// Path to static file directory. (Default: `<root>/static/`)
38+
staticDir?: string;
39+
// Path to island directory. (Default: `<root>/islands`)
40+
islandDir?: string;
41+
// Path to routes directory. (Default: `<root>/routes`)
42+
routeDir?: string;
43+
// File paths which should be ignored
44+
ignore?: RegExp[];
45+
})
46+
```
47+
48+
## Registering islands
49+
50+
The builder is where you'll register files that contain islands. This is the
51+
same API that Fresh uses internally.
52+
53+
```ts
54+
const builder = new Builder();
55+
56+
// Path to local island
57+
builder.registerIsland("path/to/my/Island.tsx");
58+
// File urls work too
59+
builder.registerIsland("file:///path/to/my/Island.tsx");
60+
// Also islands from jsr
61+
builder.registerIsland("jsr:@marvinh-test/fresh-island");
62+
```
63+
64+
## Adding build plugins
65+
66+
The `Builder` has a very simple processing mechanism for static files.
67+
68+
```ts
69+
builder.onTransformStaticFile({
70+
pluginName: "My cool plugin",
71+
filter: /\.css$/,
72+
}, (args) => {
73+
// Prepend `body { background: red }` to every `.css` file
74+
const code = `body { background: red } ${args.text}`;
75+
76+
return {
77+
content: code,
78+
map: undefined, // Optional: source maps
79+
};
80+
});
81+
```
82+
83+
> [info]: Only static files in `static/` or the value you set `staticDir` to
84+
> will be processed. The builder won't process anything else.

docs/canary/testing/index.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,37 @@ Deno.test("MyLayout - renders heading and content", async () => {
103103
expect(text).toContain("Hello");
104104
});
105105
```
106+
107+
## Testing with file routes and islands
108+
109+
[File routes](/docs/canary/concepts//file-routing) are collected by the
110+
[`Builder`](/docs/canary/concepts/builder) class and not just by
111+
[`App`](/docs/concepts/app) alone. We can generate a snapshot and re-use it for
112+
many app instances in our test suite.
113+
114+
```ts my-app.test.ts
115+
// Best to do this once instead of for every test case for
116+
// performance reasons.
117+
const builder = new Builder();
118+
const applySnapshot = await builder.build({ snapshot: "memory" });
119+
120+
function testApp() {
121+
const app = new App()
122+
.get("/", () => new Response("hello"))
123+
.fsRoutes();
124+
125+
// Applies build snapshot to this app instance.
126+
applySnapshot(app);
127+
}
128+
129+
Deno.test("My Test", () => {
130+
const handler = testApp().handler();
131+
132+
const response = await handler(new Request("http://localhost"));
133+
const text = await response.text();
134+
135+
if (text !== "hello") {
136+
throw new Error("fail");
137+
}
138+
});
139+
```

docs/toc.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const toc: RawTableOfContents = {
3939
["islands", "Islands", "link:canary"],
4040
["static-files", "Static files", "link:canary"],
4141

42+
["builder", "Builder", "link:canary"],
4243
["file-routing", "File routing", "link:canary"],
4344
],
4445
},

0 commit comments

Comments
 (0)