From 325850719ab8486b85fe21a212a4ec7128443b83 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Tue, 28 Nov 2023 17:23:27 +0100 Subject: [PATCH] feat: reduce number of island bundles (#2082) --- src/dev/manifest.ts | 53 +++------------- src/runtime/entrypoints/main.ts | 19 +++--- src/server/context.ts | 9 ++- src/server/deps.ts | 4 -- src/server/fs_extract.ts | 4 +- src/server/init_safe_deps.ts | 9 +++ src/server/rendering/fresh_tags.tsx | 14 +++-- src/server/rendering/preact_hooks.ts | 7 +-- src/server/router.ts | 2 +- tests/build_test.ts | 4 +- tests/fixture_build/fresh.gen.ts | 6 +- tests/fixture_build_out_dir/fresh.gen.ts | 6 +- .../src/fresh.gen.ts | 6 +- .../src/fresh.gen.ts | 6 +- tests/fixture_build_sub_dir/src/fresh.gen.ts | 6 +- tests/fixture_build_target/fresh.gen.ts | 6 +- tests/fixture_config/fresh.gen.ts | 2 +- tests/fixture_islands_multiple/deno.json | 14 +++++ tests/fixture_islands_multiple/dev.ts | 5 ++ tests/fixture_islands_multiple/fresh.gen.ts | 23 +++++++ .../islands/Multiple.tsx | 21 +++++++ .../islands/MultipleDefault.tsx | 31 ++++++++++ .../islands/Single.tsx | 12 ++++ tests/fixture_islands_multiple/main.ts | 10 +++ .../fixture_islands_multiple/routes/index.tsx | 22 +++++++ tests/fixture_partials/fresh.gen.ts | 62 +++++++++---------- tests/fixture_plugin_lifecycle/fresh.gen.ts | 2 +- tests/fixture_tailwind_build/fresh.gen.ts | 2 +- tests/init_test.ts | 11 +++- tests/islands_test.ts | 36 +++++++++++ tests/main_test.ts | 2 +- tests/update_test.ts | 2 + www/fresh.gen.ts | 26 ++++---- 33 files changed, 302 insertions(+), 142 deletions(-) create mode 100644 src/server/init_safe_deps.ts create mode 100644 tests/fixture_islands_multiple/deno.json create mode 100755 tests/fixture_islands_multiple/dev.ts create mode 100644 tests/fixture_islands_multiple/fresh.gen.ts create mode 100644 tests/fixture_islands_multiple/islands/Multiple.tsx create mode 100644 tests/fixture_islands_multiple/islands/MultipleDefault.tsx create mode 100644 tests/fixture_islands_multiple/islands/Single.tsx create mode 100644 tests/fixture_islands_multiple/main.ts create mode 100644 tests/fixture_islands_multiple/routes/index.tsx diff --git a/src/dev/manifest.ts b/src/dev/manifest.ts index a5434fec..05c98076 100644 --- a/src/dev/manifest.ts +++ b/src/dev/manifest.ts @@ -1,4 +1,7 @@ -import { isIdentifierChar, isIdentifierStart } from "$fresh/src/server/deps.ts"; +import { + isIdentifierChar, + isIdentifierStart, +} from "../server/init_safe_deps.ts"; import { extname, join, normalize } from "./deps.ts"; /** @@ -72,18 +75,12 @@ export async function generate(directory: string, manifest: Manifest) { normalizedRoutes.set(specifier, identifier); } - type islandManifest = { - specifier: string; - identifier: string; - importLine: string; - }; - const normalizedIslands: islandManifest[] = []; + const normalizedIslands: { specifier: string; identifier: string }[] = []; for (let i = 0; i < islands.length; i++) { const file = islands[i]; const specifier = toImportSpecifier(file); const identifier = specifierToIdentifier(specifier, used); - const importLine = await islandImportLine(specifier, identifier, directory); - normalizedIslands.push({ specifier, identifier, importLine }); + normalizedIslands.push({ specifier, identifier }); } const output = `// DO NOT EDIT. This file is generated by Fresh. @@ -99,11 +96,11 @@ ${ } ${ normalizedIslands.map(({ specifier, identifier }) => - `import * as $$${identifier} from "${specifier}";` + `import * as $${identifier} from "${specifier}";` ) .join("\n") } -import { Manifest } from "$fresh/server.ts"; +import { type Manifest } from "$fresh/server.ts"; const manifest = { routes: { @@ -116,8 +113,8 @@ const manifest = { }, islands: { ${ - normalizedIslands.map(({ specifier, importLine }) => - `${JSON.stringify(`${specifier}`)}: ${importLine},` + normalizedIslands.map(({ specifier, identifier }) => + `${JSON.stringify(`${specifier}`)}: $${identifier},` ) .join("\n ") } @@ -153,33 +150,3 @@ export default manifest; "color: blue; font-weight: bold", ); } - -async function islandImportLine( - specifier: string, - identifier: string, - directory: string, -): Promise { - const moduleURL = new URL(`file://${directory}/${specifier}`); - const module = await import(moduleURL.href); - const entries = Object.entries(module); - - const allExportsAreComponents = entries.every( - ([, exportedEntity]) => typeof exportedEntity === "function", - ); - - // if all exports are components, we can use the simple, existing form - if (allExportsAreComponents) { - return `$$${identifier}`; - } - - // if not all exports are components, list the component exports explicitly - const componentExports = entries - .filter(([, exportedEntity]) => typeof exportedEntity === "function") - .map(([exportName]) => exportName); - - const exportsString = componentExports - .map((exp) => `${exp}: $$${identifier}.${exp}`) - .join(", "); - - return `{ ${exportsString} }`; -} diff --git a/src/runtime/entrypoints/main.ts b/src/runtime/entrypoints/main.ts index ac40fe48..cd43b66a 100644 --- a/src/runtime/entrypoints/main.ts +++ b/src/runtime/entrypoints/main.ts @@ -82,7 +82,7 @@ function isElementNode(node: Node): node is HTMLElement { return node.nodeType === Node.ELEMENT_NODE && !("_frshRootFrag" in node); } -type IslandRegistry = Record>; +type IslandRegistry = Record; export function revive( islands: IslandRegistry, @@ -212,7 +212,7 @@ function hideMarker(marker: Marker) { * the client. This function checks for that */ function addChildrenFromTemplate( - islands: Record>, + islands: IslandRegistry, // deno-lint-ignore no-explicit-any props: any[], markerStack: Marker[], @@ -220,11 +220,11 @@ function addChildrenFromTemplate( comment: string, result: RenderRequest[], ) { - const [id, exportName, n] = comment.slice("/frsh-".length).split( + const [id, n] = comment.slice("/frsh-".length).split( ":", ); - const sel = `#frsh-slot-${id}-${exportName}-${n}-children`; + const sel = `#frsh-slot-${id}-${n}-children`; const template = document.querySelector(sel) as | HTMLTemplateElement | null; @@ -280,7 +280,7 @@ function addChildrenFromTemplate( * fashion over an HTMLElement's children list. */ function _walkInner( - islands: Record>, + islands: IslandRegistry, // deno-lint-ignore no-explicit-any props: any[], markerStack: Marker[], @@ -437,7 +437,7 @@ function _walkInner( } } else if (comment.startsWith("frsh")) { // We're opening a new island - const [id, exportName, n, key] = comment.slice(5).split(":"); + const [id, n, key] = comment.slice(5).split(":"); const islandProps = props[Number(n)]; markerStack.push({ @@ -447,7 +447,7 @@ function _walkInner( kind: MarkerKind.Island, }); - const vnode = h(islands[id][exportName], islandProps) as VNode; + const vnode = h(islands[id], islandProps) as VNode; if (key) vnode.key = key; vnodeStack.push(vnode); } @@ -654,7 +654,7 @@ export async function applyPartials(res: Response): Promise { const islands: IslandRegistry = {}; const dataRaw = doc.getElementById("__FRSH_PARTIAL_DATA")!; let data: { - islands: Record; + islands: Record; signals: string | null; deserializer: string | null; } | null = null; @@ -663,7 +663,8 @@ export async function applyPartials(res: Response): Promise { promises.push( ...Array.from(Object.entries(data!.islands)).map(async (entry) => { - islands[entry[0]] = await import(`${entry[1]}`); + const mod = await import(`${entry[1].url}`); + islands[entry[0]] = mod[entry[1].export]; }), ); } diff --git a/src/server/context.ts b/src/server/context.ts index a0e0d49a..811d18e4 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -702,9 +702,14 @@ function collectEntrypoints( ? import.meta.resolve(`${entrypointBase}/main_dev.ts`) : import.meta.resolve(`${entrypointBase}/main.ts`), deserializer: import.meta.resolve(`${entrypointBase}/deserializer.ts`), - fresh_dev_client: import.meta.resolve(`${entrypointBase}/client.ts`), }; + if (dev) { + entryPoints.fresh_dev_client = import.meta.resolve( + `${entrypointBase}/client.ts`, + ); + } + try { import.meta.resolve("@preact/signals"); entryPoints.signals = import.meta.resolve(`${entrypointBase}/signals.ts`); @@ -713,7 +718,7 @@ function collectEntrypoints( } for (const island of islands) { - entryPoints[`island-${island.id}`] = island.url; + entryPoints[`island-${island.name}`] = island.url; } for (const plugin of plugins) { diff --git a/src/server/deps.ts b/src/server/deps.ts index 8aa6283f..13220051 100644 --- a/src/server/deps.ts +++ b/src/server/deps.ts @@ -26,7 +26,3 @@ export { assertEquals, assertThrows, } from "https://deno.land/std@0.205.0/assert/mod.ts"; -export { - isIdentifierChar, - isIdentifierStart, -} from "https://esm.sh/@babel/helper-validator-identifier@7.22.20"; diff --git a/src/server/fs_extract.ts b/src/server/fs_extract.ts index bc0b3620..4c119a2e 100644 --- a/src/server/fs_extract.ts +++ b/src/server/fs_extract.ts @@ -289,8 +289,8 @@ export async function extractRoutes( ) ) { if (typeof exportedFunction !== "function") continue; - const name = processedIsland.name; - const id = `${name}_${exportName}`.toLowerCase(); + const name = processedIsland.name.toLowerCase(); + const id = `${name}_${exportName.toLowerCase()}`; islands.push({ id, name, diff --git a/src/server/init_safe_deps.ts b/src/server/init_safe_deps.ts new file mode 100644 index 00000000..f51b9032 --- /dev/null +++ b/src/server/init_safe_deps.ts @@ -0,0 +1,9 @@ +// This file includes dependencies that are safe to use even +// when the user has no `deno.json` in their project folder. +// This commonly occurs when the user is bootstrapping a new +// project. + +export { + isIdentifierChar, + isIdentifierStart, +} from "https://esm.sh/@babel/helper-validator-identifier@7.22.20"; diff --git a/src/server/rendering/fresh_tags.tsx b/src/server/rendering/fresh_tags.tsx index d57cfe1d..bf0f41cf 100644 --- a/src/server/rendering/fresh_tags.tsx +++ b/src/server/rendering/fresh_tags.tsx @@ -131,15 +131,19 @@ export function renderFreshTags( // Finally, it loads all island scripts and hydrates the islands using the // reviver from the "main" script. let islandRegistry = ""; - const islandMapping: Record = {}; + const islandMapping: Record = {}; if (renderState.encounteredIslands.size > 0) { // Prepare the inline script that loads and revives the islands for (const island of renderState.encounteredIslands) { - const url = addImport(`island-${island.id}.js`); - script += - `import * as ${island.name}_${island.exportName} from "${url}";`; + const url = addImport(`island-${island.name}.js`); + script += island.exportName === "default" + ? `import ${island.name}_${island.exportName} from "${url}";` + : `import { ${island.exportName} as ${island.name}_${island.exportName} } from "${url}";`; islandRegistry += `${island.id}:${island.name}_${island.exportName},`; - islandMapping[island.id] = url; + islandMapping[island.id] = { + export: island.exportName, + url, + }; } } diff --git a/src/server/rendering/preact_hooks.ts b/src/server/rendering/preact_hooks.ts index 72f9cc93..2f75cc1b 100644 --- a/src/server/rendering/preact_hooks.ts +++ b/src/server/rendering/preact_hooks.ts @@ -331,8 +331,7 @@ options.__b = (vnode: VNode>) => { ); } - const markerText = - `frsh-slot-${island.id}:${island.exportName}:${id}:children`; + const markerText = `frsh-slot-${island.id}:${id}:children`; // @ts-ignore nonono props.children = wrapWithMarker( children, @@ -354,9 +353,7 @@ options.__b = (vnode: VNode>) => { return wrapWithMarker( child, - `frsh-${island.id}:${island.exportName}:${islandProps.length - 1}:${ - vnode.key ?? "" - }`, + `frsh-${island.id}:${islandProps.length - 1}:${vnode.key ?? ""}`, ); }; // deno-lint-ignore no-explicit-any diff --git a/src/server/router.ts b/src/server/router.ts index 36d916d4..23e5d002 100644 --- a/src/server/router.ts +++ b/src/server/router.ts @@ -1,6 +1,6 @@ import { PARTIAL_SEARCH_PARAM } from "../constants.ts"; import { BaseRoute, ErrorHandlerContext, ServeHandlerInfo } from "./types.ts"; -import { isIdentifierChar, isIdentifierStart } from "./deps.ts"; +import { isIdentifierChar, isIdentifierStart } from "./init_safe_deps.ts"; type HandlerContext = T & ServeHandlerInfo & { isPartial: boolean; diff --git a/tests/build_test.ts b/tests/build_test.ts index c657d403..d01da7d1 100644 --- a/tests/build_test.ts +++ b/tests/build_test.ts @@ -63,7 +63,7 @@ async function testBuild( await t.step("check snapshot file", async () => { assert( - Array.isArray(snapshot.files["island-counter_default.js"]), + Array.isArray(snapshot.files["island-counter.js"]), "Island output file not found in snapshot", ); assert( @@ -252,7 +252,7 @@ Deno.test("pass target options", async () => { const { stdout, stderr } = getStdOutput(out); const txt = await Deno.readTextFile( - path.join(fixture, "_fresh", "island-counter_default.js"), + path.join(fixture, "_fresh", "island-counter.js"), ); assertNotMatch( diff --git a/tests/fixture_build/fresh.gen.ts b/tests/fixture_build/fresh.gen.ts index 7f26b684..4b4066a4 100644 --- a/tests/fixture_build/fresh.gen.ts +++ b/tests/fixture_build/fresh.gen.ts @@ -3,15 +3,15 @@ // This file is automatically updated during development when running `dev.ts`. import * as $index from "./routes/index.tsx"; -import * as $$Counter from "./islands/Counter.tsx"; -import { Manifest } from "$fresh/server.ts"; +import * as $Counter from "./islands/Counter.tsx"; +import { type Manifest } from "$fresh/server.ts"; const manifest = { routes: { "./routes/index.tsx": $index, }, islands: { - "./islands/Counter.tsx": $$Counter, + "./islands/Counter.tsx": $Counter, }, baseUrl: import.meta.url, } satisfies Manifest; diff --git a/tests/fixture_build_out_dir/fresh.gen.ts b/tests/fixture_build_out_dir/fresh.gen.ts index 7f26b684..4b4066a4 100644 --- a/tests/fixture_build_out_dir/fresh.gen.ts +++ b/tests/fixture_build_out_dir/fresh.gen.ts @@ -3,15 +3,15 @@ // This file is automatically updated during development when running `dev.ts`. import * as $index from "./routes/index.tsx"; -import * as $$Counter from "./islands/Counter.tsx"; -import { Manifest } from "$fresh/server.ts"; +import * as $Counter from "./islands/Counter.tsx"; +import { type Manifest } from "$fresh/server.ts"; const manifest = { routes: { "./routes/index.tsx": $index, }, islands: { - "./islands/Counter.tsx": $$Counter, + "./islands/Counter.tsx": $Counter, }, baseUrl: import.meta.url, } satisfies Manifest; diff --git a/tests/fixture_build_out_dir_sub/src/fresh.gen.ts b/tests/fixture_build_out_dir_sub/src/fresh.gen.ts index 7f26b684..4b4066a4 100644 --- a/tests/fixture_build_out_dir_sub/src/fresh.gen.ts +++ b/tests/fixture_build_out_dir_sub/src/fresh.gen.ts @@ -3,15 +3,15 @@ // This file is automatically updated during development when running `dev.ts`. import * as $index from "./routes/index.tsx"; -import * as $$Counter from "./islands/Counter.tsx"; -import { Manifest } from "$fresh/server.ts"; +import * as $Counter from "./islands/Counter.tsx"; +import { type Manifest } from "$fresh/server.ts"; const manifest = { routes: { "./routes/index.tsx": $index, }, islands: { - "./islands/Counter.tsx": $$Counter, + "./islands/Counter.tsx": $Counter, }, baseUrl: import.meta.url, } satisfies Manifest; diff --git a/tests/fixture_build_out_dir_sub2/src/fresh.gen.ts b/tests/fixture_build_out_dir_sub2/src/fresh.gen.ts index 7f26b684..4b4066a4 100644 --- a/tests/fixture_build_out_dir_sub2/src/fresh.gen.ts +++ b/tests/fixture_build_out_dir_sub2/src/fresh.gen.ts @@ -3,15 +3,15 @@ // This file is automatically updated during development when running `dev.ts`. import * as $index from "./routes/index.tsx"; -import * as $$Counter from "./islands/Counter.tsx"; -import { Manifest } from "$fresh/server.ts"; +import * as $Counter from "./islands/Counter.tsx"; +import { type Manifest } from "$fresh/server.ts"; const manifest = { routes: { "./routes/index.tsx": $index, }, islands: { - "./islands/Counter.tsx": $$Counter, + "./islands/Counter.tsx": $Counter, }, baseUrl: import.meta.url, } satisfies Manifest; diff --git a/tests/fixture_build_sub_dir/src/fresh.gen.ts b/tests/fixture_build_sub_dir/src/fresh.gen.ts index 7f26b684..4b4066a4 100644 --- a/tests/fixture_build_sub_dir/src/fresh.gen.ts +++ b/tests/fixture_build_sub_dir/src/fresh.gen.ts @@ -3,15 +3,15 @@ // This file is automatically updated during development when running `dev.ts`. import * as $index from "./routes/index.tsx"; -import * as $$Counter from "./islands/Counter.tsx"; -import { Manifest } from "$fresh/server.ts"; +import * as $Counter from "./islands/Counter.tsx"; +import { type Manifest } from "$fresh/server.ts"; const manifest = { routes: { "./routes/index.tsx": $index, }, islands: { - "./islands/Counter.tsx": $$Counter, + "./islands/Counter.tsx": $Counter, }, baseUrl: import.meta.url, } satisfies Manifest; diff --git a/tests/fixture_build_target/fresh.gen.ts b/tests/fixture_build_target/fresh.gen.ts index 7f26b684..4b4066a4 100644 --- a/tests/fixture_build_target/fresh.gen.ts +++ b/tests/fixture_build_target/fresh.gen.ts @@ -3,15 +3,15 @@ // This file is automatically updated during development when running `dev.ts`. import * as $index from "./routes/index.tsx"; -import * as $$Counter from "./islands/Counter.tsx"; -import { Manifest } from "$fresh/server.ts"; +import * as $Counter from "./islands/Counter.tsx"; +import { type Manifest } from "$fresh/server.ts"; const manifest = { routes: { "./routes/index.tsx": $index, }, islands: { - "./islands/Counter.tsx": $$Counter, + "./islands/Counter.tsx": $Counter, }, baseUrl: import.meta.url, } satisfies Manifest; diff --git a/tests/fixture_config/fresh.gen.ts b/tests/fixture_config/fresh.gen.ts index 6999d64a..b5bfec72 100644 --- a/tests/fixture_config/fresh.gen.ts +++ b/tests/fixture_config/fresh.gen.ts @@ -4,7 +4,7 @@ import * as $index from "./routes/index.tsx"; -import { Manifest } from "$fresh/server.ts"; +import { type Manifest } from "$fresh/server.ts"; const manifest = { routes: { diff --git a/tests/fixture_islands_multiple/deno.json b/tests/fixture_islands_multiple/deno.json new file mode 100644 index 00000000..4353235b --- /dev/null +++ b/tests/fixture_islands_multiple/deno.json @@ -0,0 +1,14 @@ +{ + "lock": false, + "imports": { + "$fresh/": "../../", + "preact": "https://esm.sh/preact@10.19.2", + "preact/": "https://esm.sh/preact@10.19.2/", + "@preact/signals": "https://esm.sh/*@preact/signals@1.2.2", + "@preact/signals-core": "https://esm.sh/@preact/signals-core@1.5.0" + }, + "compilerOptions": { + "jsx": "react-jsx", + "jsxImportSource": "preact" + } +} diff --git a/tests/fixture_islands_multiple/dev.ts b/tests/fixture_islands_multiple/dev.ts new file mode 100755 index 00000000..2d85d6c1 --- /dev/null +++ b/tests/fixture_islands_multiple/dev.ts @@ -0,0 +1,5 @@ +#!/usr/bin/env -S deno run -A --watch=static/,routes/ + +import dev from "$fresh/dev.ts"; + +await dev(import.meta.url, "./main.ts"); diff --git a/tests/fixture_islands_multiple/fresh.gen.ts b/tests/fixture_islands_multiple/fresh.gen.ts new file mode 100644 index 00000000..5be78994 --- /dev/null +++ b/tests/fixture_islands_multiple/fresh.gen.ts @@ -0,0 +1,23 @@ +// DO NOT EDIT. This file is generated by Fresh. +// This file SHOULD be checked into source version control. +// This file is automatically updated during development when running `dev.ts`. + +import * as $index from "./routes/index.tsx"; +import * as $Multiple from "./islands/Multiple.tsx"; +import * as $MultipleDefault from "./islands/MultipleDefault.tsx"; +import * as $Single from "./islands/Single.tsx"; +import { type Manifest } from "$fresh/server.ts"; + +const manifest = { + routes: { + "./routes/index.tsx": $index, + }, + islands: { + "./islands/Multiple.tsx": $Multiple, + "./islands/MultipleDefault.tsx": $MultipleDefault, + "./islands/Single.tsx": $Single, + }, + baseUrl: import.meta.url, +} satisfies Manifest; + +export default manifest; diff --git a/tests/fixture_islands_multiple/islands/Multiple.tsx b/tests/fixture_islands_multiple/islands/Multiple.tsx new file mode 100644 index 00000000..f5fa3a28 --- /dev/null +++ b/tests/fixture_islands_multiple/islands/Multiple.tsx @@ -0,0 +1,21 @@ +import { useSignal } from "@preact/signals"; + +export function Multiple1() { + const sig = useSignal(0); + return ( +
+

Multiple1 Island: {sig}

+ +
+ ); +} + +export function Multiple2() { + const sig = useSignal(0); + return ( +
+

Multiple2 Island: {sig}

+ +
+ ); +} diff --git a/tests/fixture_islands_multiple/islands/MultipleDefault.tsx b/tests/fixture_islands_multiple/islands/MultipleDefault.tsx new file mode 100644 index 00000000..e265bc0b --- /dev/null +++ b/tests/fixture_islands_multiple/islands/MultipleDefault.tsx @@ -0,0 +1,31 @@ +import { useSignal } from "@preact/signals"; + +export default function MultipleDefault() { + const sig = useSignal(0); + return ( +
+

MultipleDefault Island: {sig}

+ +
+ ); +} + +export function MultipleDefault1() { + const sig = useSignal(0); + return ( +
+

MultipleDefault1 Island: {sig}

+ +
+ ); +} + +export function MultipleDefault2() { + const sig = useSignal(0); + return ( +
+

MultipleDefault2 Island: {sig}

+ +
+ ); +} diff --git a/tests/fixture_islands_multiple/islands/Single.tsx b/tests/fixture_islands_multiple/islands/Single.tsx new file mode 100644 index 00000000..f1c73354 --- /dev/null +++ b/tests/fixture_islands_multiple/islands/Single.tsx @@ -0,0 +1,12 @@ +import { useSignal } from "@preact/signals"; + +export default function Single() { + const sig = useSignal(0); + + return ( +
+

Single Island: {sig}

+ +
+ ); +} diff --git a/tests/fixture_islands_multiple/main.ts b/tests/fixture_islands_multiple/main.ts new file mode 100644 index 00000000..dedce9cb --- /dev/null +++ b/tests/fixture_islands_multiple/main.ts @@ -0,0 +1,10 @@ +/// +/// +/// +/// +/// + +import { start } from "$fresh/server.ts"; +import manifest from "./fresh.gen.ts"; + +await start(manifest); diff --git a/tests/fixture_islands_multiple/routes/index.tsx b/tests/fixture_islands_multiple/routes/index.tsx new file mode 100644 index 00000000..2087f9d3 --- /dev/null +++ b/tests/fixture_islands_multiple/routes/index.tsx @@ -0,0 +1,22 @@ +import Single from "../islands/Single.tsx"; +import { Multiple1, Multiple2 } from "../islands/Multiple.tsx"; +import MultipleDefault, { + MultipleDefault1, + MultipleDefault2, +} from "../islands/MultipleDefault.tsx"; + +export default function Home() { + return ( +
+

