diff --git a/.changeset/swift-crabs-exercise.md b/.changeset/swift-crabs-exercise.md new file mode 100644 index 000000000000..4da2c886595a --- /dev/null +++ b/.changeset/swift-crabs-exercise.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes an issue with some package managers where sites would not build if TypeScript was not installed. diff --git a/packages/astro/src/content/types-generator.ts b/packages/astro/src/content/types-generator.ts index e031293da047..70c0fd9b79fc 100644 --- a/packages/astro/src/content/types-generator.ts +++ b/packages/astro/src/content/types-generator.ts @@ -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'; @@ -396,8 +395,17 @@ async function typeForCollection( 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';