Home / Function/ emitImageMetadata() — astro Function Reference

emitImageMetadata() — astro Function Reference

Architecture documentation for the emitImageMetadata() function in node.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  3a4327f8_602e_101e_f3dd_6616f760fe79["emitImageMetadata()"]
  cb45fbc5_a108_7969_b027_ddb8eab65943["node.ts"]
  3a4327f8_602e_101e_f3dd_6616f760fe79 -->|defined in| cb45fbc5_a108_7969_b027_ddb8eab65943
  a76ac834_db5a_84b2_dcc7_813558f1bb72["fileURLToNormalizedPath()"]
  3a4327f8_602e_101e_f3dd_6616f760fe79 -->|calls| a76ac834_db5a_84b2_dcc7_813558f1bb72
  609d5d31_892d_f263_3e18_9f1fa7712cd0["handleSvgDeduplication()"]
  3a4327f8_602e_101e_f3dd_6616f760fe79 -->|calls| 609d5d31_892d_f263_3e18_9f1fa7712cd0
  style 3a4327f8_602e_101e_f3dd_6616f760fe79 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/assets/utils/node.ts lines 72–139

export async function emitImageMetadata(
	id: string | undefined,
	fileEmitter?: FileEmitter,
): Promise<ImageMetadataWithContents | undefined> {
	if (!id) {
		return undefined;
	}

	const url = pathToFileURL(id);
	let fileData: Buffer;
	try {
		fileData = await fs.readFile(url);
	} catch {
		return undefined;
	}

	const fileMetadata = await imageMetadata(fileData, id);

	const emittedImage: Omit<ImageMetadataWithContents, 'fsPath'> = {
		src: '',
		...fileMetadata,
	};

	// Private for now, we generally don't want users to rely on filesystem paths, but we need it so that we can maybe remove the original asset from the build if it's unused.
	Object.defineProperty(emittedImage, 'fsPath', {
		enumerable: false,
		writable: false,
		value: fileURLToNormalizedPath(url),
	});

	// Build
	let isBuild = typeof fileEmitter === 'function';
	if (isBuild) {
		const pathname = decodeURI(url.pathname);
		const filename = path.basename(pathname, path.extname(pathname) + `.${fileMetadata.format}`);

		try {
			let handle: string;

			if (fileMetadata.format === 'svg') {
				// check if this content already exists
				handle = await handleSvgDeduplication(fileData, filename, fileEmitter!);
			} else {
				// Non-SVG assets: emit normally
				handle = fileEmitter!({
					name: filename,
					source: fileData,
					type: 'asset',
				});
			}

			emittedImage.src = `__ASTRO_ASSET_IMAGE__${handle}__`;
		} catch {
			isBuild = false;
		}
	}

	if (!isBuild) {
		// Pass the original file information through query params so we don't have to load the file twice
		url.searchParams.append('origWidth', fileMetadata.width.toString());
		url.searchParams.append('origHeight', fileMetadata.height.toString());
		url.searchParams.append('origFormat', fileMetadata.format);

		emittedImage.src = `/@fs` + prependForwardSlash(fileURLToNormalizedPath(url));
	}

	return emittedImage as ImageMetadataWithContents;
}

Domain

Subdomains

Frequently Asked Questions

What does emitImageMetadata() do?
emitImageMetadata() is a function in the astro codebase, defined in packages/astro/src/assets/utils/node.ts.
Where is emitImageMetadata() defined?
emitImageMetadata() is defined in packages/astro/src/assets/utils/node.ts at line 72.
What does emitImageMetadata() call?
emitImageMetadata() calls 2 function(s): fileURLToNormalizedPath, handleSvgDeduplication.

Analyze Your Own Codebase

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

Try Supermodel Free