Repository: withastro/astro
File: packages/astro/src/content/vite-plugin-content-virtual-mod.ts (TypeScript)

Code before:
		dataEntryGlobResult = createGlob(
			globWithUnderscoresIgnored(relContentDir, dataEntryExts),
			DATA_FLAG,
		);
		renderEntryGlobResult = createGlob(
			globWithUnderscoresIgnored(relContentDir, contentEntryExts),
			CONTENT_RENDER_FLAG,
		);
	} else {
		contentEntryGlobResult = getStringifiedCollectionFromLookup(
			'content',
			relContentDir,
			lookupMap,
		);
		dataEntryGlobResult = getStringifiedCollectionFromLookup('data', relContentDir, lookupMap);
		renderEntryGlobResult = getStringifiedCollectionFromLookup('render', relContentDir, lookupMap);
	}

	let virtualModContents: string;
	if(isClient) {
		// Empty exports is probably enough to break the build.
		// If for some reason its being imported as a side-effect ala `import 'astro:content';` then throw an error message.
		virtualModContents = `export {};
throw new Error('astro:content is only supported running server-side.');`;

		logger.error('content', 'astro:content is only supported running server-side.');
	} else {
		 virtualModContents =
			nodeFs
				.readFileSync(contentPaths.virtualModTemplate, 'utf-8')
				.replace('@@CONTENT_DIR@@', relContentDir)
				.replace("'@@CONTENT_ENTRY_GLOB_PATH@@'", contentEntryGlobResult)
				.replace("'@@DATA_ENTRY_GLOB_PATH@@'", dataEntryGlobResult)
				.replace("'@@RENDER_ENTRY_GLOB_PATH@@'", renderEntryGlobResult)
				.replace('/* @@LOOKUP_MAP_ASSIGNMENT@@ */', `lookupMap = ${JSON.stringify(lookupMap)};`);
	}

	return virtualModContents;
}

function getStringifiedCollectionFromLookup(
	wantedType: 'content' | 'data' | 'render',
	relContentDir: string,
	lookupMap: ContentLookupMap,
) {
	let str = '{';
	// In dev, we don't need to normalize the import specifier at all. Vite handles it.
	let normalize = (slug: string) => slug;
	// For prod builds, we need to transform from `/src/content/**/*.{md,mdx,json,yaml}` to a relative `./**/*.mjs` import
	if (process.env.NODE_ENV === 'production') {
		const suffix = wantedType === 'render' ? '.entry.mjs' : '.mjs';

Review comment (suggestion):
We could throw an `AstroError` to make it break immediately with the instruction about where it is being used and should be removed right

Code after:
			CONTENT_RENDER_FLAG,
		);
	} else {
		contentEntryGlobResult = getStringifiedCollectionFromLookup(
			'content',
			relContentDir,
			lookupMap,
		);
		dataEntryGlobResult = getStringifiedCollectionFromLookup('data', relContentDir, lookupMap);
		renderEntryGlobResult = getStringifiedCollectionFromLookup('render', relContentDir, lookupMap);
	}

	let virtualModContents: string;
	if(isClient) {
		throw new AstroError({
			...AstroErrorData.ServerOnlyModule,
			message: AstroErrorData.ServerOnlyModule.message('astro:content'),
		});
	} else {
		 virtualModContents =
			nodeFs
				.readFileSync(contentPaths.virtualModTemplate, 'utf-8')
				.replace('@@CONTENT_DIR@@', relContentDir)
				.replace("'@@CONTENT_ENTRY_GLOB_PATH@@'", contentEntryGlobResult)
				.replace("'@@DATA_ENTRY_GLOB_PATH@@'", dataEntryGlobResult)
				.replace("'@@RENDER_ENTRY_GLOB_PATH@@'", renderEntryGlobResult)
				.replace('/* @@LOOKUP_MAP_ASSIGNMENT@@ */', `lookupMap = ${JSON.stringify(lookupMap)};`);
	}

	return virtualModContents;
}

function getStringifiedCollectionFromLookup(
	wantedType: 'content' | 'data' | 'render',
	relContentDir: string,
	lookupMap: ContentLookupMap,
) {
	let str = '{';
	// In dev, we don't need to normalize the import specifier at all. Vite handles it.
	let normalize = (slug: string) => slug;
	// For prod builds, we need to transform from `/src/content/**/*.{md,mdx,json,yaml}` to a relative `./**/*.mjs` import
	if (process.env.NODE_ENV === 'production') {
		const suffix = wantedType === 'render' ? '.entry.mjs' : '.mjs';
		normalize = (slug: string) =>
			`${removeFileExtension(encodeName(slug)).replace(relContentDir, './')}${suffix}`;
	} else {
		let suffix = '';
		if (wantedType === 'content') suffix = CONTENT_FLAG;
		else if (wantedType === 'data') suffix = DATA_FLAG;
		else if (wantedType === 'render') suffix = CONTENT_RENDER_FLAG;
		normalize = (slug: string) => `${slug}?${suffix}`;