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:
@@ -14,7 +14,6 @@ import type { Stringifiers } from "../../jsonify/stringify.ts";
|
||||
import type { PageProps } from "../../render.ts";
|
||||
import { Partial, type PartialProps } from "../shared.ts";
|
||||
import { stringify } from "../../jsonify/stringify.ts";
|
||||
import type { ServerIslandRegistry } from "../../context.ts";
|
||||
import type { Island } from "../../context.ts";
|
||||
import {
|
||||
assetHashingHook,
|
||||
@@ -90,7 +89,6 @@ export class RenderState {
|
||||
constructor(
|
||||
// deno-lint-ignore no-explicit-any
|
||||
public ctx: PageProps<any, any>,
|
||||
public islandRegistry: ServerIslandRegistry,
|
||||
public buildCache: BuildCache,
|
||||
public partialId: string,
|
||||
) {
|
||||
@@ -203,7 +201,7 @@ options[OptionsType.DIFF] = (vnode) => {
|
||||
} else if (
|
||||
!PATCHED.has(vnode) && !hasIslandOwner(RENDER_STATE, vnode)
|
||||
) {
|
||||
const island = RENDER_STATE.islandRegistry.get(vnode.type);
|
||||
const island = RENDER_STATE.buildCache.islandRegistry.get(vnode.type);
|
||||
if (island === undefined) {
|
||||
// Not an island, but we might need to preserve keys
|
||||
if (vnode.key !== undefined) {
|
||||
@@ -324,7 +322,7 @@ function hasIslandOwner(current: RenderState, vnode: VNode): boolean {
|
||||
let tmpVNode = vnode;
|
||||
let owner;
|
||||
while ((owner = current.owners.get(tmpVNode)) !== undefined) {
|
||||
if (current.islandRegistry.has(owner.type as ComponentType)) {
|
||||
if (current.buildCache.islandRegistry.has(owner.type as ComponentType)) {
|
||||
return true;
|
||||
}
|
||||
tmpVNode = owner;
|
||||
@@ -425,23 +423,16 @@ export interface PartialStateJson {
|
||||
}
|
||||
|
||||
function FreshRuntimeScript() {
|
||||
const { islands, nonce, ctx, islandProps, partialId, buildCache } =
|
||||
RENDER_STATE!;
|
||||
const { islands, nonce, ctx, islandProps, partialId } = RENDER_STATE!;
|
||||
const basePath = ctx.config.basePath;
|
||||
|
||||
const islandArr = Array.from(islands);
|
||||
|
||||
if (ctx.url.searchParams.has(PARTIAL_SEARCH_PARAM)) {
|
||||
const islands = islandArr.map((island) => {
|
||||
const chunk = buildCache.getIslandChunkName(island.name);
|
||||
if (chunk === null) {
|
||||
throw new Error(
|
||||
`Could not find chunk for ${island.name} ${island.file}#${island.exportName}`,
|
||||
);
|
||||
}
|
||||
return {
|
||||
exportName: island.exportName,
|
||||
chunk,
|
||||
chunk: island.file,
|
||||
name: island.name,
|
||||
};
|
||||
});
|
||||
@@ -464,16 +455,12 @@ function FreshRuntimeScript() {
|
||||
);
|
||||
} else {
|
||||
const islandImports = islandArr.map((island) => {
|
||||
const chunk = buildCache.getIslandChunkName(island.name);
|
||||
if (chunk === null) {
|
||||
throw new Error(
|
||||
`Could not find chunk for ${island.name} ${island.file}#${island.exportName}`,
|
||||
);
|
||||
}
|
||||
const named = island.exportName === "default"
|
||||
? island.name
|
||||
: `{ ${island.exportName} }`;
|
||||
return `import ${named} from "${`${basePath}${chunk}`}";`;
|
||||
: island.exportName === island.name
|
||||
? `{ ${island.exportName} }`
|
||||
: `{ ${island.exportName} as ${island.name} }`;
|
||||
return `import ${named} from "${`${basePath}${island.file}`}";`;
|
||||
}).join("");
|
||||
|
||||
const islandObj = "{" + islandArr.map((island) => island.name)
|
||||
|
||||
Reference in New Issue
Block a user