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
+101 -65
View File
@@ -1,16 +1,21 @@
import { expect } from "@std/expect";
import * as path from "@std/path";
import { Builder } from "./builder.ts";
import { Builder, specToName } from "./builder.ts";
import { App } from "../app.ts";
import { RemoteIsland } from "@marvinh-test/fresh-island";
import { BUILD_ID } from "../runtime/build_id.ts";
import { withTmpDir } from "../test_utils.ts";
Deno.test({
name: "Builder - chain onTransformStaticFile",
fn: async () => {
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
const logs: string[] = [];
const builder = new Builder();
const builder = new Builder({
outDir: path.join(tmp, "dist"),
staticDir: tmp,
});
builder.onTransformStaticFile(
{ pluginName: "A", filter: /\.css$/ },
() => {
@@ -30,16 +35,8 @@ Deno.test({
},
);
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
await Deno.writeTextFile(path.join(tmp, "foo.css"), "body { color: red; }");
const app = new App({
staticDir: tmp,
build: {
outDir: path.join(tmp, "dist"),
},
});
await builder.build(app);
await builder.build();
expect(logs).toEqual(["A", "B", "C"]);
},
@@ -50,24 +47,22 @@ Deno.test({
Deno.test({
name: "Builder - handles Windows paths",
fn: async () => {
const builder = new Builder();
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
const builder = new Builder({
outDir: path.join(tmp, "dist"),
staticDir: tmp,
});
await Deno.mkdir(path.join(tmp, "images"));
await Deno.writeTextFile(
path.join(tmp, "images", "batman.svg"),
"<svg></svg>",
);
const app = new App({
staticDir: tmp,
build: {
outDir: path.join(tmp, "dist"),
},
});
await builder.build(app);
await builder.build();
const snapshotJson = await Deno.readTextFile(
path.join(tmp, "dist", "snapshot.json"),
path.join(tmp, "dist", "static-files.json"),
);
expect(snapshotJson).toContain("/images/batman.svg");
},
@@ -78,20 +73,18 @@ Deno.test({
Deno.test({
name: "Builder - hashes CSS urls by default",
fn: async () => {
const builder = new Builder();
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
const builder = new Builder({
outDir: path.join(tmp, "dist"),
staticDir: tmp,
});
await Deno.writeTextFile(
path.join(tmp, "foo.css"),
"body { background: url('/foo.jpg'); }",
);
const app = new App({
staticDir: tmp,
build: {
outDir: path.join(tmp, "dist"),
},
});
await builder.build(app);
await builder.build();
const css = await Deno.readTextFile(
path.join(tmp, "dist", "static", "foo.css"),
@@ -106,20 +99,17 @@ Deno.test({
Deno.test({
name: "Builder - hashes CSS urls by default",
fn: async () => {
const builder = new Builder();
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
const builder = new Builder({
outDir: path.join(tmp, "dist"),
staticDir: tmp,
});
await Deno.writeTextFile(
path.join(tmp, "foo.css"),
`:root { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='rgb(76, 154.5, 137.5)' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='20 6 9 17 4 12'%3E%3C/polyline%3E%3C/svg%3E"); }`,
);
const app = new App({
staticDir: tmp,
build: {
outDir: path.join(tmp, "dist"),
},
});
await builder.build(app);
await builder.build();
const css = await Deno.readTextFile(
path.join(tmp, "dist", "static", "foo.css"),
@@ -135,20 +125,18 @@ Deno.test({
Deno.test({
name: "Builder - can bundle islands from JSR",
fn: async () => {
const builder = new Builder();
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
const app = new App({
staticDir: tmp,
build: {
outDir: path.join(tmp, "dist"),
},
});
app.island("jsr:@marvinh-test/fresh-island", "RemoteIsland", RemoteIsland);
const outDir = path.join(tmp, "dist");
const builder = new Builder({ outDir });
await builder.build(app);
const specifier = "jsr:@marvinh-test/fresh-island";
builder.registerIsland(specifier);
await builder.build();
const name = specToName(specifier);
const code = await Deno.readTextFile(
path.join(
tmp,
@@ -157,7 +145,7 @@ Deno.test({
"_fresh",
"js",
BUILD_ID,
"RemoteIsland.js",
`${name}.js`,
),
);
expect(code).toContain('"remote-island"');
@@ -169,8 +157,14 @@ Deno.test({
Deno.test({
name: "Builder - exclude files",
fn: async () => {
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
const logs: string[] = [];
const builder = new Builder();
const builder = new Builder({
outDir: path.join(tmp, "dist"),
staticDir: tmp,
});
// String
builder.onTransformStaticFile(
@@ -196,20 +190,12 @@ Deno.test({
},
);
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
await Deno.writeTextFile(path.join(tmp, "foo.css"), "body { color: red; }");
await Deno.writeTextFile(
path.join(tmp, "bar.css"),
"body { color: blue; }",
);
const app = new App({
staticDir: tmp,
build: {
outDir: path.join(tmp, "dist"),
},
});
await builder.build(app);
await builder.build();
expect(logs).toEqual(["A: bar.css", "B: bar.css", "C: bar.css"]);
},
@@ -220,17 +206,20 @@ Deno.test({
Deno.test({
name: "Builder - workspace folder middleware on listen",
fn: async () => {
const builder = new Builder();
const tmp = await Deno.makeTempDir();
const app = new App({
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
const builder = new Builder({
outDir: path.join(tmp, "dist"),
staticDir: tmp,
build: {
outDir: path.join(tmp, "dist"),
},
});
const app = new App();
const abort = new AbortController();
const port = 8011;
await builder.listen(app, { port, signal: abort.signal });
await builder.listen(() => Promise.resolve(app), {
port,
signal: abort.signal,
});
const res = await fetch(
`http://localhost:${port}/.well-known/appspecific/com.chrome.devtools.json`,
@@ -243,7 +232,7 @@ Deno.test({
expect(res.headers.get("etag")).toEqual(expect.any(String));
expect(json).toEqual({
workspace: {
root: app.config.root,
root: builder.config.root,
uuid: expect.any(String),
},
});
@@ -251,3 +240,50 @@ Deno.test({
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test("specToName", () => {
// HTTP
expect(specToName("http://example.com")).toEqual("example");
expect(specToName("http://example.com:8000")).toEqual("example");
expect(specToName("http://example.com:8000/foo/bar")).toEqual(
"bar",
);
// HTTPS
expect(specToName("https://example.com")).toEqual("example");
expect(specToName("https://example.com:8000")).toEqual("example");
expect(specToName("https://example.com:8000/foo/bar")).toEqual(
"bar",
);
// JSR
expect(specToName("jsr:@foo/bar")).toEqual("foo_bar");
expect(specToName("jsr:@foo/bar@1.0.0")).toEqual("foo_bar");
expect(specToName("jsr:@foo/bar@^1.0.0")).toEqual("foo_bar");
expect(specToName("jsr:@foo/bar@~1.0.0")).toEqual("foo_bar");
expect(specToName("jsr:@foo/bar@~1.0.0-alpha.32")).toEqual("foo_bar");
expect(specToName("jsr:@foo/bar@~1.0.0-alpha.32/asdf")).toEqual("asdf");
expect(specToName("jsr:@foo/bar/asdf")).toEqual("asdf");
// npm
expect(specToName("npm:foo")).toEqual("foo");
expect(specToName("npm:foo/bar")).toEqual("bar");
expect(specToName("npm:foo@1.0.0")).toEqual("foo");
expect(specToName("npm:foo@^1.0.0")).toEqual("foo");
expect(specToName("npm:foo@~1.0.0-alpha.32")).toEqual("foo");
expect(specToName("npm:@foo/bar")).toEqual("foo_bar");
expect(specToName("npm:@foo/bar/asdf")).toEqual("asdf");
expect(specToName("npm:@foo/bar@1.0.0")).toEqual("foo_bar");
expect(specToName("npm:@foo/bar@^1.0.0")).toEqual("foo_bar");
expect(specToName("npm:@foo/bar@~1.0.0-alpha.32")).toEqual("foo_bar");
// other
expect(specToName("foo")).toEqual("foo");
expect(specToName("@foo/bar")).toEqual("foo_bar");
expect(specToName("foo/bar")).toEqual("bar");
expect(specToName("@foo/bar/asdf")).toEqual("asdf");
expect(specToName("islands/foo.v2.tsx")).toEqual("foo_v2");
expect(specToName("/islands/_bar-baz-...-$.tsx")).toEqual("_bar_baz_$");
expect(specToName("/islands/1_hello.tsx")).toEqual("_hello");
});