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
8 changes: 8 additions & 0 deletions packages/void/src/router/detect-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export function detectHttpExports(absPath: string): Array<string> {
for (const node of program.body ?? []) {
if (node.type === 'ExportNamedDeclaration') {
const declarationNode = node as AstExportNamedDeclaration;
// `export type { GET }` exports no runtime value — skip the whole statement.
if (declarationNode.exportKind === 'type') {
continue;
}
// export const GET = ...; export function POST() { ... }
const decl = declarationNode.declaration;
if (decl) {
Expand All @@ -50,6 +54,10 @@ export function detectHttpExports(absPath: string): Array<string> {
// export { GET, POST } (specifiers)
if (declarationNode.specifiers) {
for (const spec of declarationNode.specifiers) {
// `export { type GET }` re-exports a type, not a runtime handler.
if (spec.exportKind === 'type') {
continue;
}
const exported = isIdentifier(spec.exported)
? spec.exported.name
: isIdentifier(spec.local)
Expand Down
4 changes: 4 additions & 0 deletions packages/void/src/shared/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ export type AstFunctionDeclaration = AstNode & {
export type AstExportSpecifier = AstNode & {
exported?: AstNode;
local?: AstNode;
/** "type" for `export { type X }`, "value" (or undefined) otherwise. */
exportKind?: string;
};

export type AstExportNamedDeclaration = AstNode & {
type: 'ExportNamedDeclaration';
declaration?: AstNode;
specifiers?: Array<AstExportSpecifier>;
/** "type" for `export type { X }`, "value" (or undefined) otherwise. */
exportKind?: string;
};

export type AstExportDefaultDeclaration = AstNode & {
Expand Down
7 changes: 7 additions & 0 deletions packages/void/src/ssr/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ function validateServerRenderExport(entry: ResolvedEntry): void {
if (node.type !== 'ExportNamedDeclaration') {
return false;
}
// `export type { render }` exports no runtime value.
if ((node as AstExportNamedDeclaration).exportKind === 'type') {
return false;
}

const declaration = (node as AstExportNamedDeclaration).declaration;
if (declaration?.type === 'FunctionDeclaration') {
Expand All @@ -126,6 +130,9 @@ function validateServerRenderExport(entry: ResolvedEntry): void {

const specifiers = (node as AstExportNamedDeclaration).specifiers ?? [];
return specifiers.some((specifier: AstExportSpecifier) => {
if (specifier.exportKind === 'type') {
return false;
}
const exported = specifier.exported;
return isIdentifier(exported) && exported.name === 'render';
});
Expand Down
20 changes: 20 additions & 0 deletions packages/void/test/unit/detect-exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ export function transform() {}`,
expect(detectHttpExports(path)).toEqual(['GET']);
});

it('ignores a type-only export statement', () => {
// `export type { GET }` erases at runtime, so it must not register a GET route.
const path = writeRoute(
'hello.ts',
`type GET = () => Response;
export type { GET };`,
);
expect(detectHttpExports(path)).toEqual([]);
});

it('ignores an inline type-only specifier but keeps value specifiers', () => {
const path = writeRoute(
'hello.ts',
`const POST = () => "ok";
type GET = () => Response;
export { POST, type GET };`,
);
expect(detectHttpExports(path)).toEqual(['POST']);
});

it('returns empty array for no HTTP exports', () => {
const path = writeRoute('hello.ts', 'export default () => "ok";');
expect(detectHttpExports(path)).toEqual([]);
Expand Down
14 changes: 14 additions & 0 deletions packages/void/test/unit/ssr-entry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ describe('ssr entry helpers', () => {
);
});

it('does not treat a type-only render export as a valid entry', () => {
const root = createTempRoot();
writeFileSync(join(root, 'src', 'main.client.ts'), "console.log('client entry');");
writeFileSync(
join(root, 'src', 'main.ssr.ts'),
`type render = () => Response;
export type { render };`,
);

expect(() => validateSsrEntry(root)).toThrow(
'Export `render(c, assetTags)` or `export default defineRender(...)`',
);
});

it('throws when server entry exists without client entry', () => {
const root = createTempRoot();
writeFileSync(
Expand Down