Home / Function/ contentAssetsBuildPostHook() — astro Function Reference

contentAssetsBuildPostHook() — astro Function Reference

Architecture documentation for the contentAssetsBuildPostHook() function in vite-plugin-content-assets.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  57133959_6bc1_0eb6_193e_1e919c67f81f["contentAssetsBuildPostHook()"]
  7a46444b_6633_b7e5_30ac_f5b4aa6a43ff["vite-plugin-content-assets.ts"]
  57133959_6bc1_0eb6_193e_1e919c67f81f -->|defined in| 7a46444b_6633_b7e5_30ac_f5b4aa6a43ff
  style 57133959_6bc1_0eb6_193e_1e919c67f81f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/content/vite-plugin-content-assets.ts lines 210–277

export async function contentAssetsBuildPostHook(
	base: string,
	assetsPrefix: AssetsPrefix | undefined,
	internals: BuildInternals,
	{
		chunks,
		mutate,
	}: {
		chunks: ExtractedChunk[];
		mutate: (fileName: string, code: string, prerender: boolean) => void;
	},
) {
	// Process each chunk that contains placeholder placeholders for styles/links
	for (const chunk of chunks) {
		// Skip chunks that don't have content placeholders to inject
		if (!chunk.code.includes(LINKS_PLACEHOLDER)) continue;

		const entryStyles = new Set<string>();
		const entryLinks = new Set<string>();

		// For each module in this chunk, look up propagated styles from the map
		for (const id of chunk.moduleIds) {
			const entryCss = internals.propagatedStylesMap.get(id);
			if (entryCss) {
				// Collect both inline content and external links
				// TODO: Separating styles and links this way is not ideal. The `entryCss` list is order-sensitive
				// and splitting them into two sets causes the order to be lost, because styles are rendered after
				// links. Refactor this away in the future.
				for (const value of entryCss) {
					if (value.type === 'inline') entryStyles.add(value.content);
					if (value.type === 'external') {
						let href: string;
						if (assetsPrefix) {
							const pf = getAssetsPrefix(fileExtension(value.src), assetsPrefix);
							href = joinPaths(pf, slash(value.src));
						} else {
							href = prependForwardSlash(joinPaths(base, slash(value.src)));
						}
						entryLinks.add(href);
					}
				}
			}
		}

		// Replace placeholders with actual styles and links
		let newCode = chunk.code;
		if (entryStyles.size) {
			newCode = newCode.replace(
				JSON.stringify(STYLES_PLACEHOLDER),
				JSON.stringify(Array.from(entryStyles)),
			);
		} else {
			// Replace with empty array if no styles found
			newCode = newCode.replace(JSON.stringify(STYLES_PLACEHOLDER), '[]');
		}
		if (entryLinks.size) {
			newCode = newCode.replace(
				JSON.stringify(LINKS_PLACEHOLDER),
				JSON.stringify(Array.from(entryLinks)),
			);
		} else {
			// Replace with empty array if no links found
			newCode = newCode.replace(JSON.stringify(LINKS_PLACEHOLDER), '[]');
		}
		// Persist the mutation for writing to disk
		mutate(chunk.fileName, newCode, chunk.prerender);
	}
}

Subdomains

Frequently Asked Questions

What does contentAssetsBuildPostHook() do?
contentAssetsBuildPostHook() is a function in the astro codebase, defined in packages/astro/src/content/vite-plugin-content-assets.ts.
Where is contentAssetsBuildPostHook() defined?
contentAssetsBuildPostHook() is defined in packages/astro/src/content/vite-plugin-content-assets.ts at line 210.

Analyze Your Own Codebase

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

Try Supermodel Free