Home / File/ style.ts — astro Source File

style.ts — astro Source File

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

File typescript CoreAstro RenderingEngine 7 imports 3 functions

Entity Profile

Dependency Diagram

graph LR
  b49b1fb8_9e6f_4c97_cf66_98d4fd2b7754["style.ts"]
  c32d12e2_d85e_28c0_eea7_9b29629857e0["../types/public/config.js"]
  b49b1fb8_9e6f_4c97_cf66_98d4fd2b7754 --> c32d12e2_d85e_28c0_eea7_9b29629857e0
  ef8a1e3f_e350_75a6_b92d_62a8566d8db9["../core/errors/index.js"]
  b49b1fb8_9e6f_4c97_cf66_98d4fd2b7754 --> ef8a1e3f_e350_75a6_b92d_62a8566d8db9
  a370a45c_02f1_30de_445d_47ee08d095a2["../core/viteUtils.js"]
  b49b1fb8_9e6f_4c97_cf66_98d4fd2b7754 --> a370a45c_02f1_30de_445d_47ee08d095a2
  dec1e816_8ffc_7c1a_0ff1_5897dbd3dc9b["../core/compile/types.js"]
  b49b1fb8_9e6f_4c97_cf66_98d4fd2b7754 --> dec1e816_8ffc_7c1a_0ff1_5897dbd3dc9b
  e16a223b_37f3_6b25_1ee1_2b7bcb9d9415["node:fs"]
  b49b1fb8_9e6f_4c97_cf66_98d4fd2b7754 --> e16a223b_37f3_6b25_1ee1_2b7bcb9d9415
  40286c5f_532d_1bd5_70e5_3dbec8398413["compiler"]
  b49b1fb8_9e6f_4c97_cf66_98d4fd2b7754 --> 40286c5f_532d_1bd5_70e5_3dbec8398413
  263e522e_1aa5_ebc3_e7d6_45ebc51671f7["vite"]
  b49b1fb8_9e6f_4c97_cf66_98d4fd2b7754 --> 263e522e_1aa5_ebc3_e7d6_45ebc51671f7
  style b49b1fb8_9e6f_4c97_cf66_98d4fd2b7754 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import fs from 'node:fs';
import type { TransformOptions } from '@astrojs/compiler';
import { preprocessCSS, type ResolvedConfig } from 'vite';
import type { AstroConfig } from '../../types/public/config.js';
import { AstroErrorData, CSSError, positionAt } from '../errors/index.js';
import { normalizePath } from '../viteUtils.js';
import type { CompileCssResult } from './types.js';

export type PartialCompileCssResult = Pick<CompileCssResult, 'isGlobal' | 'dependencies'>;

/**
 * Rewrites absolute URLs in CSS to include the base path.
 *
 * Vite's `preprocessCSS` function explicitly does NOT resolve URLs in `url()` or `image-set()`
 * (https://vite.dev/guide/api-javascript.html#preprocesscss). During build, Vite's CSS plugin handles URL rewriting through its
 * full transform pipeline, but during dev, Astro calls `preprocessCSS` directly through the
 * compiler, bypassing that pipeline.
 *
 * Only absolute URLs starting with `/` (e.g., `/fonts/font.woff`, `/images/bg.png`) are rewritten
 *
 * Uses Vite's cssUrlRE regex pattern for reliable URL matching.
 * See: https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/css.ts
 *
 * @param css - The CSS string to process
 * @param base - The base path to prepend (e.g., `/my-base`)
 * @returns The CSS with rewritten URLs
 */
function rewriteCssUrls(css: string, base: string): string {
	// Only rewrite if base is not the default '/'
	if (!base || base === '/') {
		return css;
	}

	// Normalize base path (remove trailing slash for consistent joining)
	const normalizedBase = base.endsWith('/') ? base.slice(0, -1) : base;

	// Safety check: base should start with '/' (already normalized by Astro config)
	if (!normalizedBase.startsWith('/')) {
		return css;
	}

	// Vite's production-tested regex for matching url() in CSS
	// Matches url(...) while handling quotes, unquoted URLs, and edge cases
	// Excludes @import statements via negative lookbehind
	// Matches Vite's cssUrlRE pattern exactly - capturing groups preserved for compatibility
	const cssUrlRE =
		// eslint-disable-next-line regexp/no-unused-capturing-group
		/(?<!@import\s+)(?<=^|[^\w\-\u0080-\uffff])url\((\s*('[^']+'|"[^"]+")\s*|(?:\\.|[^'")\\])+)\)/g;

	return css.replace(cssUrlRE, (match, rawUrl: string) => {
		// Extract URL value, removing quotes if present
		let url = rawUrl.trim();
		let quote = '';

		// Check if URL is quoted (single or double)
		if ((url.startsWith("'") && url.endsWith("'")) || (url.startsWith('"') && url.endsWith('"'))) {
			quote = url[0];
			url = url.slice(1, -1);
		}

// ... (127 more lines)

Domain

Subdomains

Dependencies

  • ../core/compile/types.js
  • ../core/errors/index.js
  • ../core/viteUtils.js
  • ../types/public/config.js
  • compiler
  • node:fs
  • vite

Frequently Asked Questions

What does style.ts do?
style.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 style.ts?
style.ts defines 3 function(s): createStylePreprocessor, enhanceCSSError, rewriteCssUrls.
What does style.ts depend on?
style.ts imports 7 module(s): ../core/compile/types.js, ../core/errors/index.js, ../core/viteUtils.js, ../types/public/config.js, compiler, node:fs, vite.
Where is style.ts in the architecture?
style.ts is located at packages/astro/src/core/compile/style.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/core/compile).

Analyze Your Own Codebase

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

Try Supermodel Free