mirror of
https://github.com/withastro/astro.git
synced 2025-02-03 22:29:08 -05:00
Refine content collection warnings (#10001)
* feat: remove nonexistent collection warning * fix: remove markdown syntax from console.warn * fix: respect configured collections in types when dir is missing * chore: changeset * chore: revert test schema * docs: "Removes" Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> --------- Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
This commit is contained in:
parent
5759fd9947
commit
748b2e87cd
3 changed files with 13 additions and 29 deletions
7
.changeset/hip-hotels-divide.md
Normal file
7
.changeset/hip-hotels-divide.md
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
---
|
||||||
|
"astro": minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Removes content collection warning when a configured collection does not have a matching directory name. This should resolve `i18n` collection warnings for Starlight users.
|
||||||
|
|
||||||
|
This also ensures configured collection names are always included in `getCollection()` and `getEntry()` types even when a matching directory is absent. We hope this allows users to discover typos during development by surfacing type information.
|
|
@ -62,7 +62,9 @@ export function createGetCollection({
|
||||||
} else {
|
} else {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.warn(
|
console.warn(
|
||||||
`The collection **${collection}** does not exist or is empty. Ensure a collection directory with this name exists.`
|
`The collection ${JSON.stringify(
|
||||||
|
collection
|
||||||
|
)} does not exist or is empty. Ensure a collection directory with this name exists.`
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -312,13 +312,6 @@ export async function createContentTypesGenerator({
|
||||||
viteServer,
|
viteServer,
|
||||||
});
|
});
|
||||||
invalidateVirtualMod(viteServer);
|
invalidateVirtualMod(viteServer);
|
||||||
if (observable.status === 'loaded') {
|
|
||||||
warnNonexistentCollections({
|
|
||||||
logger,
|
|
||||||
contentConfig: observable.config,
|
|
||||||
collectionEntryMap,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return { init, queueEvent };
|
return { init, queueEvent };
|
||||||
|
@ -370,6 +363,9 @@ async function writeContentFiles({
|
||||||
}) {
|
}) {
|
||||||
let contentTypesStr = '';
|
let contentTypesStr = '';
|
||||||
let dataTypesStr = '';
|
let dataTypesStr = '';
|
||||||
|
for (const [collection, config] of Object.entries(contentConfig?.collections ?? {})) {
|
||||||
|
collectionEntryMap[JSON.stringify(collection)] ??= { type: config.type, entries: {} };
|
||||||
|
}
|
||||||
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];
|
||||||
|
@ -455,24 +451,3 @@ async function writeContentFiles({
|
||||||
typeTemplateContent
|
typeTemplateContent
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function warnNonexistentCollections({
|
|
||||||
contentConfig,
|
|
||||||
collectionEntryMap,
|
|
||||||
logger,
|
|
||||||
}: {
|
|
||||||
contentConfig: ContentConfig;
|
|
||||||
collectionEntryMap: CollectionEntryMap;
|
|
||||||
logger: Logger;
|
|
||||||
}) {
|
|
||||||
for (const configuredCollection in contentConfig.collections) {
|
|
||||||
if (!collectionEntryMap[JSON.stringify(configuredCollection)]) {
|
|
||||||
logger.warn(
|
|
||||||
'content',
|
|
||||||
`The ${bold(configuredCollection)} collection is defined but no ${bold(
|
|
||||||
'content/' + configuredCollection
|
|
||||||
)} folder exists in the content directory. Create a new folder for the collection, or check your content configuration file for typos.`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue