Repository navigation
fix(adapters): scan layouts for islands #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6fb932d
test: add failing island tests for layouts containing islands
TheAlexLichter e8d06e6
fix: scan layouts for islands
TheAlexLichter 1afff14
Merge branch 'main' into fix/layout-islands
Brooooooklyn ecf9857
fix: include named layouts in island pre-scan
Brooooooklyn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| import { describe, expect, it, beforeEach, afterEach } from "vitest"; | ||
| import { mkdirSync, writeFileSync, rmSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
| import { islandsPlugin } from "../../src/plugin-islands"; | ||
|
|
||
| const TMP = join(import.meta.dirname, "__tmp_islands_plugin__"); | ||
| const PAGES_DIR = join(TMP, "pages"); | ||
|
|
||
| function writeFile(relativePath: string, content: string): void { | ||
| const full = join(PAGES_DIR, relativePath); | ||
| mkdirSync(join(full, ".."), { recursive: true }); | ||
| writeFileSync(full, content); | ||
| } | ||
|
|
||
| function setupPlugin() { | ||
| const plugins = islandsPlugin(PAGES_DIR); | ||
| const plugin = plugins[0]; | ||
|
|
||
| const config = { root: TMP, command: "build" as const, plugins: [] }; | ||
| (plugin as any).configResolved(config); | ||
|
|
||
| return { | ||
| async buildStart() { | ||
| await (plugin as any).buildStart.call({}); | ||
| }, | ||
| loadClientEntry(): string | undefined { | ||
| return (plugin as any).load.handler("\0virtual:void-islands-client"); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| describe("islandsPlugin — layout island import scanning", () => { | ||
| beforeEach(() => { | ||
| mkdirSync(PAGES_DIR, { recursive: true }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| rmSync(TMP, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it("pre-scans island imports from island layouts into the manifest", async () => { | ||
| writeFile( | ||
| "blog/layout.island.tsx", | ||
| `import Counter from "../_components/Counter" with { island: "load" } | ||
| export default function Layout({ children }) { return <div><Counter />{children}</div>; }`, | ||
| ); | ||
| writeFile( | ||
| "blog/index.island.tsx", | ||
| `export default function Blog() { return <div>Blog</div>; }`, | ||
| ); | ||
| writeFile( | ||
| "_components/Counter.tsx", | ||
| `export default function Counter() { return <button>0</button>; }`, | ||
| ); | ||
|
|
||
| const { buildStart, loadClientEntry } = setupPlugin(); | ||
| await buildStart(); | ||
| const clientEntry = loadClientEntry(); | ||
|
|
||
| expect(clientEntry).toBeDefined(); | ||
| expect(clientEntry).toContain("pages/_components/Counter"); | ||
| }); | ||
|
|
||
| it("pre-scans island imports from island pages into the manifest", async () => { | ||
| writeFile( | ||
| "index.island.tsx", | ||
| `import Toggle from "./Toggle" with { island: "idle" } | ||
| export default function Home() { return <Toggle />; }`, | ||
| ); | ||
| writeFile("Toggle.tsx", `export default function Toggle() { return <button>off</button>; }`); | ||
|
|
||
| const { buildStart, loadClientEntry } = setupPlugin(); | ||
| await buildStart(); | ||
| const clientEntry = loadClientEntry(); | ||
|
|
||
| expect(clientEntry).toBeDefined(); | ||
| expect(clientEntry).toContain("pages/Toggle"); | ||
| }); | ||
|
|
||
| it("includes island imports from both pages and layouts", async () => { | ||
| writeFile( | ||
| "app/layout.island.tsx", | ||
| `import Nav from "../_components/Nav" with { island: "load" } | ||
| export default function Layout({ children }) { return <div><Nav />{children}</div>; }`, | ||
| ); | ||
| writeFile( | ||
| "app/index.island.tsx", | ||
| `import Widget from "../_components/Widget" with { island: "idle" } | ||
| export default function Index() { return <Widget />; }`, | ||
| ); | ||
| writeFile("_components/Nav.tsx", `export default function Nav() { return <nav />; }`); | ||
| writeFile("_components/Widget.tsx", `export default function Widget() { return <div />; }`); | ||
|
|
||
| const { buildStart, loadClientEntry } = setupPlugin(); | ||
| await buildStart(); | ||
| const clientEntry = loadClientEntry(); | ||
|
|
||
| expect(clientEntry).toBeDefined(); | ||
| expect(clientEntry).toContain("pages/_components/Nav"); | ||
| expect(clientEntry).toContain("pages/_components/Widget"); | ||
| }); | ||
|
|
||
| it("pre-scans island imports from named island layouts into the manifest", async () => { | ||
| writeFile( | ||
| "_layouts/sidebar.island.tsx", | ||
| `import Clock from "../_components/Clock" with { island: "load" } | ||
| export default function Sidebar({ children }) { return <div><Clock />{children}</div>; }`, | ||
| ); | ||
| writeFile( | ||
| "_components/Clock.tsx", | ||
| `export default function Clock() { return <span>12:00</span>; }`, | ||
| ); | ||
| writeFile( | ||
| "index.island.tsx", | ||
| `export const layout = "sidebar" | ||
| export default function Home() { return <div>Home</div>; }`, | ||
| ); | ||
|
|
||
| const { buildStart, loadClientEntry } = setupPlugin(); | ||
| await buildStart(); | ||
| const clientEntry = loadClientEntry(); | ||
|
|
||
| expect(clientEntry).toBeDefined(); | ||
| expect(clientEntry).toContain("pages/_components/Clock"); | ||
| }); | ||
|
|
||
| it("skips non-island layouts during pre-scan", async () => { | ||
| writeFile( | ||
| "layout.tsx", | ||
| `import Sidebar from "./Sidebar" with { island: "load" } | ||
| export default function Layout({ children }) { return <div>{children}</div>; }`, | ||
| ); | ||
| writeFile("index.island.tsx", `export default function Home() { return <div>Home</div>; }`); | ||
| writeFile("Sidebar.tsx", `export default function Sidebar() { return <aside />; }`); | ||
|
|
||
| const { buildStart, loadClientEntry } = setupPlugin(); | ||
| await buildStart(); | ||
| const clientEntry = loadClientEntry(); | ||
|
|
||
| expect(clientEntry).not.toContain("Sidebar"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| import { describe, expect, it, beforeEach, afterEach } from "vitest"; | ||
| import { mkdirSync, writeFileSync, rmSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
| import { islandsPlugin } from "../../src/plugin-islands"; | ||
|
|
||
| const TMP = join(import.meta.dirname, "__tmp_islands_plugin__"); | ||
| const PAGES_DIR = join(TMP, "pages"); | ||
|
|
||
| function writeFile(relativePath: string, content: string): void { | ||
| const full = join(PAGES_DIR, relativePath); | ||
| mkdirSync(join(full, ".."), { recursive: true }); | ||
| writeFileSync(full, content); | ||
| } | ||
|
|
||
| function setupPlugin() { | ||
| const plugins = islandsPlugin(PAGES_DIR); | ||
| const plugin = plugins[0]; | ||
|
|
||
| const config = { root: TMP, command: "build" as const, plugins: [] }; | ||
| (plugin as any).configResolved(config); | ||
|
|
||
| return { | ||
| async buildStart() { | ||
| await (plugin as any).buildStart.call({}); | ||
| }, | ||
| loadClientEntry(): string | undefined { | ||
| return (plugin as any).load.handler("\0virtual:void-islands-client"); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| describe("islandsPlugin — layout island import scanning", () => { | ||
| beforeEach(() => { | ||
| mkdirSync(PAGES_DIR, { recursive: true }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| rmSync(TMP, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it("pre-scans island imports from island layouts into the manifest", async () => { | ||
| writeFile( | ||
| "blog/layout.island.tsx", | ||
| `import Counter from "../_components/Counter" with { island: "load" } | ||
| export default function Layout(props) { return <div><Counter />{props.children}</div>; }`, | ||
| ); | ||
| writeFile( | ||
| "blog/index.island.tsx", | ||
| `export default function Blog() { return <div>Blog</div>; }`, | ||
| ); | ||
| writeFile( | ||
| "_components/Counter.tsx", | ||
| `export default function Counter() { return <button>0</button>; }`, | ||
| ); | ||
|
|
||
| const { buildStart, loadClientEntry } = setupPlugin(); | ||
| await buildStart(); | ||
| const clientEntry = loadClientEntry(); | ||
|
|
||
| expect(clientEntry).toBeDefined(); | ||
| expect(clientEntry).toContain("pages/_components/Counter"); | ||
| }); | ||
|
|
||
| it("pre-scans island imports from island pages into the manifest", async () => { | ||
| writeFile( | ||
| "index.island.tsx", | ||
| `import Toggle from "./Toggle" with { island: "idle" } | ||
| export default function Home() { return <Toggle />; }`, | ||
| ); | ||
| writeFile("Toggle.tsx", `export default function Toggle() { return <button>off</button>; }`); | ||
|
|
||
| const { buildStart, loadClientEntry } = setupPlugin(); | ||
| await buildStart(); | ||
| const clientEntry = loadClientEntry(); | ||
|
|
||
| expect(clientEntry).toBeDefined(); | ||
| expect(clientEntry).toContain("pages/Toggle"); | ||
| }); | ||
|
|
||
| it("includes island imports from both pages and layouts", async () => { | ||
| writeFile( | ||
| "app/layout.island.tsx", | ||
| `import Nav from "../_components/Nav" with { island: "load" } | ||
| export default function Layout(props) { return <div><Nav />{props.children}</div>; }`, | ||
| ); | ||
| writeFile( | ||
| "app/index.island.tsx", | ||
| `import Widget from "../_components/Widget" with { island: "idle" } | ||
| export default function Index() { return <Widget />; }`, | ||
| ); | ||
| writeFile("_components/Nav.tsx", `export default function Nav() { return <nav />; }`); | ||
| writeFile("_components/Widget.tsx", `export default function Widget() { return <div />; }`); | ||
|
|
||
| const { buildStart, loadClientEntry } = setupPlugin(); | ||
| await buildStart(); | ||
| const clientEntry = loadClientEntry(); | ||
|
|
||
| expect(clientEntry).toBeDefined(); | ||
| expect(clientEntry).toContain("pages/_components/Nav"); | ||
| expect(clientEntry).toContain("pages/_components/Widget"); | ||
| }); | ||
|
|
||
| it("pre-scans island imports from named island layouts into the manifest", async () => { | ||
| writeFile( | ||
| "_layouts/sidebar.island.tsx", | ||
| `import Clock from "../_components/Clock" with { island: "load" } | ||
| export default function Sidebar(props) { return <div><Clock />{props.children}</div>; }`, | ||
| ); | ||
| writeFile( | ||
| "_components/Clock.tsx", | ||
| `export default function Clock() { return <span>12:00</span>; }`, | ||
| ); | ||
| writeFile( | ||
| "index.island.tsx", | ||
| `export const layout = "sidebar" | ||
| export default function Home() { return <div>Home</div>; }`, | ||
| ); | ||
|
|
||
| const { buildStart, loadClientEntry } = setupPlugin(); | ||
| await buildStart(); | ||
| const clientEntry = loadClientEntry(); | ||
|
|
||
| expect(clientEntry).toBeDefined(); | ||
| expect(clientEntry).toContain("pages/_components/Clock"); | ||
| }); | ||
|
|
||
| it("skips non-island layouts during pre-scan", async () => { | ||
| writeFile( | ||
| "layout.tsx", | ||
| `import Sidebar from "./Sidebar" with { island: "load" } | ||
| export default function Layout(props) { return <div>{props.children}</div>; }`, | ||
| ); | ||
| writeFile("index.island.tsx", `export default function Home() { return <div>Home</div>; }`); | ||
| writeFile("Sidebar.tsx", `export default function Sidebar() { return <aside />; }`); | ||
|
|
||
| const { buildStart, loadClientEntry } = setupPlugin(); | ||
| await buildStart(); | ||
| const clientEntry = loadClientEntry(); | ||
|
|
||
| expect(clientEntry).not.toContain("Sidebar"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.