diff options
author | 2024-11-29 16:02:12 +0000 | |
---|---|---|
committer | 2024-11-29 16:02:12 +0000 | |
commit | 97f413f1189fd626dffac8b48b166684c7e77627 (patch) | |
tree | 8807b6e2d745e68864454ce2e4677d66e09cd907 | |
parent | 6031962ab5f56457de986eb82bd24807e926ba1b (diff) | |
download | astro-97f413f1189fd626dffac8b48b166684c7e77627.tar.gz astro-97f413f1189fd626dffac8b48b166684c7e77627.tar.zst astro-97f413f1189fd626dffac8b48b166684c7e77627.zip |
fix: generate types even without src/content (#12565)
* fix: generate types even without src/content
* chore: add test
-rw-r--r-- | .changeset/blue-gorillas-accept.md | 5 | ||||
-rw-r--r-- | packages/astro/src/core/sync/index.ts | 23 | ||||
-rw-r--r-- | packages/astro/test/content-layer.test.js | 9 |
3 files changed, 30 insertions, 7 deletions
diff --git a/.changeset/blue-gorillas-accept.md b/.changeset/blue-gorillas-accept.md new file mode 100644 index 000000000..b122a9558 --- /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 67fdccc54..823470e97 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 16aa96c29..aaf7c594f 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, /<reference path="content.d.ts"/); + }); + it('Returns custom loader collection', async () => { assert.ok(json.hasOwnProperty('customLoader')); assert.ok(Array.isArray(json.customLoader)); |