Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dynamically load zod-to-ts #12273

Merged
merged 1 commit into from
Oct 21, 2024
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
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
Loading