Home / File/ viteUtils.ts — astro Source File

viteUtils.ts — astro Source File

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

File typescript CoreAstro RenderingEngine 5 imports 4 functions

Entity Profile

Dependency Diagram

graph LR
  b7a40933_4ff9_20ad_c87e_be8112bdd644["viteUtils.ts"]
  7e4494c0_5563_4329_1bff_a84be66e1bc2["../core/path.js"]
  b7a40933_4ff9_20ad_c87e_be8112bdd644 --> 7e4494c0_5563_4329_1bff_a84be66e1bc2
  32f8c7d4_d66e_e0cf_b019_46ec3f2fea31["../core/module-loader/index.js"]
  b7a40933_4ff9_20ad_c87e_be8112bdd644 --> 32f8c7d4_d66e_e0cf_b019_46ec3f2fea31
  f68003f1_292f_ca44_03ce_21af87a33c7b["../core/util.js"]
  b7a40933_4ff9_20ad_c87e_be8112bdd644 --> f68003f1_292f_ca44_03ce_21af87a33c7b
  c52a5f83_66e3_37d7_9ebb_767f7129bc62["node:path"]
  b7a40933_4ff9_20ad_c87e_be8112bdd644 --> c52a5f83_66e3_37d7_9ebb_767f7129bc62
  d9a92db9_c95e_9165_13ac_24b3d859d946["node:url"]
  b7a40933_4ff9_20ad_c87e_be8112bdd644 --> d9a92db9_c95e_9165_13ac_24b3d859d946
  style b7a40933_4ff9_20ad_c87e_be8112bdd644 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { prependForwardSlash, slash } from '../core/path.js';
import type { ModuleLoader } from './module-loader/index.js';
import { resolveJsToTs, unwrapId, VALID_ID_PREFIX, viteID } from './util.js';

const isWindows = typeof process !== 'undefined' && process.platform === 'win32';

/**
 * Re-implementation of Vite's normalizePath that can be used without Vite
 */
export function normalizePath(id: string) {
	return path.posix.normalize(isWindows ? slash(id) : id);
}

/**
 * Resolve the hydration paths so that it can be imported in the client
 */
export function resolvePath(specifier: string, importer: string) {
	if (specifier.startsWith('.')) {
		const absoluteSpecifier = path.resolve(path.dirname(importer), specifier);
		return resolveJsToTs(normalizePath(absoluteSpecifier));
	} else {
		return specifier;
	}
}

export function rootRelativePath(
	root: URL,
	idOrUrl: URL | string,
	shouldPrependForwardSlash = true,
) {
	let id: string;
	if (typeof idOrUrl !== 'string') {
		id = unwrapId(viteID(idOrUrl));
	} else {
		id = idOrUrl;
	}
	const normalizedRoot = normalizePath(fileURLToPath(root));
	if (id.startsWith(normalizedRoot)) {
		id = id.slice(normalizedRoot.length);
	}
	return shouldPrependForwardSlash ? prependForwardSlash(id) : id;
}

/**
 * Simulate Vite's resolve and import analysis so we can import the id as an URL
 * through a script tag or a dynamic import as-is.
 */
// NOTE: `/@id/` should only be used when the id is fully resolved
export async function resolveIdToUrl(loader: ModuleLoader, id: string, root?: URL) {
	let resultId = await loader.resolveId(id, undefined);
	// Try resolve jsx to tsx
	if (!resultId && id.endsWith('.jsx')) {
		resultId = await loader.resolveId(id.slice(0, -4), undefined);
	}
	if (!resultId) {
		return VALID_ID_PREFIX + id;
	}
	if (path.isAbsolute(resultId)) {
		const normalizedRoot = root && normalizePath(fileURLToPath(root));
		// Convert to root-relative path if path is inside root
		if (normalizedRoot && resultId.startsWith(normalizedRoot)) {
			return resultId.slice(normalizedRoot.length - 1);
		} else {
			return '/@fs' + prependForwardSlash(resultId);
		}
	}
	return VALID_ID_PREFIX + resultId;
}

// https://github.com/vitejs/vite/blob/2f9428d1ffd988e30cb253d5bb84844fb1654e86/packages/vite/src/node/constants.ts#L108
// Used by isCSSRequest() under the hood
export const CSS_LANGS_RE = /\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)(?:$|\?)/;

Domain

Subdomains

Dependencies

  • ../core/module-loader/index.js
  • ../core/path.js
  • ../core/util.js
  • node:path
  • node:url

Frequently Asked Questions

What does viteUtils.ts do?
viteUtils.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 viteUtils.ts?
viteUtils.ts defines 4 function(s): normalizePath, resolveIdToUrl, resolvePath, rootRelativePath.
What does viteUtils.ts depend on?
viteUtils.ts imports 5 module(s): ../core/module-loader/index.js, ../core/path.js, ../core/util.js, node:path, node:url.
Where is viteUtils.ts in the architecture?
viteUtils.ts is located at packages/astro/src/core/viteUtils.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/core).

Analyze Your Own Codebase

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

Try Supermodel Free