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:
@@ -0,0 +1,281 @@
|
||||
import type { AnyComponent } from "preact";
|
||||
import type { RouteConfig } from "./types.ts";
|
||||
import type { HandlerByMethod, RouteHandler } from "./handlers.ts";
|
||||
import type { MiddlewareFn } from "./middlewares/mod.ts";
|
||||
import type { AsyncAnyComponent } from "./render.ts";
|
||||
import { type HandlerFn, isHandlerByMethod } from "./handlers.ts";
|
||||
import type { PageProps } from "./render.ts";
|
||||
import {
|
||||
type Command,
|
||||
CommandType,
|
||||
newAppCmd,
|
||||
newErrorCmd,
|
||||
newLayoutCmd,
|
||||
newMiddlewareCmd,
|
||||
newNotFoundCmd,
|
||||
newRouteCmd,
|
||||
} from "./commands.ts";
|
||||
|
||||
export interface FreshFsMod<State> {
|
||||
config?: RouteConfig;
|
||||
handler?: RouteHandler<unknown, State> | HandlerFn<unknown, State>[];
|
||||
handlers?: RouteHandler<unknown, State>;
|
||||
default?:
|
||||
| AnyComponent<PageProps<unknown, State>>
|
||||
| AsyncAnyComponent<PageProps<unknown, State>>;
|
||||
}
|
||||
|
||||
export interface FsRouteFile<State> {
|
||||
id: string; // FIXME remove?
|
||||
filePath: string;
|
||||
mod: FreshFsMod<State>;
|
||||
pattern: string;
|
||||
type: CommandType;
|
||||
routePattern: string;
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
function isFreshFile<State>(mod: any): mod is FreshFsMod<State> {
|
||||
if (mod === null || typeof mod !== "object") return false;
|
||||
|
||||
return typeof mod.default === "function" ||
|
||||
typeof mod.config === "object" || typeof mod.handlers === "object" ||
|
||||
typeof mod.handlers === "function" || typeof mod.handler === "object" ||
|
||||
typeof mod.handler === "function";
|
||||
}
|
||||
|
||||
export interface FsRoutesOptions {
|
||||
/**
|
||||
* Parent directory for the `/routes` and `/islands` folders.
|
||||
*
|
||||
* By default, the `root` config option of the provided app is used.
|
||||
* @default app.config.root
|
||||
*/
|
||||
dir?: string;
|
||||
ignoreFilePattern?: RegExp[];
|
||||
loadRoute: (path: string) => Promise<unknown>;
|
||||
}
|
||||
|
||||
export function fsItemsToCommands<State>(
|
||||
items: FsRouteFile<State>[],
|
||||
): Command<State>[] {
|
||||
const commands: Command<State>[] = [];
|
||||
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
const { filePath, type, mod, pattern, routePattern } = items[i];
|
||||
|
||||
if (!isFreshFile<State>(mod)) {
|
||||
throw new Error(
|
||||
`Expected a route, middleware, layout or error template, but couldn't find relevant exports in: ${filePath}`,
|
||||
);
|
||||
}
|
||||
|
||||
const handlers = mod.handlers ?? mod.handler ?? null;
|
||||
if (typeof handlers === "function" && handlers.length > 1) {
|
||||
throw new Error(
|
||||
`Handlers must only have one argument but found more than one. Check the function signature in: ${filePath}`,
|
||||
);
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case CommandType.Middleware: {
|
||||
let middlewares = (handlers ?? mod.default) as unknown as
|
||||
| MiddlewareFn<State>
|
||||
| MiddlewareFn<State>[]
|
||||
| HandlerByMethod<unknown, State> ??
|
||||
null;
|
||||
if (middlewares === null) continue;
|
||||
|
||||
if (isHandlerByMethod(middlewares)) {
|
||||
warnInvalidRoute(
|
||||
"Middleware does not support object handlers with GET, POST, etc.",
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!Array.isArray(middlewares)) {
|
||||
middlewares = [middlewares];
|
||||
}
|
||||
|
||||
commands.push(
|
||||
newMiddlewareCmd(pattern, middlewares, true),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
case CommandType.Layout: {
|
||||
if (handlers !== null) {
|
||||
warnInvalidRoute("Layout does not support handlers");
|
||||
}
|
||||
if (!mod.default) continue;
|
||||
|
||||
commands.push(newLayoutCmd(pattern, mod.default, mod.config, true));
|
||||
continue;
|
||||
}
|
||||
case CommandType.Error: {
|
||||
commands.push(newErrorCmd(
|
||||
pattern,
|
||||
{
|
||||
component: mod.default ?? undefined,
|
||||
config: mod.config ?? undefined,
|
||||
// deno-lint-ignore no-explicit-any
|
||||
handler: (handlers as any) ?? undefined,
|
||||
},
|
||||
true,
|
||||
));
|
||||
continue;
|
||||
}
|
||||
case CommandType.NotFound: {
|
||||
commands.push(newNotFoundCmd({
|
||||
config: mod.config,
|
||||
component: mod.default,
|
||||
// deno-lint-ignore no-explicit-any
|
||||
handler: handlers as any ?? undefined,
|
||||
}));
|
||||
continue;
|
||||
}
|
||||
case CommandType.App: {
|
||||
if (mod.default === undefined) continue;
|
||||
|
||||
commands.push(newAppCmd(mod.default));
|
||||
continue;
|
||||
}
|
||||
case CommandType.Route: {
|
||||
commands.push(newRouteCmd(
|
||||
pattern,
|
||||
{
|
||||
config: {
|
||||
...mod.config,
|
||||
routeOverride: mod.config?.routeOverride ?? routePattern,
|
||||
},
|
||||
// deno-lint-ignore no-explicit-any
|
||||
handler: (handlers as any) ?? undefined,
|
||||
component: mod.default,
|
||||
},
|
||||
false,
|
||||
));
|
||||
continue;
|
||||
}
|
||||
case CommandType.Handler:
|
||||
throw new Error(`Not supported`);
|
||||
case CommandType.FsRoute:
|
||||
throw new Error(`Nested FsRoutes are not supported`);
|
||||
default:
|
||||
throw new Error(`Unknown command type: ${type}`);
|
||||
}
|
||||
}
|
||||
|
||||
return commands;
|
||||
}
|
||||
|
||||
function warnInvalidRoute(message: string) {
|
||||
// deno-lint-ignore no-console
|
||||
console.warn(
|
||||
`🍋 %c[WARNING] Unsupported route config: ${message}`,
|
||||
"color:rgb(251, 184, 0)",
|
||||
);
|
||||
}
|
||||
|
||||
const APP_REG = /_app(?!\.[tj]sx?)?$/;
|
||||
|
||||
/**
|
||||
* Sort route paths where special Fresh files like `_app`,
|
||||
* `_layout` and `_middleware` are sorted in front.
|
||||
*/
|
||||
export function sortRoutePaths(a: string, b: string) {
|
||||
// The `_app` route should always be the first
|
||||
if (APP_REG.test(a)) return -1;
|
||||
else if (APP_REG.test(b)) return 1;
|
||||
|
||||
const aLen = a.length;
|
||||
const bLen = b.length;
|
||||
|
||||
let segment = false;
|
||||
let aIdx = 0;
|
||||
let bIdx = 0;
|
||||
for (; aIdx < aLen && bIdx < bLen; aIdx++, bIdx++) {
|
||||
const charA = a.charAt(aIdx);
|
||||
const charB = b.charAt(bIdx);
|
||||
|
||||
// When comparing a grouped route with a non-grouped one, we
|
||||
// need to skip over the group name to effectively compare the
|
||||
// actual route.
|
||||
if (charA === "(" && charB !== "(") {
|
||||
if (charB == "[") return -1;
|
||||
return 1;
|
||||
} else if (charB === "(" && charA !== "(") {
|
||||
if (charA == "[") return 1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (charA === "/" || charB === "/") {
|
||||
segment = true;
|
||||
|
||||
// If the other path doesn't close the segment
|
||||
// then we don't need to continue
|
||||
if (charA !== "/") return 1;
|
||||
if (charB !== "/") return -1;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (segment) {
|
||||
segment = false;
|
||||
|
||||
const scoreA = getRoutePathScore(charA, a, aIdx);
|
||||
const scoreB = getRoutePathScore(charB, b, bIdx);
|
||||
if (scoreA === scoreB) {
|
||||
if (charA !== charB) {
|
||||
// TODO: Do we need localeSort here or is this good enough?
|
||||
return charA < charB ? 0 : 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
return scoreA > scoreB ? -1 : 1;
|
||||
}
|
||||
|
||||
if (charA !== charB) {
|
||||
// TODO: Do we need localeSort here or is this good enough?
|
||||
return charA < charB ? 0 : 1;
|
||||
}
|
||||
|
||||
// If we're at the end of A or B, then we assume that the longer
|
||||
// path is more specific
|
||||
if (aIdx === aLen - 1 && bIdx < bLen - 1) {
|
||||
return 1;
|
||||
} else if (bIdx === bLen - 1 && aIdx < aLen - 1) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign a score based on the first two characters of a path segment.
|
||||
* The goal is to sort `_middleware` and `_layout` in front of everything
|
||||
* and `[` or `[...` last respectively.
|
||||
*/
|
||||
function getRoutePathScore(char: string, s: string, i: number): number {
|
||||
if (char === "_") {
|
||||
if (i + 1 < s.length) {
|
||||
if (s[i + 1] === "e") return 4;
|
||||
if (s[i + 1] === "m") return 6;
|
||||
}
|
||||
return 5;
|
||||
} else if (char === "[") {
|
||||
if (i + 1 < s.length && s[i + 1] === ".") {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (
|
||||
i + 4 === s.length - 1 && char === "i" && s[i + 1] === "n" &&
|
||||
s[i + 2] === "d" && s[i + 3] === "e" && s[i + 4] === "x"
|
||||
) {
|
||||
return 3;
|
||||
}
|
||||
|
||||
return 2;
|
||||
}
|
||||
Reference in New Issue
Block a user