Skip to content

Commit

Permalink
fix: dynamically load zod-to-ts (#12273)
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic authored Oct 21, 2024
1 parent f32a7a8 commit c2ee963
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-crabs-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue with some package managers where sites would not build if TypeScript was not installed.
14 changes: 11 additions & 3 deletions packages/astro/src/content/types-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { bold, cyan } from 'kleur/colors';
import { type ViteDevServer, normalizePath } from 'vite';
import { type ZodSchema, z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';
import { printNode, zodToTs } from 'zod-to-ts';
import type { AstroSettings, ContentEntryType } from '../@types/astro.js';
import { AstroError } from '../core/errors/errors.js';
import { AstroErrorData } from '../core/errors/index.js';
Expand Down Expand Up @@ -396,8 +395,17 @@ async function typeForCollection<T extends keyof ContentConfig['collections']>(
if (collection?.type === CONTENT_LAYER_TYPE) {
const schema = await getContentLayerSchema(collection, collectionKey);
if (schema) {
const ast = zodToTs(schema);
return printNode(ast.node);
try {
const zodToTs = await import('zod-to-ts');
const ast = zodToTs.zodToTs(schema);
return zodToTs.printNode(ast.node);
} catch (err: any) {
// zod-to-ts is sad if we don't have TypeScript installed, but that's fine as we won't be needing types in that case
if (err.message.includes("Cannot find package 'typescript'")) {
return 'any';
}
throw err;
}
}
}
return 'any';
Expand Down

0 comments on commit c2ee963

Please sign in to comment.