From 5d7db1dbb0ff06db98e08b0ca241ff09d0b8b44d Mon Sep 17 00:00:00 2001 From: Lou Cyx Date: Wed, 24 Jan 2024 06:40:02 -0800 Subject: [PATCH] Fix for #9673 (#9680) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix for #9673 * 🦋 add changeset file * Update breezy-plants-smoke.md Co-authored-by: Florian Lefebvre * ⚡️ simplified normalizeConfigPath --------- Co-authored-by: Florian Lefebvre --- .changeset/breezy-plants-smoke.md | 5 +++ packages/astro/src/content/types-generator.ts | 31 ++++++++++++------- 2 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 .changeset/breezy-plants-smoke.md diff --git a/.changeset/breezy-plants-smoke.md b/.changeset/breezy-plants-smoke.md new file mode 100644 index 000000000000..034ab448a56f --- /dev/null +++ b/.changeset/breezy-plants-smoke.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Fixes types generation from Content Collections config file diff --git a/packages/astro/src/content/types-generator.ts b/packages/astro/src/content/types-generator.ts index 4d466f594223..d08f15109230 100644 --- a/packages/astro/src/content/types-generator.ts +++ b/packages/astro/src/content/types-generator.ts @@ -333,6 +333,22 @@ function invalidateVirtualMod(viteServer: ViteDevServer) { viteServer.moduleGraph.invalidateModule(virtualMod); } +/** + * Takes the source (`from`) and destination (`to`) of a config path and + * returns a normalized relative version: + * - If is not relative, it adds `./` to the beginning. + * - If it ends with `.ts`, it replaces it with `.js`. + * - It adds `""` around the string. + * @param from Config path source. + * @param to Config path destination. + * @returns Normalized config path. + */ +function normalizeConfigPath(from: string, to: string) { + const configPath = path.relative(from, to).replace(/\.ts$/, '.js'); + + return `"${isRelativePath(configPath) ? '' : './'}${configPath}"` as const; +} + async function writeContentFiles({ fs, contentPaths, @@ -415,18 +431,11 @@ async function writeContentFiles({ fs.mkdirSync(contentPaths.cacheDir, { recursive: true }); } - let configPathRelativeToCacheDir = normalizePath( - path.relative(contentPaths.cacheDir.pathname, contentPaths.config.url.pathname) + const configPathRelativeToCacheDir = normalizeConfigPath( + contentPaths.cacheDir.pathname, + contentPaths.config.url.pathname ); - if (!isRelativePath(configPathRelativeToCacheDir)) - configPathRelativeToCacheDir = './' + configPathRelativeToCacheDir; - - // Remove `.ts` from import path - if (configPathRelativeToCacheDir.endsWith('.ts')) { - configPathRelativeToCacheDir = configPathRelativeToCacheDir.replace(/\.ts$/, ''); - } - for (const contentEntryType of contentEntryTypes) { if (contentEntryType.contentModuleTypes) { typeTemplateContent = contentEntryType.contentModuleTypes + '\n' + typeTemplateContent; @@ -436,7 +445,7 @@ async function writeContentFiles({ typeTemplateContent = typeTemplateContent.replace('// @@DATA_ENTRY_MAP@@', dataTypesStr); typeTemplateContent = typeTemplateContent.replace( "'@@CONTENT_CONFIG_TYPE@@'", - contentConfig ? `typeof import(${JSON.stringify(configPathRelativeToCacheDir)})` : 'never' + contentConfig ? `typeof import(${configPathRelativeToCacheDir})` : 'never' ); await fs.promises.writeFile(