Home / File/ build-service.ts — astro Source File

build-service.ts — astro Source File

Architecture documentation for build-service.ts, a typescript file in the astro codebase. 4 imports, 0 dependents.

File typescript CoreAstro CoreMiddleware 4 imports 5 functions

Entity Profile

Dependency Diagram

graph LR
  50d6c5a0_3bd9_55ef_d9d2_9d25a375dbf7["build-service.ts"]
  33ed95cd_96ff_f301_bc85_29b41d61d35b["./image/shared.js"]
  50d6c5a0_3bd9_55ef_d9d2_9d25a375dbf7 --> 33ed95cd_96ff_f301_bc85_29b41d61d35b
  f16d8c76_2866_6150_bd14_0347b59abfe9["astro"]
  50d6c5a0_3bd9_55ef_d9d2_9d25a375dbf7 --> f16d8c76_2866_6150_bd14_0347b59abfe9
  01c5fbc2_ddb6_180b_d98d_2ff488540314["assets"]
  50d6c5a0_3bd9_55ef_d9d2_9d25a375dbf7 --> 01c5fbc2_ddb6_180b_d98d_2ff488540314
  f8ab2297_5406_4f9e_e15c_c4eb026871f9["utils"]
  50d6c5a0_3bd9_55ef_d9d2_9d25a375dbf7 --> f8ab2297_5406_4f9e_e15c_c4eb026871f9
  style 50d6c5a0_3bd9_55ef_d9d2_9d25a375dbf7 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { ExternalImageService } from 'astro';
import { baseService } from 'astro/assets';
import { isESMImportedImage } from 'astro/assets/utils';
import { sharedValidateOptions, type VercelImageConfig } from './shared.js';

const service: ExternalImageService = {
	...baseService,
	validateOptions: (options, serviceOptions) =>
		sharedValidateOptions(options, serviceOptions.service.config, 'production'),
	getHTMLAttributes(options) {
		const { inputtedWidth, ...props } = options;

		// If `validateOptions` returned a different width than the one of the image, use it for attributes
		if (inputtedWidth) {
			props.width = inputtedWidth;
		}

		let targetWidth = props.width;
		let targetHeight = props.height;
		if (isESMImportedImage(props.src)) {
			const aspectRatio = props.src.width / props.src.height;
			if (targetHeight && !targetWidth) {
				// If we have a height but no width, use height to calculate the width
				targetWidth = Math.round(targetHeight * aspectRatio);
			} else if (targetWidth && !targetHeight) {
				// If we have a width but no height, use width to calculate the height
				targetHeight = Math.round(targetWidth / aspectRatio);
			} else if (!targetWidth && !targetHeight) {
				// If we have neither width or height, use the original image's dimensions
				targetWidth = props.src.width;
				targetHeight = props.src.height;
			}
		}

		const { src, width, height, format, quality, densities, widths, formats, ...attributes } =
			options;

		return {
			...attributes,
			width: targetWidth,
			height: targetHeight,
			loading: attributes.loading ?? 'lazy',
			decoding: attributes.decoding ?? 'async',
		};
	},
	getURL(options) {
		// For SVG files, return the original source path
		if (isESMImportedImage(options.src) && options.src.format === 'svg') {
			return options.src.src;
		}

		// For non-SVG files, continue with the Vercel image processing
		const fileSrc = isESMImportedImage(options.src)
			? removeLeadingForwardSlash(options.src.src)
			: options.src;

		const searchParams = new URLSearchParams();
		searchParams.append('url', fileSrc);

		options.width && searchParams.append('w', options.width.toString());
// ... (121 more lines)

Domain

Subdomains

Dependencies

  • ./image/shared.js
  • assets
  • astro
  • utils

Frequently Asked Questions

What does build-service.ts do?
build-service.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, CoreMiddleware subdomain.
What functions are defined in build-service.ts?
build-service.ts defines 5 function(s): removeLeadingForwardSlash, service.getHTMLAttributes, service.getSrcSet, service.getURL, service.validateOptions.
What does build-service.ts depend on?
build-service.ts imports 4 module(s): ./image/shared.js, assets, astro, utils.
Where is build-service.ts in the architecture?
build-service.ts is located at packages/integrations/vercel/src/image/build-service.ts (domain: CoreAstro, subdomain: CoreMiddleware, directory: packages/integrations/vercel/src/image).

Analyze Your Own Codebase

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

Try Supermodel Free