Skip to content

Commit

Permalink
fix: generate types even without src/content (#12565)
Browse files Browse the repository at this point in the history
* fix: generate types even without src/content

* chore: add test
  • Loading branch information
ascorbic authored Nov 29, 2024
1 parent 6031962 commit 97f413f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-gorillas-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a bug where content types were not generated when first running astro dev unless src/content exists
23 changes: 17 additions & 6 deletions packages/astro/src/core/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);

Expand Down
9 changes: 8 additions & 1 deletion packages/astro/test/content-layer.test.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 97f413f

Please sign in to comment.