major: build islands + scan routeDir without importing user code (#3122)
This fixes a bunch of long standing issues in our tracker where a build
wouldn't finish because of importing user code. It's common for backend
apps to spawn some open ended tasks like subscribing to Deno KV or other
things. This addresses another problem of having to set up ENV vars just
for the build, even though they are technically not required to build
islands.
This also gives a roughly ~30% performance boost on cold starts (at
least on `deno.com`). Unfortunately, this does require some breaking
changes. But I believe the performance improvements + removing the
footgun of loading user code during build is well worth it. This work is
also a precursor in case we want to exploring moving to vite at some
point in the future.
## Breaking changes
1. Build options have moved to the builder.
```diff
// main.ts
- const app = new App({ build: { outDir: "dist/" } })
+ const app = new App()
// dev.ts
- const builder = new Builder();
+ const builder = new Builder({ outDir: "dist/" });
```
2. Another breaking change is how file based routes are loaded. Since
they're prepared and loaded by the builder the API changed.
```diff
// main.ts
const app = new App()
.use(staticFiles())
- await fsRoutes(app, {
- loadIsland: (path) => import(`./islands/${path}`),
- loadRoute: (path) => import(`./routes/${path}`),
- });
+ app.fsRoutes()
```
You can optionally change the `islandDir` or `routeDir` by passing that
to the `Builder`:
```ts
// dev.ts
const builder = new Builder({
islandDir: "/path/to/islands",
routeDir: "/path/to/routes"
});
```
3. Import `app` lazily in `dev.ts`. Passing `app` to `builder.build()`
is not necessary anymore.
```diff
- import { app } from "./main.ts";
const builder = new Builder({ target: "safari12" });
if (Deno.args.includes("build")) {
- await builder.build(app);
+ await builder.build();
} else {
- await builder.listen(app);
+ await builder.listen(() => import("./main.ts"));
}
```
4. The production run command has changed. It's now using `deno serve`
```diff
// deno.json
{
"tasks": {
"dev": "deno run -A --watch=static/,routes/ dev.ts",
"dev": "deno run -A dev.ts build",
- "start": "deno run -A main.ts",
+ "start": "deno serve -A _fresh/server.js",
}
}
```
Remove this bit in `main.ts`:
```diff
// main.ts
- if (import.meta.main) {
- await app.listen()
- }
```
Fixes https://github.com/denoland/fresh/issues/2240
Fixes https://github.com/denoland/fresh/issues/2640
Fixes https://github.com/denoland/fresh/issues/2592
Fixes https://github.com/denoland/fresh/issues/1843
This commit is contained in:
@@ -29,7 +29,7 @@ To run Fresh in production mode, run the `start` task:
|
||||
```sh Terminal
|
||||
deno task start
|
||||
# or
|
||||
deno run -A main.ts
|
||||
deno serve -A _fresh/server.js
|
||||
```
|
||||
|
||||
Fresh will automatically pick up the optimized assets in the `_fresh` directory.
|
||||
|
||||
@@ -53,21 +53,20 @@ The full `dev.ts` file for newly generated Fresh 2 projects looks like this:
|
||||
```ts
|
||||
import { Builder } from "fresh/dev";
|
||||
import { tailwind } from "@fresh/plugin-tailwind";
|
||||
import { app } from "./main.ts";
|
||||
|
||||
// Pass development only configuration here
|
||||
const builder = new Builder({ target: "safari12" });
|
||||
|
||||
// Example: Enabling the tailwind plugin for Fresh
|
||||
tailwind(builder, app);
|
||||
tailwind(builder);
|
||||
|
||||
// Create optimized assets for the browser when
|
||||
// running `deno run -A dev.ts build`
|
||||
if (Deno.args.includes("build")) {
|
||||
await builder.build(app);
|
||||
await builder.build();
|
||||
} else {
|
||||
// ...otherwise start the development server
|
||||
await builder.listen(app);
|
||||
await builder.listen(() => import("./main.ts"));
|
||||
}
|
||||
```
|
||||
|
||||
@@ -82,18 +81,9 @@ import { App, fsRoutes, staticFiles } from "fresh";
|
||||
|
||||
export const app = new App()
|
||||
// Add static file serving middleware
|
||||
.use(staticFiles());
|
||||
|
||||
// Enable file-system based routing
|
||||
await fsRoutes(app, {
|
||||
loadIsland: (path) => import(`./islands/${path}`),
|
||||
loadRoute: (path) => import(`./routes/${path}`),
|
||||
});
|
||||
|
||||
// If this module is called directly, start the server
|
||||
if (import.meta.main) {
|
||||
await app.listen();
|
||||
}
|
||||
.use(staticFiles())
|
||||
// Enable file-system based routing
|
||||
.fsRoutes();
|
||||
```
|
||||
|
||||
## Merging error pages
|
||||
@@ -226,9 +216,9 @@ Same is true for handlers:
|
||||
|
||||
All the various context interfaces have been consolidated and simplified:
|
||||
|
||||
| Fresh 1.x | Fresh 2.x |
|
||||
| --------------------------------------------- | -------------- |
|
||||
| `AppContext`, `LayoutContext`, `RouteContext` | `FreshContext` |
|
||||
| Fresh 1.x | Fresh 2.x |
|
||||
| --------------------------------------------- | ------------------------------------------ |
|
||||
| `AppContext`, `LayoutContext`, `RouteContext` | [`Context`](/docs/canary/concepts/context) |
|
||||
|
||||
### Context methods
|
||||
|
||||
|
||||
Reference in New Issue
Block a user