|
| 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. |
0 commit comments