Home / File/ svg.ts — astro Source File

svg.ts — astro Source File

Architecture documentation for svg.ts, a typescript file in the astro codebase. 6 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 6 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  d53c456b_ca45_596d_c3fb_0f448cc565ec["svg.ts"]
  ef8a1e3f_e350_75a6_b92d_62a8566d8db9["../core/errors/index.js"]
  d53c456b_ca45_596d_c3fb_0f448cc565ec --> ef8a1e3f_e350_75a6_b92d_62a8566d8db9
  c32d12e2_d85e_28c0_eea7_9b29629857e0["../types/public/config.js"]
  d53c456b_ca45_596d_c3fb_0f448cc565ec --> c32d12e2_d85e_28c0_eea7_9b29629857e0
  2f4887f6_f326_35b7_c8e4_5d371a87bf9b["../runtime.js"]
  d53c456b_ca45_596d_c3fb_0f448cc565ec --> 2f4887f6_f326_35b7_c8e4_5d371a87bf9b
  416eda3d_47e6_c298_102b_b7a37d86a44e["../../assets/types.js"]
  d53c456b_ca45_596d_c3fb_0f448cc565ec --> 416eda3d_47e6_c298_102b_b7a37d86a44e
  21fed27e_530c_dc1d_9c13_6eebedd78776["svgo"]
  d53c456b_ca45_596d_c3fb_0f448cc565ec --> 21fed27e_530c_dc1d_9c13_6eebedd78776
  115170fa_0670_7778_2f13_84e482d5f344["ultrahtml"]
  d53c456b_ca45_596d_c3fb_0f448cc565ec --> 115170fa_0670_7778_2f13_84e482d5f344
  style d53c456b_ca45_596d_c3fb_0f448cc565ec fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { optimize } from 'svgo';
import { parse, renderSync } from 'ultrahtml';
import { AstroError, AstroErrorData } from '../../core/errors/index.js';
import type { AstroConfig } from '../../types/public/config.js';
import type { SvgComponentProps } from '../runtime.js';
import { dropAttributes } from '../runtime.js';
import type { ImageMetadata } from '../types.js';

function parseSvg({
	path,
	contents,
	svgoConfig,
}: {
	path: string;
	contents: string;
	svgoConfig: AstroConfig['experimental']['svgo'];
}) {
	let processedContents = contents;
	if (svgoConfig) {
		try {
			const config = typeof svgoConfig === 'boolean' ? undefined : svgoConfig;
			const result = optimize(contents, config);
			processedContents = result.data;
		} catch (cause) {
			throw new AstroError(
				{
					...AstroErrorData.CannotOptimizeSvg,
					message: AstroErrorData.CannotOptimizeSvg.message(path),
				},
				{ cause },
			);
		}
	}
	const root = parse(processedContents);
	const svgNode = root.children.find(
		({ name, type }: { name: string; type: number }) => type === 1 /* Element */ && name === 'svg',
	);
	if (!svgNode) {
		throw new Error('SVG file does not contain an <svg> element');
	}
	const { attributes, children } = svgNode;
	const body = renderSync({ ...root, children });

	return { attributes, body };
}

export function makeSvgComponent(
	meta: ImageMetadata,
	contents: Buffer | string,
	svgoConfig: AstroConfig['experimental']['svgo'],
): string {
	const file = typeof contents === 'string' ? contents : contents.toString('utf-8');
	const { attributes, body: children } = parseSvg({
		path: meta.fsPath,
		contents: file,
		svgoConfig,
	});
	const props: SvgComponentProps = {
		meta,
		attributes: dropAttributes(attributes),
		children,
	};

	return `import { createSvgComponent } from 'astro/assets/runtime';
export default createSvgComponent(${JSON.stringify(props)})`;
}

Domain

Subdomains

Dependencies

  • ../../assets/types.js
  • ../core/errors/index.js
  • ../runtime.js
  • ../types/public/config.js
  • svgo
  • ultrahtml

Frequently Asked Questions

What does svg.ts do?
svg.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in svg.ts?
svg.ts defines 2 function(s): makeSvgComponent, parseSvg.
What does svg.ts depend on?
svg.ts imports 6 module(s): ../../assets/types.js, ../core/errors/index.js, ../runtime.js, ../types/public/config.js, svgo, ultrahtml.
Where is svg.ts in the architecture?
svg.ts is located at packages/astro/src/assets/utils/svg.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/assets/utils).

Analyze Your Own Codebase

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

Try Supermodel Free