Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/react/src/plugin-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ type PagesPluginOptions = {
prefetch?: PrefetchConfig;
};

/** Check if the pages directory has any non-island page files (.tsx/.jsx without .island.). */
/** Check if the pages directory has any page components that need the full client runtime. */
function hasNonIslandPages(dir: string): boolean {
try {
for (const entry of readdirSync(dir, { withFileTypes: true, recursive: true })) {
if (!entry.isFile()) {
continue;
}
const name = entry.name;
if (name.startsWith('_') || name.startsWith('layout.')) {
if (name.startsWith('_') || name.startsWith('layout.') || name.includes('.server.')) {
continue;
}
if (/\.(tsx|jsx)$/.test(name) && !name.includes('.island.')) {
Expand Down
60 changes: 60 additions & 0 deletions packages/react/test/unit/plugin-pages.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { afterEach, describe, expect, it, vi } from 'vite-plus/test';
import { islandsPlugin } from '../../src/plugin-islands.ts';
import { pagesPlugin } from '../../src/plugin-pages.ts';

const TMP = join(import.meta.dirname, '__tmp_pages_plugin__');
Expand Down Expand Up @@ -63,6 +64,65 @@ function setupWatcher(plugin: PagesPluginHooks, logError = vi.fn(), invalidateMo
return handlers;
}

describe('pagesPlugin build config', () => {
afterEach(() => {
rmSync(TMP, { recursive: true, force: true });
});

it.each(['tsx', 'jsx'])(
'omits pages-client for island-only pages with .server.%s files',
async (extension) => {
writePage(
'index.island.tsx',
`import Counter from '../Counter' with { island: 'load' };
export default function Home() { return <Counter />; }`,
);
writePage(`index.server.${extension}`, 'export const loader = () => ({ count: 0 });');
writeFileSync(
join(TMP, 'Counter.tsx'),
'export default function Counter() { return <button>0</button>; }',
);

const plugin = pagesPlugin(PAGES_DIR) as PagesPluginHooks;
expect(getHookHandler(plugin.config)({}, { command: 'build' })).toBeUndefined();

const islandPlugin = islandsPlugin(PAGES_DIR)[0] as PagesPluginHooks;
const islandConfig = await getHookHandler(islandPlugin.config)({}, { command: 'build' });
expect(islandConfig).toMatchObject({
environments: {
client: {
build: {
rollupOptions: { input: { 'islands-client': 'virtual:void-islands-client' } },
},
},
},
});
await getHookHandler(islandPlugin.buildStart).call({});
const client = await getHookHandler(islandPlugin.load)('\0virtual:void-islands-client');
expect(client).toContain('"Counter"');
expect(client).toContain('initIslands(islands,');
},
);

it.each(['about.tsx', 'about.jsx', 'blog/about.tsx', 'blog/about.jsx'])(
'keeps pages-client for an ordinary %s page alongside island pages',
(page) => {
writePage('index.island.tsx', 'export default function Home() { return <h1>Home</h1>; }');
writePage('index.server.tsx', 'export const loader = () => ({});');
writePage('index.server.jsx', 'export const loader = () => ({});');
writePage(page, 'export default function About() { return <h1>About</h1>; }');

const plugin = pagesPlugin(PAGES_DIR) as PagesPluginHooks;
expect(getHookHandler(plugin.config)({}, { command: 'build' })).toMatchObject({
build: {
manifest: true,
rollupOptions: { input: { 'pages-client': 'virtual:void-pages-client' } },
},
});
},
);
});

describe('pagesPlugin dev config', () => {
afterEach(() => {
rmSync(TMP, { recursive: true, force: true });
Expand Down
4 changes: 2 additions & 2 deletions packages/solid/src/plugin-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ type PagesPluginOptions = {
prefetch?: PrefetchConfig;
};

/** Check if the pages directory has any non-island page files (.tsx/.jsx without .island.). */
/** Check if the pages directory has any page components that need the full client runtime. */
function hasNonIslandPages(dir: string): boolean {
try {
for (const entry of readdirSync(dir, { withFileTypes: true, recursive: true })) {
if (!entry.isFile()) {
continue;
}
const name = entry.name;
if (name.startsWith('_') || name.startsWith('layout.')) {
if (name.startsWith('_') || name.startsWith('layout.') || name.includes('.server.')) {
continue;
}
if (/\.(tsx|jsx)$/.test(name) && !name.includes('.island.')) {
Expand Down
60 changes: 60 additions & 0 deletions packages/solid/test/unit/plugin-pages.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { afterEach, describe, expect, it, vi } from 'vite-plus/test';
import { islandsPlugin } from '../../src/plugin-islands.ts';
import { pagesPlugin } from '../../src/plugin-pages.ts';

const TMP = join(import.meta.dirname, '__tmp_pages_plugin__');
Expand Down Expand Up @@ -54,6 +55,65 @@ function setupWatcher(plugin: PagesPluginHooks, logError = vi.fn(), invalidateMo
return handlers;
}

describe('pagesPlugin build config', () => {
afterEach(() => {
rmSync(TMP, { recursive: true, force: true });
});

it.each(['tsx', 'jsx'])(
'omits pages-client for island-only pages with .server.%s files',
async (extension) => {
writePage(
'index.island.tsx',
`import Counter from '../Counter' with { island: 'load' };
export default function Home() { return <Counter />; }`,
);
writePage(`index.server.${extension}`, 'export const loader = () => ({ count: 0 });');
writeFileSync(
join(TMP, 'Counter.tsx'),
'export default function Counter() { return <button>0</button>; }',
);

const plugin = pagesPlugin(PAGES_DIR) as PagesPluginHooks;
expect(getHookHandler(plugin.config)({}, { command: 'build' })).toBeUndefined();

const islandPlugin = islandsPlugin(PAGES_DIR)[0] as PagesPluginHooks;
const islandConfig = await getHookHandler(islandPlugin.config)({}, { command: 'build' });
expect(islandConfig).toMatchObject({
environments: {
client: {
build: {
rollupOptions: { input: { 'islands-client': 'virtual:void-islands-client' } },
},
},
},
});
await getHookHandler(islandPlugin.buildStart).call({});
const client = await getHookHandler(islandPlugin.load)('\0virtual:void-islands-client');
expect(client).toContain('"Counter"');
expect(client).toContain('initIslands(islands,');
},
);

it.each(['about.tsx', 'about.jsx', 'blog/about.tsx', 'blog/about.jsx'])(
'keeps pages-client for an ordinary %s page alongside island pages',
(page) => {
writePage('index.island.tsx', 'export default function Home() { return <h1>Home</h1>; }');
writePage('index.server.tsx', 'export const loader = () => ({});');
writePage('index.server.jsx', 'export const loader = () => ({});');
writePage(page, 'export default function About() { return <h1>About</h1>; }');

const plugin = pagesPlugin(PAGES_DIR) as PagesPluginHooks;
expect(getHookHandler(plugin.config)({}, { command: 'build' })).toMatchObject({
build: {
manifest: true,
rollupOptions: { input: { 'pages-client': 'virtual:void-pages-client' } },
},
});
},
);
});

describe('pagesPlugin dev HMR', () => {
afterEach(() => {
rmSync(TMP, { recursive: true, force: true });
Expand Down