From 97f413f1189fd626dffac8b48b166684c7e77627 Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Fri, 29 Nov 2024 16:02:12 +0000 Subject: [PATCH] fix: generate types even without src/content (#12565) * fix: generate types even without src/content * chore: add test --- .changeset/blue-gorillas-accept.md | 5 +++++ packages/astro/src/core/sync/index.ts | 23 +++++++++++++++++------ packages/astro/test/content-layer.test.js | 9 ++++++++- 3 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 .changeset/blue-gorillas-accept.md diff --git a/.changeset/blue-gorillas-accept.md b/.changeset/blue-gorillas-accept.md new file mode 100644 index 0000000000..b122a95583 --- /dev/null +++ b/.changeset/blue-gorillas-accept.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes a bug where content types were not generated when first running astro dev unless src/content exists diff --git a/packages/astro/src/core/sync/index.ts b/packages/astro/src/core/sync/index.ts index 67fdccc543..823470e970 100644 --- a/packages/astro/src/core/sync/index.ts +++ b/packages/astro/src/core/sync/index.ts @@ -86,7 +86,11 @@ export async function clearContentLayerCache({ settings, logger, fs = fsMod, -}: { settings: AstroSettings; logger: Logger; fs?: typeof fsMod }) { +}: { + settings: AstroSettings; + logger: Logger; + fs?: typeof fsMod; +}) { const dataStore = getDataStoreFile(settings); if (fs.existsSync(dataStore)) { logger.debug('content', 'clearing data store'); @@ -138,14 +142,21 @@ export async function syncInternal({ }); await contentLayer.sync(); settings.timer.end('Sync content layer'); - } else if (fs.existsSync(fileURLToPath(getContentPaths(settings.config, fs).contentDir))) { + } else { + const paths = getContentPaths(settings.config, fs); // Content is synced after writeFiles. That means references are not created // To work around it, we create a stub so the reference is created and content // sync will override the empty file - settings.injectedTypes.push({ - filename: CONTENT_TYPES_FILE, - content: '', - }); + if ( + paths.config.exists || + // Legacy collections don't require a config file + (settings.config.legacy?.collections && fs.existsSync(paths.contentDir)) + ) { + settings.injectedTypes.push({ + filename: CONTENT_TYPES_FILE, + content: '', + }); + } } syncAstroEnv(settings); diff --git a/packages/astro/test/content-layer.test.js b/packages/astro/test/content-layer.test.js index 16aa96c293..aaf7c594f1 100644 --- a/packages/astro/test/content-layer.test.js +++ b/packages/astro/test/content-layer.test.js @@ -1,5 +1,5 @@ import assert from 'node:assert/strict'; -import { promises as fs } from 'node:fs'; +import { promises as fs, existsSync } from 'node:fs'; import { sep } from 'node:path'; import { sep as posixSep } from 'node:path/posix'; import { after, before, describe, it } from 'node:test'; @@ -313,6 +313,13 @@ describe('Content Layer', () => { devServer?.stop(); }); + + it('Generates content types files', async () => { + assert.ok(existsSync(new URL('./.astro/content.d.ts', fixture.config.root))); + const data = await fs.readFile(new URL('./.astro/types.d.ts', fixture.config.root), 'utf-8'); + assert.match(data, / { assert.ok(json.hasOwnProperty('customLoader')); assert.ok(Array.isArray(json.customLoader));