mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
Content collections - fix .json
collection errors (#7246)
* fix: avoid error on collectionType === 'unknown' * fix: ignore underscores in file globs * chore: clarify [!_] * fix: mismatch error not throwing * fix: bad collectionType var * test: no error for empty collection * chore: changeset
This commit is contained in:
parent
8da07ab754
commit
f5063d0a01
4 changed files with 52 additions and 7 deletions
5
.changeset/olive-rabbits-rhyme.md
Normal file
5
.changeset/olive-rabbits-rhyme.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix content collection build errors for empty collections or underscore files of type `.json`.
|
|
@ -414,7 +414,11 @@ async function writeContentFiles({
|
||||||
for (const collectionKey of Object.keys(collectionEntryMap).sort()) {
|
for (const collectionKey of Object.keys(collectionEntryMap).sort()) {
|
||||||
const collectionConfig = contentConfig?.collections[JSON.parse(collectionKey)];
|
const collectionConfig = contentConfig?.collections[JSON.parse(collectionKey)];
|
||||||
const collection = collectionEntryMap[collectionKey];
|
const collection = collectionEntryMap[collectionKey];
|
||||||
if (collectionConfig?.type && collection.type !== collectionConfig.type) {
|
if (
|
||||||
|
collectionConfig?.type &&
|
||||||
|
collection.type !== 'unknown' &&
|
||||||
|
collection.type !== collectionConfig.type
|
||||||
|
) {
|
||||||
viteServer.ws.send({
|
viteServer.ws.send({
|
||||||
type: 'error',
|
type: 'error',
|
||||||
err: new AstroError({
|
err: new AstroError({
|
||||||
|
@ -433,7 +437,14 @@ async function writeContentFiles({
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
switch (collection.type) {
|
const resolvedType: 'content' | 'data' =
|
||||||
|
collection.type === 'unknown'
|
||||||
|
? // Add empty / unknown collections to the data type map by default
|
||||||
|
// This ensures `getCollection('empty-collection')` doesn't raise a type error
|
||||||
|
collectionConfig?.type ?? 'data'
|
||||||
|
: collection.type;
|
||||||
|
|
||||||
|
switch (resolvedType) {
|
||||||
case 'content':
|
case 'content':
|
||||||
contentTypesStr += `${collectionKey}: {\n`;
|
contentTypesStr += `${collectionKey}: {\n`;
|
||||||
for (const entryKey of Object.keys(collection.entries).sort()) {
|
for (const entryKey of Object.keys(collection.entries).sort()) {
|
||||||
|
@ -449,9 +460,6 @@ async function writeContentFiles({
|
||||||
contentTypesStr += `};\n`;
|
contentTypesStr += `};\n`;
|
||||||
break;
|
break;
|
||||||
case 'data':
|
case 'data':
|
||||||
// Add empty / unknown collections to the data type map by default
|
|
||||||
// This ensures `getCollection('empty-collection')` doesn't raise a type error
|
|
||||||
case 'unknown':
|
|
||||||
dataTypesStr += `${collectionKey}: {\n`;
|
dataTypesStr += `${collectionKey}: {\n`;
|
||||||
for (const entryKey of Object.keys(collection.entries).sort()) {
|
for (const entryKey of Object.keys(collection.entries).sort()) {
|
||||||
const dataType = collectionConfig?.schema ? `InferEntrySchema<${collectionKey}>` : 'any';
|
const dataType = collectionConfig?.schema ? `InferEntrySchema<${collectionKey}>` : 'any';
|
||||||
|
|
|
@ -43,8 +43,12 @@ export function astroContentVirtualModPlugin({
|
||||||
new URL('reference-map.json', contentPaths.cacheDir).pathname
|
new URL('reference-map.json', contentPaths.cacheDir).pathname
|
||||||
)
|
)
|
||||||
.replace('@@CONTENT_DIR@@', relContentDir)
|
.replace('@@CONTENT_DIR@@', relContentDir)
|
||||||
.replace('@@CONTENT_ENTRY_GLOB_PATH@@', `${relContentDir}**/*${getExtGlob(contentEntryExts)}`)
|
.replace(
|
||||||
.replace('@@DATA_ENTRY_GLOB_PATH@@', `${relContentDir}**/*${getExtGlob(dataEntryExts)}`)
|
'@@CONTENT_ENTRY_GLOB_PATH@@',
|
||||||
|
// [!_] = ignore files starting with "_"
|
||||||
|
`${relContentDir}**/[!_]*${getExtGlob(contentEntryExts)}`
|
||||||
|
)
|
||||||
|
.replace('@@DATA_ENTRY_GLOB_PATH@@', `${relContentDir}**/[!_]*${getExtGlob(dataEntryExts)}`)
|
||||||
.replace(
|
.replace(
|
||||||
'@@RENDER_ENTRY_GLOB_PATH@@',
|
'@@RENDER_ENTRY_GLOB_PATH@@',
|
||||||
`${relContentDir}**/*${getExtGlob(/** Note: data collections excluded */ contentEntryExts)}`
|
`${relContentDir}**/*${getExtGlob(/** Note: data collections excluded */ contentEntryExts)}`
|
||||||
|
|
|
@ -119,4 +119,32 @@ title: Post
|
||||||
expect(e.hint).to.include("Try adding `type: 'data'`");
|
expect(e.hint).to.include("Try adding `type: 'data'`");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('does not raise error for empty collection with config', async () => {
|
||||||
|
const fs = createFsWithFallback(
|
||||||
|
{
|
||||||
|
// Add placeholder to ensure directory exists
|
||||||
|
'/src/content/i18n/_placeholder.txt': 'Need content here',
|
||||||
|
'/src/content/config.ts': `
|
||||||
|
import { z, defineCollection } from 'astro:content';
|
||||||
|
|
||||||
|
const i18n = defineCollection({
|
||||||
|
type: 'data',
|
||||||
|
schema: z.object({
|
||||||
|
greeting: z.string(),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const collections = { i18n };`,
|
||||||
|
},
|
||||||
|
root
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await sync({ fs });
|
||||||
|
expect(res).to.equal(0);
|
||||||
|
} catch (e) {
|
||||||
|
expect.fail(0, 1, `Did not expect sync to throw: ${e.message}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue