diff --git a/packages/react/src/plugin-islands.ts b/packages/react/src/plugin-islands.ts index b91c731b58..8c20a6dc44 100644 --- a/packages/react/src/plugin-islands.ts +++ b/packages/react/src/plugin-islands.ts @@ -64,15 +64,25 @@ export function islandsPlugin(pagesDir: string): Plugin[] { // Pre-scan island pages to populate the manifest eagerly. // This is needed because the client build may load the virtual // islands-client entry before island page transforms run. - for (const page of pageScan.pages) { - if (!page.island) continue; - const filePath = resolve(pagesDir, page.componentPath); + const importAttrRe = + /import\s+\w+\s+from\s+("[^"]+"|'[^']+')\s+with\s*\{\s*island\s*:\s*("[^"]+"|'[^']+')\s*\}/g; + // Scan both pages AND layouts for island imports + const filesToScan: { filePath: string; island: boolean }[] = [ + ...pageScan.pages.map((p: any) => ({ filePath: p.componentPath, island: p.island })), + ...pageScan.layouts.map((l: any) => ({ filePath: l.filePath, island: l.island })), + ...pageScan.namedLayouts.map((nl: any) => ({ + filePath: nl.filePath, + island: nl.island, + })), + ]; + for (const entry of filesToScan) { + if (!entry.island) continue; + const filePath = resolve(pagesDir, entry.filePath); const code = readFileSync(filePath, "utf-8"); const importerDir = dirname(filePath); - const re = - /import\s+\w+\s+from\s+("[^"]+"|'[^']+')\s+with\s*\{\s*island\s*:\s*("[^"]+"|'[^']+')\s*\}/g; + importAttrRe.lastIndex = 0; let m; - while ((m = re.exec(code)) !== null) { + while ((m = importAttrRe.exec(code)) !== null) { const spec = m[1].slice(1, -1); const strategy = m[2].slice(1, -1); const absPath = resolveSpecifier(spec, importerDir, COMPONENT_EXTENSIONS); diff --git a/packages/react/test/unit/plugin-islands.test.ts b/packages/react/test/unit/plugin-islands.test.ts new file mode 100644 index 0000000000..be3f87baa3 --- /dev/null +++ b/packages/react/test/unit/plugin-islands.test.ts @@ -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
{children}
; }`, + ); + writeFile( + "blog/index.island.tsx", + `export default function Blog() { return
Blog
; }`, + ); + writeFile( + "_components/Counter.tsx", + `export default function Counter() { return ; }`, + ); + + 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 ; }`, + ); + writeFile("Toggle.tsx", `export default function Toggle() { return ; }`); + + 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
; }`, + ); + writeFile( + "app/index.island.tsx", + `import Widget from "../_components/Widget" with { island: "idle" } +export default function Index() { return ; }`, + ); + writeFile("_components/Nav.tsx", `export default function Nav() { return