Skip to content
Merged
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
10 changes: 8 additions & 2 deletions packages/react/src/plugin-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,21 @@ 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.')) {
// Server companions such as `index.server.tsx` are not pages.
if (
name.startsWith('_') ||
name.startsWith('layout.') ||
name.endsWith('.server.tsx') ||
name.endsWith('.server.jsx')
) {
continue;
}
if (/\.(tsx|jsx)$/.test(name) && !name.includes('.island.')) {
Expand Down
35 changes: 35 additions & 0 deletions packages/react/test/unit/plugin-pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,41 @@ 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',
(extension) => {
writePage('index.island.tsx', 'export default function Home() { return <h1>Home</h1>; }');
writePage(`index.server.${extension}`, 'export const loader = () => ({ count: 0 });');

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

it.each(['about.tsx', 'about.jsx', 'blog/about.tsx', 'blog/about.jsx', 'status.server.v2.tsx'])(
'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
10 changes: 8 additions & 2 deletions packages/solid/src/plugin-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,21 @@ 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.')) {
// Server companions such as `index.server.tsx` are not pages.
if (
name.startsWith('_') ||
name.startsWith('layout.') ||
name.endsWith('.server.tsx') ||
name.endsWith('.server.jsx')
) {
continue;
}
if (/\.(tsx|jsx)$/.test(name) && !name.includes('.island.')) {
Expand Down
35 changes: 35 additions & 0 deletions packages/solid/test/unit/plugin-pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,41 @@ 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',
(extension) => {
writePage('index.island.tsx', 'export default function Home() { return <h1>Home</h1>; }');
writePage(`index.server.${extension}`, 'export const loader = () => ({ count: 0 });');

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

it.each(['about.tsx', 'about.jsx', 'blog/about.tsx', 'blog/about.jsx', 'status.server.v2.tsx'])(
'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
Loading