Home / Function/ autogenerateCollections() — astro Function Reference

autogenerateCollections() — astro Function Reference

Architecture documentation for the autogenerateCollections() function in utils.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  9f7b1f5d_6264_645b_77b9_79aaa945256d["autogenerateCollections()"]
  7a09e708_c090_71c0_8138_7343699b1865["utils.ts"]
  9f7b1f5d_6264_645b_77b9_79aaa945256d -->|defined in| 7a09e708_c090_71c0_8138_7343699b1865
  6820fa7c_c0e9_20cf_23d1_0ee1afd42476["reloadContentConfigObserver()"]
  6820fa7c_c0e9_20cf_23d1_0ee1afd42476 -->|calls| 9f7b1f5d_6264_645b_77b9_79aaa945256d
  6d302b91_7577_1260_3810_92b7b21e8427["getContentEntryExts()"]
  9f7b1f5d_6264_645b_77b9_79aaa945256d -->|calls| 6d302b91_7577_1260_3810_92b7b21e8427
  644b6926_dce5_a525_9e32_507bd7352a73["getDataEntryExts()"]
  9f7b1f5d_6264_645b_77b9_79aaa945256d -->|calls| 644b6926_dce5_a525_9e32_507bd7352a73
  681405dc_44fb_0b21_802d_47dcdc4e75da["globWithUnderscoresIgnored()"]
  9f7b1f5d_6264_645b_77b9_79aaa945256d -->|calls| 681405dc_44fb_0b21_802d_47dcdc4e75da
  style 9f7b1f5d_6264_645b_77b9_79aaa945256d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/content/utils.ts lines 542–627

async function autogenerateCollections({
	config,
	settings,
	fs,
}: {
	config?: ContentConfig;
	settings: AstroSettings;
	fs: typeof fsMod;
}): Promise<ContentConfig | undefined> {
	if (!config) {
		return config;
	}

	if (!settings.config.legacy?.collectionsBackwardsCompat) {
		return config;
	}

	const contentDir = new URL('./content/', settings.config.srcDir);
	const collections: Record<string, CollectionConfig> = config.collections ?? {};

	const contentExts = getContentEntryExts(settings);
	const dataExts = getDataEntryExts(settings);

	const contentPattern = globWithUnderscoresIgnored('', contentExts);
	const dataPattern = globWithUnderscoresIgnored('', dataExts);

	let usesContentLayer = false;
	for (const collectionName of Object.keys(collections)) {
		const collection = collections[collectionName];

		if (collection?.type === CONTENT_LAYER_TYPE || collection?.type === LIVE_CONTENT_TYPE) {
			usesContentLayer = true;
			continue;
		}

		const isDataCollection = collection?.type === 'data';
		const base = new URL(`${collectionName}/`, contentDir);

		collections[collectionName] = {
			...collection,
			type: CONTENT_LAYER_TYPE,
			loader: glob({
				base,
				pattern: isDataCollection ? dataPattern : contentPattern,
				[secretLegacyFlag]: true,
			}) as any,
		};
	}

	if (!usesContentLayer && fs.existsSync(contentDir)) {
		const orphanedCollections = [];
		for (const entry of await fs.promises.readdir(contentDir, { withFileTypes: true })) {
			const collectionName = entry.name;

			if (['_', '.'].includes(collectionName.at(0) ?? '')) {
				continue;
			}

			if (entry.isDirectory() && !(collectionName in collections)) {
				orphanedCollections.push(collectionName);
				const base = new URL(`${collectionName}/`, contentDir);
				collections[collectionName] = {
					type: CONTENT_LAYER_TYPE,
					loader: glob({
						base,
						pattern: contentPattern,
						[secretLegacyFlag]: true,
					}) as any,
				};
			}
		}

		if (orphanedCollections.length > 0) {
			console.warn(
				`
Auto-generating collections for folders in "src/content/" that are not defined as collections.
This is deprecated, so you should define these collections yourself in "src/content.config.ts".
The following collections have been auto-generated: ${orphanedCollections
					.map((name) => colors.green(name))
					.join(', ')}\n`,
			);

Subdomains

Frequently Asked Questions

What does autogenerateCollections() do?
autogenerateCollections() is a function in the astro codebase, defined in packages/astro/src/content/utils.ts.
Where is autogenerateCollections() defined?
autogenerateCollections() is defined in packages/astro/src/content/utils.ts at line 542.
What does autogenerateCollections() call?
autogenerateCollections() calls 3 function(s): getContentEntryExts, getDataEntryExts, globWithUnderscoresIgnored.
What calls autogenerateCollections()?
autogenerateCollections() is called by 1 function(s): reloadContentConfigObserver.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free