Home / File/ resolveImports.ts — astro Source File

resolveImports.ts — astro Source File

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

File typescript CoreAstro RenderingEngine 4 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  f60c7a86_1224_0eea_f0b7_7bdfeeefce2f["resolveImports.ts"]
  eb7ca709_080c_a438_b9d7_f1238835779d["../content/consts.js"]
  f60c7a86_1224_0eea_f0b7_7bdfeeefce2f --> eb7ca709_080c_a438_b9d7_f1238835779d
  df93c329_2f79_6708_fa84_5c5c757a6c19["../runtime/server/shorthash.js"]
  f60c7a86_1224_0eea_f0b7_7bdfeeefce2f --> df93c329_2f79_6708_fa84_5c5c757a6c19
  a19b6b68_b1f2_2d3e_2481_283031d370ff["./consts.js"]
  f60c7a86_1224_0eea_f0b7_7bdfeeefce2f --> a19b6b68_b1f2_2d3e_2481_283031d370ff
  e4df8f29_fb2f_3d70_a962_fdf6a3670b22["path"]
  f60c7a86_1224_0eea_f0b7_7bdfeeefce2f --> e4df8f29_fb2f_3d70_a962_fdf6a3670b22
  style f60c7a86_1224_0eea_f0b7_7bdfeeefce2f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { isRemotePath, removeBase } from '@astrojs/internal-helpers/path';
import { CONTENT_IMAGE_FLAG, IMAGE_IMPORT_PREFIX } from '../../content/consts.js';
import { shorthash } from '../../runtime/server/shorthash.js';
import { VALID_INPUT_FORMATS } from '../consts.js';

/**
 * Resolves an image src from a content file (such as markdown) to a module ID or import that can be resolved by Vite.
 *
 * @param imageSrc The src attribute of an image tag
 * @param filePath The path to the file that contains the imagem relative to the site root
 * @returns A module id of the image that can be rsolved by Vite, or undefined if it is not a local image
 */
export function imageSrcToImportId(imageSrc: string, filePath?: string): string | undefined {
	// If the import is coming from the data store it will have a special prefix to identify it
	// as an image import. We remove this prefix so that we can resolve the image correctly.
	imageSrc = removeBase(imageSrc, IMAGE_IMPORT_PREFIX);

	// We only care about local imports
	if (isRemotePath(imageSrc)) {
		return;
	}
	// We only care about images
	const ext = imageSrc.split('.').at(-1)?.toLowerCase() as
		| (typeof VALID_INPUT_FORMATS)[number]
		| undefined;
	if (!ext || !VALID_INPUT_FORMATS.includes(ext)) {
		return;
	}

	// The import paths are relative to the content (md) file, but when it's actually resolved it will
	// be in a single assets file, so relative paths will no longer work. To deal with this we use
	// a query parameter to store the original path to the file and append a query param flag.
	// This allows our Vite plugin to intercept the import and resolve the path relative to the
	// importer and get the correct full path for the imported image.

	const params = new URLSearchParams(CONTENT_IMAGE_FLAG);
	if (filePath) {
		params.set('importer', filePath);
	}
	return `${imageSrc}?${params.toString()}`;
}

export const importIdToSymbolName = (importId: string) =>
	`__ASTRO_IMAGE_IMPORT_${shorthash(importId)}`;

Domain

Subdomains

Dependencies

  • ../content/consts.js
  • ../runtime/server/shorthash.js
  • ./consts.js
  • path

Frequently Asked Questions

What does resolveImports.ts do?
resolveImports.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 resolveImports.ts?
resolveImports.ts defines 2 function(s): imageSrcToImportId, importIdToSymbolName.
What does resolveImports.ts depend on?
resolveImports.ts imports 4 module(s): ../content/consts.js, ../runtime/server/shorthash.js, ./consts.js, path.
Where is resolveImports.ts in the architecture?
resolveImports.ts is located at packages/astro/src/assets/utils/resolveImports.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