0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

fix: generate types even without src/content (#12565)

* fix: generate types even without src/content

* chore: add test
This commit is contained in:
Matt Kane 2024-11-29 16:02:12 +00:00 committed by GitHub
parent 6031962ab5
commit 97f413f118
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 30 additions and 7 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes a bug where content types were not generated when first running astro dev unless src/content exists

View file

@ -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);

View file

@ -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));