Single

+ +

Multiple

+ + +

Multiple Default

+ + + +
+ ); +} diff --git a/tests/fixture_partials/fresh.gen.ts b/tests/fixture_partials/fresh.gen.ts index 70f15dec..86ef52ab 100644 --- a/tests/fixture_partials/fresh.gen.ts +++ b/tests/fixture_partials/fresh.gen.ts @@ -116,22 +116,22 @@ import * as $relative_link_index from "./routes/relative_link/index.tsx"; import * as $scroll_restoration_index from "./routes/scroll_restoration/index.tsx"; import * as $scroll_restoration_injected from "./routes/scroll_restoration/injected.tsx"; import * as $scroll_restoration_update from "./routes/scroll_restoration/update.tsx"; -import * as $$Counter from "./islands/Counter.tsx"; -import * as $$CounterA from "./islands/CounterA.tsx"; -import * as $$CounterB from "./islands/CounterB.tsx"; -import * as $$Fader from "./islands/Fader.tsx"; -import * as $$InvalidSlot from "./islands/InvalidSlot.tsx"; -import * as $$KeyExplorer from "./islands/KeyExplorer.tsx"; -import * as $$LazyLink from "./islands/LazyLink.tsx"; -import * as $$Logger from "./islands/Logger.tsx"; -import * as $$Other from "./islands/Other.tsx"; -import * as $$PartialTrigger from "./islands/PartialTrigger.tsx"; -import * as $$PassThrough from "./islands/PassThrough.tsx"; -import * as $$PropIsland from "./islands/PropIsland.tsx"; -import * as $$SignalProp from "./islands/SignalProp.tsx"; -import * as $$Spinner from "./islands/Spinner.tsx"; -import * as $$Stateful from "./islands/Stateful.tsx"; -import { Manifest } from "$fresh/server.ts"; +import * as $Counter from "./islands/Counter.tsx"; +import * as $CounterA from "./islands/CounterA.tsx"; +import * as $CounterB from "./islands/CounterB.tsx"; +import * as $Fader from "./islands/Fader.tsx"; +import * as $InvalidSlot from "./islands/InvalidSlot.tsx"; +import * as $KeyExplorer from "./islands/KeyExplorer.tsx"; +import * as $LazyLink from "./islands/LazyLink.tsx"; +import * as $Logger from "./islands/Logger.tsx"; +import * as $Other from "./islands/Other.tsx"; +import * as $PartialTrigger from "./islands/PartialTrigger.tsx"; +import * as $PassThrough from "./islands/PassThrough.tsx"; +import * as $PropIsland from "./islands/PropIsland.tsx"; +import * as $SignalProp from "./islands/SignalProp.tsx"; +import * as $Spinner from "./islands/Spinner.tsx"; +import * as $Stateful from "./islands/Stateful.tsx"; +import { type Manifest } from "$fresh/server.ts"; const manifest = { routes: { @@ -262,21 +262,21 @@ const manifest = { "./routes/scroll_restoration/update.tsx": $scroll_restoration_update, }, islands: { - "./islands/Counter.tsx": $$Counter, - "./islands/CounterA.tsx": $$CounterA, - "./islands/CounterB.tsx": $$CounterB, - "./islands/Fader.tsx": $$Fader, - "./islands/InvalidSlot.tsx": $$InvalidSlot, - "./islands/KeyExplorer.tsx": $$KeyExplorer, - "./islands/LazyLink.tsx": $$LazyLink, - "./islands/Logger.tsx": $$Logger, - "./islands/Other.tsx": $$Other, - "./islands/PartialTrigger.tsx": $$PartialTrigger, - "./islands/PassThrough.tsx": $$PassThrough, - "./islands/PropIsland.tsx": $$PropIsland, - "./islands/SignalProp.tsx": $$SignalProp, - "./islands/Spinner.tsx": $$Spinner, - "./islands/Stateful.tsx": $$Stateful, + "./islands/Counter.tsx": $Counter, + "./islands/CounterA.tsx": $CounterA, + "./islands/CounterB.tsx": $CounterB, + "./islands/Fader.tsx": $Fader, + "./islands/InvalidSlot.tsx": $InvalidSlot, + "./islands/KeyExplorer.tsx": $KeyExplorer, + "./islands/LazyLink.tsx": $LazyLink, + "./islands/Logger.tsx": $Logger, + "./islands/Other.tsx": $Other, + "./islands/PartialTrigger.tsx": $PartialTrigger, + "./islands/PassThrough.tsx": $PassThrough, + "./islands/PropIsland.tsx": $PropIsland, + "./islands/SignalProp.tsx": $SignalProp, + "./islands/Spinner.tsx": $Spinner, + "./islands/Stateful.tsx": $Stateful, }, baseUrl: import.meta.url, } satisfies Manifest; diff --git a/tests/fixture_plugin_lifecycle/fresh.gen.ts b/tests/fixture_plugin_lifecycle/fresh.gen.ts index 6999d64a..b5bfec72 100644 --- a/tests/fixture_plugin_lifecycle/fresh.gen.ts +++ b/tests/fixture_plugin_lifecycle/fresh.gen.ts @@ -4,7 +4,7 @@ import * as $index from "./routes/index.tsx"; -import { Manifest } from "$fresh/server.ts"; +import { type Manifest } from "$fresh/server.ts"; const manifest = { routes: { diff --git a/tests/fixture_tailwind_build/fresh.gen.ts b/tests/fixture_tailwind_build/fresh.gen.ts index eff1b80e..e6643ad2 100644 --- a/tests/fixture_tailwind_build/fresh.gen.ts +++ b/tests/fixture_tailwind_build/fresh.gen.ts @@ -5,7 +5,7 @@ import * as $_app from "./routes/_app.tsx"; import * as $index from "./routes/index.tsx"; -import { Manifest } from "$fresh/server.ts"; +import { type Manifest } from "$fresh/server.ts"; const manifest = { routes: { diff --git a/tests/init_test.ts b/tests/init_test.ts index 32c6a1e1..bd52fa59 100644 --- a/tests/init_test.ts +++ b/tests/init_test.ts @@ -38,6 +38,7 @@ Deno.test({ args: [ "run", "-A", + "--no-config", "init.ts", tmpDirName, ], @@ -155,6 +156,7 @@ Deno.test({ args: [ "run", "-A", + "--no-config", "init.ts", tmpDirName, "--tailwind", @@ -245,7 +247,7 @@ Deno.test("fresh-init error(help)", async function (t) { "execute invalid init command (deno run -A init.ts)", async () => { const cliProcess = new Deno.Command(Deno.execPath(), { - args: ["run", "-A", "init.ts"], + args: ["run", "-A", "--no-config", "init.ts"], stdin: "null", stderr: "piped", }); @@ -261,7 +263,7 @@ Deno.test("fresh-init error(help)", async function (t) { "execute invalid init command (deno run -A init.ts -f)", async () => { const cliProcess = new Deno.Command(Deno.execPath(), { - args: ["run", "-A", "init.ts", "-f"], + args: ["run", "-A", "--no-config", "init.ts", "-f"], }); const { code, stderr } = await cliProcess.output(); assertEquals(code, 1); @@ -275,7 +277,7 @@ Deno.test("fresh-init error(help)", async function (t) { "execute invalid init command (deno run -A init.ts --foo)", async () => { const cliProcess = new Deno.Command(Deno.execPath(), { - args: ["run", "-A", "init.ts", "--foo"], + args: ["run", "-A", "--no-config", "init.ts", "--foo"], }); const { code, stderr } = await cliProcess.output(); assertEquals(code, 1); @@ -295,6 +297,7 @@ Deno.test("fresh-init .", async function (t) { args: [ "run", "-A", + "--no-config", path.join(Deno.cwd(), "init.ts"), ".", ], @@ -322,6 +325,7 @@ Deno.test({ args: [ "run", "-A", + "--no-config", path.join(Deno.cwd(), "init.ts"), "subdirectory/subsubdirectory", ], @@ -379,6 +383,7 @@ Deno.test({ args: [ "run", "-A", + "--no-config", "init.ts", tmpDirName, "--tailwind", diff --git a/tests/islands_test.ts b/tests/islands_test.ts index e0cb1c43..332a21db 100644 --- a/tests/islands_test.ts +++ b/tests/islands_test.ts @@ -442,3 +442,39 @@ Deno.test("throws when passing non-jsx children to an island", async (t) => { }, ); }); + +Deno.test("serves multiple islands in one file", async () => { + await withPageName( + "./tests/fixture_islands_multiple/dev.ts", + async (page, address) => { + await page.goto(address, { waitUntil: "networkidle2" }); + + await page.click(".single button"); + await waitForText(page, ".single p", "Single Island: 1"); + + await page.click(".multiple1 button"); + await waitForText(page, ".multiple1 p", "Multiple1 Island: 1"); + await page.click(".multiple2 button"); + await waitForText(page, ".multiple2 p", "Multiple2 Island: 1"); + + await page.click(".multipledefault button"); + await waitForText( + page, + ".multipledefault p", + "MultipleDefault Island: 1", + ); + await page.click(".multipledefault1 button"); + await waitForText( + page, + ".multipledefault1 p", + "MultipleDefault1 Island: 1", + ); + await page.click(".multipledefault2 button"); + await waitForText( + page, + ".multipledefault2 p", + "MultipleDefault2 Island: 1", + ); + }, + ); +}); diff --git a/tests/main_test.ts b/tests/main_test.ts index fcc63c75..b8fddaed 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -33,7 +33,7 @@ Deno.test("/ page prerender", async () => { assertEquals(resp.headers.get("server"), "fresh test server"); const body = await resp.text(); assertStringIncludes(body, ``); - assertStringIncludes(body, "test_default.js"); + assertStringIncludes(body, "test.js"); assertStringIncludes(body, "

Hello!

"); assertStringIncludes(body, "

Viewing JIT render.

"); assertStringIncludes(body, `>{"v":[[{"message":"Hello!"}],[]]}`); diff --git a/tests/update_test.ts b/tests/update_test.ts index 15241418..15443cda 100644 --- a/tests/update_test.ts +++ b/tests/update_test.ts @@ -15,6 +15,7 @@ async function updateAndVerify(cwd: string | URL, expected: RegExp) { args: [ "run", "-A", + "--no-config", path.join(Deno.cwd(), "update.ts"), ".", ], @@ -40,6 +41,7 @@ async function initProject() { args: [ "run", "-A", + "--no-config", path.join(Deno.cwd(), "init.ts"), ".", ], diff --git a/www/fresh.gen.ts b/www/fresh.gen.ts index d2c34ad6..220a5839 100644 --- a/www/fresh.gen.ts +++ b/www/fresh.gen.ts @@ -13,13 +13,13 @@ import * as $index from "./routes/index.tsx"; import * as $raw from "./routes/raw.ts"; import * as $showcase from "./routes/showcase.tsx"; import * as $update from "./routes/update.tsx"; -import * as $$ComponentGallery from "./islands/ComponentGallery.tsx"; -import * as $$CopyArea from "./islands/CopyArea.tsx"; -import * as $$Counter from "./islands/Counter.tsx"; -import * as $$LemonDrop from "./islands/LemonDrop.tsx"; -import * as $$SearchButton from "./islands/SearchButton.tsx"; -import * as $$VersionSelect from "./islands/VersionSelect.tsx"; -import { Manifest } from "$fresh/server.ts"; +import * as $ComponentGallery from "./islands/ComponentGallery.tsx"; +import * as $CopyArea from "./islands/CopyArea.tsx"; +import * as $Counter from "./islands/Counter.tsx"; +import * as $LemonDrop from "./islands/LemonDrop.tsx"; +import * as $SearchButton from "./islands/SearchButton.tsx"; +import * as $VersionSelect from "./islands/VersionSelect.tsx"; +import { type Manifest } from "$fresh/server.ts"; const manifest = { routes: { @@ -36,12 +36,12 @@ const manifest = { "./routes/update.tsx": $update, }, islands: { - "./islands/ComponentGallery.tsx": $$ComponentGallery, - "./islands/CopyArea.tsx": $$CopyArea, - "./islands/Counter.tsx": $$Counter, - "./islands/LemonDrop.tsx": $$LemonDrop, - "./islands/SearchButton.tsx": $$SearchButton, - "./islands/VersionSelect.tsx": $$VersionSelect, + "./islands/ComponentGallery.tsx": $ComponentGallery, + "./islands/CopyArea.tsx": $CopyArea, + "./islands/Counter.tsx": $Counter, + "./islands/LemonDrop.tsx": $LemonDrop, + "./islands/SearchButton.tsx": $SearchButton, + "./islands/VersionSelect.tsx": $VersionSelect, }, baseUrl: import.meta.url, } satisfies Manifest;