From 9036f638fb5b1490230982f34d98eeb2ab1b2a56 Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Mon, 21 Oct 2024 14:20:08 +0100 Subject: [PATCH] fix: dynamically load zod-to-ts --- .changeset/swift-crabs-exercise.md | 5 +++++ packages/astro/src/content/types-generator.ts | 14 +++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 .changeset/swift-crabs-exercise.md 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';