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:
Marvin Hagemeister
2025-07-24 15:04:05 +02:00
committed by GitHub
parent 69af00ef65
commit 118242eaa6
54 changed files with 1911 additions and 1770 deletions
+9 -16
View File
@@ -158,11 +158,11 @@ ENV DENO_DEPLOYMENT_ID=\${GIT_REVISION}
WORKDIR /app
COPY . .
RUN deno cache main.ts
RUN deno cache _fresh/server.js
EXPOSE 8000
CMD ["run", "-A", "main.ts"]
CMD ["serve", "-A", "_fresh/server.js"]
`;
await writeFile("Dockerfile", DOCKERFILE_TEXT);
@@ -349,7 +349,7 @@ ${GRADIENT_CSS}`;
// Skip this and be silent if there is a network issue.
}
const MAIN_TS = `import { App, fsRoutes, staticFiles } from "fresh";
const MAIN_TS = `import { App, staticFiles } from "fresh";
import { define, type State } from "./utils.ts";
export const app = new App<State>();
@@ -371,14 +371,8 @@ const exampleLoggerMiddleware = define.middleware((ctx) => {
});
app.use(exampleLoggerMiddleware);
await fsRoutes(app, {
loadIsland: (path) => import(\`./islands/\${path}\`),
loadRoute: (path) => import(\`./routes/\${path}\`),
});
if (import.meta.main) {
await app.listen();
}`;
// Include file-system based routes here
app.fsRoutes();`;
await writeFile("main.ts", MAIN_TS);
const COMPONENTS_BUTTON_TSX =
@@ -489,14 +483,13 @@ export default function Counter(props: CounterProps) {
const DEV_TS = `#!/usr/bin/env -S deno run -A --watch=static/,routes/
${useTailwind ? `import { tailwind } from "@fresh/plugin-tailwind";\n` : ""}
import { Builder } from "fresh/dev";
import { app } from "./main.ts";
const builder = new Builder();
${useTailwind ? "tailwind(builder, app);" : ""}
${useTailwind ? "tailwind(builder);" : ""}
if (Deno.args.includes("build")) {
await builder.build(app);
await builder.build();
} else {
await builder.listen(app);
await builder.listen(() => import("./main.ts"));
}`;
await writeFile("dev.ts", DEV_TS);
@@ -506,7 +499,7 @@ if (Deno.args.includes("build")) {
check: "deno fmt --check . && deno lint . && deno check",
dev: "deno run -A --watch=static/,routes/ dev.ts",
build: "deno run -A dev.ts build",
start: "deno run -A main.ts",
start: "deno serve -A _fresh/server.js",
update: "deno run -A -r jsr:@fresh/update .",
},
lint: {
+23 -20
View File
@@ -139,28 +139,31 @@ Deno.test({
},
});
Deno.test("init with tailwind - fmt, lint, and type check project", async () => {
await using tmp = await withTmpDir();
const dir = tmp.dir;
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm({
[CONFIRM_TAILWIND_MESSAGE]: true,
});
Deno.test(
"init with tailwind - fmt, lint, and type check project",
async () => {
await using tmp = await withTmpDir();
const dir = tmp.dir;
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm({
[CONFIRM_TAILWIND_MESSAGE]: true,
});
await initProject(dir, [], {});
await expectProjectFile(dir, "main.ts");
await expectProjectFile(dir, "dev.ts");
await initProject(dir, [], {});
await expectProjectFile(dir, "main.ts");
await expectProjectFile(dir, "dev.ts");
await patchProject(dir);
await patchProject(dir);
const check = await new Deno.Command(Deno.execPath(), {
args: ["task", "check"],
cwd: dir,
stderr: "inherit",
stdout: "inherit",
}).output();
expect(check.code).toEqual(0);
});
const check = await new Deno.Command(Deno.execPath(), {
args: ["task", "check"],
cwd: dir,
stderr: "inherit",
stdout: "inherit",
}).output();
expect(check.code).toEqual(0);
},
);
Deno.test("init - can start dev server", async () => {
await using tmp = await withTmpDir();
@@ -240,5 +243,5 @@ Deno.test("init - errors on missing build cache in prod", async () => {
const { stderr } = getStdOutput(cp);
expect(cp.code).toEqual(1);
expect(stderr).toMatch(/Found 1 islands, but did not/);
expect(stderr).toMatch(/Module not found/);
});