Home / Function/ astroContentAssetPropagationPlugin() — astro Function Reference

astroContentAssetPropagationPlugin() — astro Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  121a0a31_a45b_2d2b_1133_094c7123cad8["astroContentAssetPropagationPlugin()"]
  7a46444b_6633_b7e5_30ac_f5b4aa6a43ff["vite-plugin-content-assets.ts"]
  121a0a31_a45b_2d2b_1133_094c7123cad8 -->|defined in| 7a46444b_6633_b7e5_30ac_f5b4aa6a43ff
  b4620bc6_fff6_cf0a_dfd0_21b3625d5f6f["getStylesForURL()"]
  121a0a31_a45b_2d2b_1133_094c7123cad8 -->|calls| b4620bc6_fff6_cf0a_dfd0_21b3625d5f6f
  style 121a0a31_a45b_2d2b_1133_094c7123cad8 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/content/vite-plugin-content-assets.ts lines 26–138

export function astroContentAssetPropagationPlugin({
	settings,
}: {
	settings: AstroSettings;
}): Plugin {
	let environment: RunnableDevEnvironment | undefined = undefined;
	return {
		name: 'astro:content-asset-propagation',
		enforce: 'pre',
		resolveId: {
			filter: {
				id: new RegExp(`(?:\\?|&)(?:${CONTENT_IMAGE_FLAG}|${CONTENT_RENDER_FLAG})(?:&|=|$)`),
			},
			async handler(id, importer, opts) {
				if (hasContentFlag(id, CONTENT_IMAGE_FLAG)) {
					const [base, query] = id.split('?');
					const params = new URLSearchParams(query);
					const importerParam = params.get('importer');

					const importerPath = importerParam
						? fileURLToPath(new URL(importerParam, settings.config.root))
						: importer;

					const resolved = await this.resolve(base, importerPath, { skipSelf: true, ...opts });
					if (!resolved) {
						throw new AstroError({
							...AstroErrorData.ImageNotFound,
							message: AstroErrorData.ImageNotFound.message(base),
						});
					}
					return resolved;
				}
				if (hasContentFlag(id, CONTENT_RENDER_FLAG)) {
					const base = id.split('?')[0];

					for (const { extensions, handlePropagation = true } of settings.contentEntryTypes) {
						if (handlePropagation && extensions.includes(extname(base))) {
							return this.resolve(`${base}?${PROPAGATED_ASSET_FLAG}`, importer, {
								skipSelf: true,
								...opts,
							});
						}
					}
					// Resolve to the base id (no content flags)
					// if Astro doesn't need to handle propagation.
					return this.resolve(base, importer, { skipSelf: true, ...opts });
				}
			},
		},
		configureServer(server) {
			if (!isRunnableDevEnvironment(server.environments[ASTRO_VITE_ENVIRONMENT_NAMES.ssr])) {
				return;
			}
			environment = server.environments[ASTRO_VITE_ENVIRONMENT_NAMES.ssr] as RunnableDevEnvironment;
		},
		transform: {
			filter: {
				id: new RegExp(`(?:\\?|&)${PROPAGATED_ASSET_FLAG}(?:&|=|$)`),
			},
			async handler(_, id) {
				if (hasContentFlag(id, PROPAGATED_ASSET_FLAG)) {
					const basePath = id.split('?')[0];
					let stringifiedLinks: string, stringifiedStyles: string;

					// We can access the server in dev,
					// so resolve collected styles and scripts here.
					if (isAstroServerEnvironment(this.environment) && environment) {
						if (!environment.moduleGraph.getModuleById(basePath)?.ssrModule) {
							await environment.runner.import(basePath);
						}
						const {
							styles,
							urls,
							crawledFiles: styleCrawledFiles,
						} = await getStylesForURL(basePath, environment);

						// Register files we crawled to be able to retrieve the rendered styles and scripts,
						// as when they get updated, we need to re-transform ourselves.
						// We also only watch files within the user source code, as changes in node_modules
						// are usually also ignored by Vite.
						for (const file of styleCrawledFiles) {

Subdomains

Frequently Asked Questions

What does astroContentAssetPropagationPlugin() do?
astroContentAssetPropagationPlugin() is a function in the astro codebase, defined in packages/astro/src/content/vite-plugin-content-assets.ts.
Where is astroContentAssetPropagationPlugin() defined?
astroContentAssetPropagationPlugin() is defined in packages/astro/src/content/vite-plugin-content-assets.ts at line 26.
What does astroContentAssetPropagationPlugin() call?
astroContentAssetPropagationPlugin() calls 1 function(s): getStylesForURL.

Analyze Your Own Codebase

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

Try Supermodel Free