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:
@@ -1,35 +1,24 @@
|
||||
import { App, staticFiles } from "fresh";
|
||||
import {
|
||||
allIslandApp,
|
||||
ALL_ISLAND_DIR,
|
||||
assertNotSelector,
|
||||
assertSelector,
|
||||
buildProd,
|
||||
Doc,
|
||||
getIsland,
|
||||
parseHtml,
|
||||
withBrowserApp,
|
||||
} from "./test_utils.tsx";
|
||||
|
||||
import { SelfCounter } from "./fixtures_islands/SelfCounter.tsx";
|
||||
import { PartialInIsland } from "./fixtures_islands/PartialInIsland.tsx";
|
||||
import { JsonIsland } from "./fixtures_islands/JsonIsland.tsx";
|
||||
import { FakeServer } from "../src/test_utils.ts";
|
||||
import { Partial } from "fresh/runtime";
|
||||
import { getBuildCache, setBuildCache } from "../src/app.ts";
|
||||
|
||||
await buildProd(allIslandApp);
|
||||
const allIslandCache = await buildProd({ islandDir: ALL_ISLAND_DIR });
|
||||
|
||||
function testApp<T>(): App<T> {
|
||||
const selfCounter = getIsland("SelfCounter.tsx");
|
||||
const partialInIsland = getIsland("PartialInIsland.tsx");
|
||||
const jsonIsland = getIsland("JsonIsland.tsx");
|
||||
|
||||
const app = new App<T>()
|
||||
.island(selfCounter, "SelfCounter", SelfCounter)
|
||||
.island(partialInIsland, "PartialInIsland", PartialInIsland)
|
||||
.island(jsonIsland, "JsonIsland", JsonIsland)
|
||||
.use(staticFiles());
|
||||
setBuildCache(app, getBuildCache(allIslandApp));
|
||||
|
||||
allIslandCache(app);
|
||||
return app;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { useSignal } from "@preact/signals";
|
||||
import { useEffect } from "preact/hooks";
|
||||
|
||||
export function Foo() {
|
||||
const active = useSignal(false);
|
||||
useEffect(() => {
|
||||
active.value = true;
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div id="foo-both" class={active.value ? "ready" : ""}>
|
||||
{active.value ? "it works" : "it doesn't work"}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { Foo } from "./(_islands)/Foo.tsx";
|
||||
import { Foo as Foo2 } from "../foo/(_islands)/Foo.tsx";
|
||||
|
||||
export default function Home() {
|
||||
return (
|
||||
<div>
|
||||
<Foo />
|
||||
<Foo2 />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -8,7 +8,7 @@ export function Foo() {
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div class={active.value ? "ready" : ""}>
|
||||
<div id="foo" class={active.value ? "ready" : ""}>
|
||||
{active.value ? "it works" : "it doesn't work"}
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { Builder } from "../../../src/dev/mod.ts";
|
||||
import { app } from "./main.tsx";
|
||||
|
||||
const builder = new Builder();
|
||||
|
||||
await builder.listen(app, {
|
||||
await builder.listen(() => import("./main.tsx"), {
|
||||
port: 4001,
|
||||
});
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { App } from "../../../src/app.ts";
|
||||
|
||||
const app = new App({ staticDir: "./static" }).get(
|
||||
const app = new App().get(
|
||||
"/",
|
||||
(ctx) =>
|
||||
ctx.render(
|
||||
|
||||
+48
-95
@@ -1,4 +1,4 @@
|
||||
import { App, fsRoutes, staticFiles } from "fresh";
|
||||
import { App, staticFiles } from "fresh";
|
||||
import { Counter } from "./fixtures_islands/Counter.tsx";
|
||||
import { IslandInIsland } from "./fixtures_islands/IslandInIsland.tsx";
|
||||
import { JsonIsland } from "./fixtures_islands/JsonIsland.tsx";
|
||||
@@ -12,42 +12,49 @@ import { JsxChildrenIsland } from "./fixtures_islands/JsxChildrenIsland.tsx";
|
||||
import { NodeProcess } from "./fixtures_islands/NodeProcess.tsx";
|
||||
import { signal } from "@preact/signals";
|
||||
import {
|
||||
allIslandApp,
|
||||
ALL_ISLAND_DIR,
|
||||
buildProd,
|
||||
Doc,
|
||||
getIsland,
|
||||
ISLAND_GROUP_DIR,
|
||||
withBrowserApp,
|
||||
} from "./test_utils.tsx";
|
||||
import { parseHtml, waitForText } from "./test_utils.tsx";
|
||||
import { expect } from "@std/expect";
|
||||
import { JsxConditional } from "./fixtures_islands/JsxConditional.tsx";
|
||||
import { FnIsland } from "./fixtures_islands/FnIsland.tsx";
|
||||
import { FragmentIsland } from "./fixtures_islands/FragmentIsland.tsx";
|
||||
import { EscapeIsland } from "./fixtures_islands/EscapeIsland.tsx";
|
||||
import * as path from "@std/path";
|
||||
import { setBuildCache } from "../src/app.ts";
|
||||
import { getBuildCache } from "../src/app.ts";
|
||||
import type { FreshConfig } from "../src/config.ts";
|
||||
import { FreshAttrs } from "./fixtures_islands/FreshAttrs.tsx";
|
||||
import { FakeServer } from "../src/test_utils.ts";
|
||||
import { PARTIAL_SEARCH_PARAM } from "../src/constants.ts";
|
||||
|
||||
await buildProd(allIslandApp);
|
||||
const allIslandCache = await buildProd({ islandDir: ALL_ISLAND_DIR });
|
||||
const islandGroupCache = await buildProd({ root: ISLAND_GROUP_DIR });
|
||||
|
||||
function testApp(config?: FreshConfig): App<unknown> {
|
||||
const app = new App(config)
|
||||
.use(staticFiles())
|
||||
.fsRoutes();
|
||||
|
||||
allIslandCache(app);
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
function testGroupApp(config?: FreshConfig): App<unknown> {
|
||||
const app = new App(config)
|
||||
.use(staticFiles())
|
||||
.fsRoutes();
|
||||
|
||||
islandGroupCache(app);
|
||||
|
||||
function testApp(config?: FreshConfig) {
|
||||
const app = new App(config);
|
||||
setBuildCache(app, getBuildCache(allIslandApp));
|
||||
app.use(staticFiles());
|
||||
return app;
|
||||
}
|
||||
|
||||
Deno.test({
|
||||
name: "islands - should make signals interactive",
|
||||
fn: async () => {
|
||||
const counterIsland = getIsland("Counter.tsx");
|
||||
|
||||
const app = testApp()
|
||||
.island(counterIsland, "Counter", Counter)
|
||||
.get("/", (ctx) => {
|
||||
const sig = signal(3);
|
||||
return ctx.render(
|
||||
@@ -69,11 +76,7 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "islands - revive multiple islands from one island file",
|
||||
fn: async () => {
|
||||
const multipleIslands = getIsland("Multiple.tsx");
|
||||
|
||||
const app = testApp()
|
||||
.island(multipleIslands, "Multiple1", Multiple1)
|
||||
.island(multipleIslands, "Multiple2", Multiple2)
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
@@ -98,10 +101,7 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "islands - revive multiple islands with shared signal",
|
||||
fn: async () => {
|
||||
const counterIsland = getIsland("Counter.tsx");
|
||||
|
||||
const app = testApp()
|
||||
.island(counterIsland, "Counter", Counter)
|
||||
.get("/", (ctx) => {
|
||||
const sig = signal(0);
|
||||
return ctx.render(
|
||||
@@ -126,10 +126,7 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "islands - import json",
|
||||
fn: async () => {
|
||||
const jsonIsland = getIsland("JsonIsland.tsx");
|
||||
|
||||
const app = testApp()
|
||||
.island(jsonIsland, "JsonIsland", Counter)
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
@@ -153,10 +150,7 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "islands - returns null",
|
||||
fn: async () => {
|
||||
const nullIsland = getIsland("NullIsland.tsx");
|
||||
|
||||
const app = testApp()
|
||||
.island(nullIsland, "NullIsland", NullIsland)
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
@@ -175,12 +169,7 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "islands - only instantiate top level island",
|
||||
fn: async () => {
|
||||
const counter = getIsland("Counter.tsx");
|
||||
const islandInIsland = getIsland("IslandInIsland.tsx");
|
||||
|
||||
const app = testApp()
|
||||
.island(counter, "Counter", Counter)
|
||||
.island(islandInIsland, "IslandInIsland", IslandInIsland)
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
@@ -204,10 +193,7 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "islands - pass null JSX props to islands",
|
||||
fn: async () => {
|
||||
const jsxIsland = getIsland("JsxIsland.tsx");
|
||||
|
||||
const app = testApp()
|
||||
.island(jsxIsland, "JsxIsland", JsxIsland)
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
@@ -231,10 +217,7 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "islands - pass JSX props to islands",
|
||||
fn: async () => {
|
||||
const jsxIsland = getIsland("JsxIsland.tsx");
|
||||
|
||||
const app = testApp()
|
||||
.island(jsxIsland, "JsxIsland", JsxIsland)
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
@@ -260,10 +243,7 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "islands - never serialize children prop",
|
||||
fn: async () => {
|
||||
const jsxChildrenIsland = getIsland("JsxChildrenIsland.tsx");
|
||||
|
||||
const app = testApp()
|
||||
.island(jsxChildrenIsland, "JsxChildrenIsland", JsxChildrenIsland)
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
@@ -294,12 +274,7 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "islands - instantiate islands in jsx children",
|
||||
fn: async () => {
|
||||
const passThrough = getIsland("PassThrough.tsx");
|
||||
const selfCounter = getIsland("SelfCounter.tsx");
|
||||
|
||||
const app = testApp()
|
||||
.island(passThrough, "PassThrough", PassThrough)
|
||||
.island(selfCounter, "SelfCounter", SelfCounter)
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
@@ -325,12 +300,7 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "islands - instantiate islands in jsx children with slots",
|
||||
fn: async () => {
|
||||
const counterWithSlots = getIsland("CounterWithSlots.tsx");
|
||||
const selfCounter = getIsland("SelfCounter.tsx");
|
||||
|
||||
const app = testApp()
|
||||
.island(counterWithSlots, "CounterWithSlots", CounterWithSlots)
|
||||
.island(selfCounter, "SelfCounter", SelfCounter)
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
@@ -367,12 +337,7 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "islands - nested children slots",
|
||||
fn: async () => {
|
||||
const passThrough = getIsland("PassThrough.tsx");
|
||||
const selfCounter = getIsland("SelfCounter.tsx");
|
||||
|
||||
const app = testApp()
|
||||
.island(passThrough, "PassThrough", PassThrough)
|
||||
.island(selfCounter, "SelfCounter", SelfCounter)
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
@@ -408,12 +373,7 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "islands - conditional jsx children",
|
||||
fn: async () => {
|
||||
const jsxConditional = getIsland("JsxConditional.tsx");
|
||||
const selfCounter = getIsland("SelfCounter.tsx");
|
||||
|
||||
const app = testApp()
|
||||
.island(jsxConditional, "JsxConditional", JsxConditional)
|
||||
.island(selfCounter, "SelfCounter", SelfCounter)
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
@@ -450,10 +410,7 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "islands - revive DOM attributes",
|
||||
fn: async () => {
|
||||
const jsxConditional = getIsland("JsxConditional.tsx");
|
||||
|
||||
const app = testApp()
|
||||
.island(jsxConditional, "JsxConditional", JsxConditional)
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
@@ -523,12 +480,7 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "islands - revive island with fn inside",
|
||||
fn: async () => {
|
||||
const fragmentIsland = getIsland("FragmentIsland.tsx");
|
||||
const fnIsland = getIsland("FnIsland.tsx");
|
||||
|
||||
const app = testApp()
|
||||
.island(fragmentIsland, "FragmentIsland", FragmentIsland)
|
||||
.island(fnIsland, "FnIsland", FnIsland)
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
@@ -552,10 +504,7 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "islands - escape props",
|
||||
fn: async () => {
|
||||
const escapeIsland = getIsland("EscapeIsland.tsx");
|
||||
|
||||
const app = testApp()
|
||||
.island(escapeIsland, "EscapeIsland", EscapeIsland)
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
@@ -648,10 +597,7 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "islands - stub Node 'process.env'",
|
||||
fn: async () => {
|
||||
const nodeProcess = getIsland("NodeProcess.tsx");
|
||||
|
||||
const app = testApp()
|
||||
.island(nodeProcess, "NodeProcess", NodeProcess)
|
||||
.get("/", (ctx) =>
|
||||
ctx.render(
|
||||
<Doc>
|
||||
@@ -675,10 +621,7 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "islands - in base path",
|
||||
fn: async () => {
|
||||
const selfCounter = getIsland("SelfCounter.tsx");
|
||||
|
||||
const app = testApp({ basePath: "/foo" })
|
||||
.island(selfCounter, "SelfCounter", SelfCounter)
|
||||
.get("/", (ctx) =>
|
||||
ctx.render(
|
||||
<Doc>
|
||||
@@ -699,10 +642,7 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "islands - preserve f-* attributes",
|
||||
fn: async () => {
|
||||
const freshAttrs = getIsland("FreshAttrs.tsx");
|
||||
|
||||
const app = testApp()
|
||||
.island(freshAttrs, "FreshAttrs", FreshAttrs)
|
||||
.get("/", (ctx) =>
|
||||
ctx.render(
|
||||
<Doc>
|
||||
@@ -728,16 +668,7 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "fsRoutes - load islands from group folder",
|
||||
fn: async () => {
|
||||
const app = testApp();
|
||||
|
||||
await fsRoutes(app, {
|
||||
dir: path.join(
|
||||
import.meta.dirname!,
|
||||
"fixture_island_groups",
|
||||
),
|
||||
loadIsland: (path) => import("./fixture_island_groups/islands/" + path),
|
||||
loadRoute: (path) => import("./fixture_island_groups/routes/" + path),
|
||||
});
|
||||
const app = testGroupApp();
|
||||
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/foo`, { waitUntil: "load" });
|
||||
@@ -752,13 +683,34 @@ Deno.test({
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "fsRoutes - load islands from group folder with same name",
|
||||
fn: async () => {
|
||||
const app = testGroupApp();
|
||||
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/both`, { waitUntil: "load" });
|
||||
|
||||
await page.locator(".ready").wait();
|
||||
|
||||
// Page would error here
|
||||
let text = await page
|
||||
.locator<HTMLDivElement>("#foo.ready")
|
||||
.evaluate((el) => el.textContent!);
|
||||
expect(text).toEqual("it works");
|
||||
|
||||
text = await page
|
||||
.locator<HTMLDivElement>("#foo-both.ready")
|
||||
.evaluate((el) => el.textContent!);
|
||||
expect(text).toEqual("it works");
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "islands - adds preload HTTP headers",
|
||||
fn: async () => {
|
||||
const selfCounter = getIsland("SelfCounter.tsx");
|
||||
|
||||
const app = testApp()
|
||||
.island(selfCounter, "SelfCounter", SelfCounter)
|
||||
.get("/", (ctx) =>
|
||||
ctx.render(
|
||||
<Doc>
|
||||
@@ -771,6 +723,7 @@ Deno.test({
|
||||
await res.body?.cancel();
|
||||
|
||||
const link = res.headers.get("Link");
|
||||
|
||||
expect(link).toMatch(
|
||||
/<\/_fresh\/js\/[a-zA-Z0-9]+\/fresh-runtime\.js>; rel="modulepreload"; as="script", <\/_fresh\/js\/[a-zA-Z0-9]+\/SelfCounter\.js>; rel="modulepreload"; as="script"/,
|
||||
);
|
||||
|
||||
+6
-16
@@ -1,14 +1,13 @@
|
||||
import { App, staticFiles } from "fresh";
|
||||
import { Partial } from "fresh/runtime";
|
||||
import {
|
||||
allIslandApp,
|
||||
ALL_ISLAND_DIR,
|
||||
assertMetaContent,
|
||||
assertNotSelector,
|
||||
buildProd,
|
||||
charset,
|
||||
Doc,
|
||||
favicon,
|
||||
getIsland,
|
||||
parseHtml,
|
||||
waitFor,
|
||||
waitForText,
|
||||
@@ -21,29 +20,20 @@ import { FakeServer } from "../src/test_utils.ts";
|
||||
import { JsonIsland } from "./fixtures_islands/JsonIsland.tsx";
|
||||
import { OptOutPartialLink } from "./fixtures_islands/OptOutPartialLink.tsx";
|
||||
import * as path from "@std/path";
|
||||
import { getBuildCache, setBuildCache } from "../src/app.ts";
|
||||
import { retry } from "@std/async/retry";
|
||||
|
||||
const loremIpsum = await Deno.readTextFile(
|
||||
path.join(import.meta.dirname!, "lorem_ipsum.txt"),
|
||||
);
|
||||
|
||||
await buildProd(allIslandApp);
|
||||
const applyBuildCache = await buildProd({ islandDir: ALL_ISLAND_DIR });
|
||||
|
||||
function testApp<T>(): App<T> {
|
||||
const selfCounter = getIsland("SelfCounter.tsx");
|
||||
const partialInIsland = getIsland("PartialInIsland.tsx");
|
||||
const jsonIsland = getIsland("JsonIsland.tsx");
|
||||
const optOutPartialLink = getIsland("OptOutPartialLink.tsx");
|
||||
|
||||
const app = new App<T>()
|
||||
.island(selfCounter, "SelfCounter", SelfCounter)
|
||||
.island(partialInIsland, "PartialInIsland", PartialInIsland)
|
||||
.island(jsonIsland, "JsonIsland", JsonIsland)
|
||||
.island(optOutPartialLink, "OptOutPartialLink", OptOutPartialLink)
|
||||
.use(staticFiles());
|
||||
|
||||
setBuildCache(app, getBuildCache(allIslandApp));
|
||||
applyBuildCache<T>(app);
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
@@ -305,7 +295,7 @@ Deno.test({
|
||||
);
|
||||
});
|
||||
|
||||
await buildProd(app);
|
||||
applyBuildCache(app);
|
||||
const server = new FakeServer(app.handler());
|
||||
let checked = false;
|
||||
try {
|
||||
@@ -452,7 +442,7 @@ Deno.test({
|
||||
await page.locator(".partial-update").wait();
|
||||
|
||||
const doc = parseHtml(await page.content());
|
||||
const raw = JSON.parse(doc.querySelector("pre")?.textContent!);
|
||||
const raw = JSON.parse(doc.querySelector("pre")!.textContent!);
|
||||
expect(raw).toEqual({ foo: 123 });
|
||||
|
||||
assertNotSelector(doc, ".output");
|
||||
|
||||
+34
-80
@@ -1,32 +1,13 @@
|
||||
import { App, getIslandRegistry, setBuildCache } from "../src/app.ts";
|
||||
import type { App } from "../src/app.ts";
|
||||
import { launch, type Page } from "@astral/astral";
|
||||
import * as colors from "@std/fmt/colors";
|
||||
import { DOMParser, HTMLElement } from "linkedom";
|
||||
import { Builder } from "../src/dev/builder.ts";
|
||||
import { Builder, type BuildOptions } from "../src/dev/builder.ts";
|
||||
import { TextLineStream } from "@std/streams/text-line-stream";
|
||||
import * as path from "@std/path";
|
||||
import type { ComponentChildren } from "preact";
|
||||
import { expect } from "@std/expect";
|
||||
import { ProdBuildCache } from "../src/build_cache.ts";
|
||||
import { Counter } from "./fixtures_islands/Counter.tsx";
|
||||
import { CounterWithSlots } from "./fixtures_islands/CounterWithSlots.tsx";
|
||||
import { EscapeIsland } from "./fixtures_islands/EscapeIsland.tsx";
|
||||
import { FnIsland } from "./fixtures_islands/FnIsland.tsx";
|
||||
import { FragmentIsland } from "./fixtures_islands/FragmentIsland.tsx";
|
||||
import { IslandInIsland } from "./fixtures_islands/IslandInIsland.tsx";
|
||||
import { JsonIsland } from "./fixtures_islands/JsonIsland.tsx";
|
||||
import { JsxChildrenIsland } from "./fixtures_islands/JsxChildrenIsland.tsx";
|
||||
import { JsxConditional } from "./fixtures_islands/JsxConditional.tsx";
|
||||
import { JsxIsland } from "./fixtures_islands/JsxIsland.tsx";
|
||||
import { NullIsland } from "./fixtures_islands/NullIsland.tsx";
|
||||
import { PartialInIsland } from "./fixtures_islands/PartialInIsland.tsx";
|
||||
import { PassThrough } from "./fixtures_islands/PassThrough.tsx";
|
||||
import { SelfCounter } from "./fixtures_islands/SelfCounter.tsx";
|
||||
import { Multiple1, Multiple2 } from "./fixtures_islands/Multiple.tsx";
|
||||
import { Foo } from "./fixture_island_groups/routes/foo/(_islands)/Foo.tsx";
|
||||
import { NodeProcess } from "./fixtures_islands/NodeProcess.tsx";
|
||||
import { FreshAttrs } from "./fixtures_islands/FreshAttrs.tsx";
|
||||
import { OptOutPartialLink } from "./fixtures_islands/OptOutPartialLink.tsx";
|
||||
import { mergeReadableStreams } from "@std/streams";
|
||||
|
||||
const browser = await launch({
|
||||
args: [
|
||||
@@ -35,17 +16,9 @@ const browser = await launch({
|
||||
? ["--no-sandbox"]
|
||||
: []),
|
||||
],
|
||||
headless: true,
|
||||
headless: Deno.env.get("HEADLESS") !== "false",
|
||||
});
|
||||
|
||||
export function getIsland(pathname: string) {
|
||||
return path.join(
|
||||
import.meta.dirname!,
|
||||
"fixtures_islands",
|
||||
pathname,
|
||||
);
|
||||
}
|
||||
|
||||
export const charset = <meta charset="utf-8" />;
|
||||
|
||||
export const favicon = (
|
||||
@@ -71,17 +44,21 @@ export function Doc(props: { children?: ComponentChildren; title?: string }) {
|
||||
);
|
||||
}
|
||||
|
||||
export async function buildProd(app: App<unknown>) {
|
||||
export const ALL_ISLAND_DIR = path.join(
|
||||
import.meta.dirname!,
|
||||
"fixtures_islands",
|
||||
);
|
||||
export const ISLAND_GROUP_DIR = path.join(
|
||||
import.meta.dirname!,
|
||||
"fixture_island_groups",
|
||||
);
|
||||
|
||||
export async function buildProd(
|
||||
options: Omit<BuildOptions, "outDir">,
|
||||
): Promise<<T>(app: App<T>) => void> {
|
||||
const outDir = await Deno.makeTempDir();
|
||||
// FIXME: Sharing build output path is weird
|
||||
app.config.build.outDir = outDir;
|
||||
const builder = new Builder({});
|
||||
await builder.build(app);
|
||||
const cache = await ProdBuildCache.fromSnapshot(
|
||||
app.config,
|
||||
getIslandRegistry(app).size,
|
||||
);
|
||||
setBuildCache(app, cache);
|
||||
const builder = new Builder({ outDir, ...options });
|
||||
return await builder.build({ mode: "production", snapshot: "memory" });
|
||||
}
|
||||
|
||||
export async function withBrowserApp(
|
||||
@@ -128,15 +105,21 @@ export async function withChildProcessServer(
|
||||
args: ["task", task],
|
||||
stdin: "null",
|
||||
stdout: "piped",
|
||||
stderr: "inherit",
|
||||
stderr: "piped",
|
||||
cwd: dir,
|
||||
signal: aborter.signal,
|
||||
}).spawn();
|
||||
|
||||
const lines: ReadableStream<string> = cp.stdout
|
||||
const linesStdout: ReadableStream<string> = cp.stdout
|
||||
.pipeThrough(new TextDecoderStream())
|
||||
.pipeThrough(new TextLineStream());
|
||||
|
||||
const linesStderr: ReadableStream<string> = cp.stderr
|
||||
.pipeThrough(new TextDecoderStream())
|
||||
.pipeThrough(new TextLineStream());
|
||||
|
||||
const lines = mergeReadableStreams(linesStdout, linesStderr);
|
||||
|
||||
const output: string[] = [];
|
||||
let address = "";
|
||||
let found = false;
|
||||
@@ -348,40 +331,11 @@ export function getStdOutput(
|
||||
return { stdout, stderr };
|
||||
}
|
||||
|
||||
export const allIslandApp = new App()
|
||||
.island(getIsland("Counter.tsx"), "Counter", Counter)
|
||||
.island(
|
||||
getIsland("CounterWithSlots.tsx"),
|
||||
"CounterWithSlots",
|
||||
CounterWithSlots,
|
||||
)
|
||||
.island(getIsland("EscapeIsland.tsx"), "EscapeIsland", EscapeIsland)
|
||||
.island(getIsland("FnIsland.tsx"), "FnIsland", FnIsland)
|
||||
.island(getIsland("FragmentIsland.tsx"), "FragmentIsland", FragmentIsland)
|
||||
.island(getIsland("IslandInIsland.tsx"), "IslandInIsland", IslandInIsland)
|
||||
.island(getIsland("JsonIsland.tsx"), "JsonIsland", JsonIsland)
|
||||
.island(
|
||||
getIsland("JsxChildrenIsland.tsx"),
|
||||
"JsxChildrenIsland",
|
||||
JsxChildrenIsland,
|
||||
)
|
||||
.island(getIsland("JsxConditional.tsx"), "JsxConditional", JsxConditional)
|
||||
.island(getIsland("JsxIsland.tsx"), "JsxIsland", JsxIsland)
|
||||
.island(getIsland("Multiple.tsx"), "Multiple1", Multiple1)
|
||||
.island(getIsland("Multiple.tsx"), "Multiple2", Multiple2)
|
||||
.island(getIsland("NullIsland.tsx"), "NullIsland", NullIsland)
|
||||
.island(getIsland("PartialInIsland.tsx"), "PartialInIsland", PartialInIsland)
|
||||
.island(getIsland("PassThrough.tsx"), "PassThrough", PassThrough)
|
||||
.island(getIsland("SelfCounter.tsx"), "SelfCounter", SelfCounter)
|
||||
.island(getIsland("NodeProcess.tsx"), "NodeProcess", NodeProcess)
|
||||
.island(getIsland("FreshAttrs.tsx"), "FreshAttrs", FreshAttrs)
|
||||
.island(
|
||||
getIsland("OptOutPartialLink.tsx"),
|
||||
"OptOutPartialLink",
|
||||
OptOutPartialLink,
|
||||
)
|
||||
.island(
|
||||
getIsland("../fixture_island_groups/routes/foo/(_islands)/Foo.tsx"),
|
||||
"Foo",
|
||||
Foo,
|
||||
);
|
||||
const ISLAND_FIXTURE_DIR = path.join(import.meta.dirname!, "fixtures_islands");
|
||||
const allIslandBuilder = new Builder({});
|
||||
for await (const entry of Deno.readDirSync(ISLAND_FIXTURE_DIR)) {
|
||||
if (entry.name.endsWith(".json")) continue;
|
||||
|
||||
const spec = path.join(ISLAND_FIXTURE_DIR, entry.name);
|
||||
allIslandBuilder.registerIsland(spec);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user