Home / File/ index.ts — astro Source File

index.ts — astro Source File

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

File typescript CoreAstro RenderingEngine 4 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  3add2cbd_84c6_ab3f_c916_c599a116c63a["index.ts"]
  e9b74c5a_8d34_34a7_e196_5e41b87214aa["../types/astro.js"]
  3add2cbd_84c6_ab3f_c916_c599a116c63a --> e9b74c5a_8d34_34a7_e196_5e41b87214aa
  4c453c0b_17bb_ebc3_f7de_e2a632e42c1e["../types/public/integrations.js"]
  3add2cbd_84c6_ab3f_c916_c599a116c63a --> 4c453c0b_17bb_ebc3_f7de_e2a632e42c1e
  7216d952_4e4a_2d18_a85b_74b4ace79e2b["../core/constants.js"]
  3add2cbd_84c6_ab3f_c916_c599a116c63a --> 7216d952_4e4a_2d18_a85b_74b4ace79e2b
  263e522e_1aa5_ebc3_e7d6_45ebc51671f7["vite"]
  3add2cbd_84c6_ab3f_c916_c599a116c63a --> 263e522e_1aa5_ebc3_e7d6_45ebc51671f7
  style 3add2cbd_84c6_ab3f_c916_c599a116c63a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { Plugin as VitePlugin } from 'vite';
import type { AstroSettings } from '../types/astro.js';
import type { InjectedScriptStage } from '../types/public/integrations.js';
import { ASTRO_VITE_ENVIRONMENT_NAMES } from '../core/constants.js';

// NOTE: We can't use the virtual "\0" ID convention because we need to
// inject these as ESM imports into actual code, where they would not
// resolve correctly.
const SCRIPT_ID_PREFIX = `astro:scripts/`;
export const BEFORE_HYDRATION_SCRIPT_ID = `${SCRIPT_ID_PREFIX}${
	'before-hydration' as InjectedScriptStage
}.js`;
export const PAGE_SCRIPT_ID = `${SCRIPT_ID_PREFIX}${'page' as InjectedScriptStage}.js`;
export const PAGE_SSR_SCRIPT_ID = `${SCRIPT_ID_PREFIX}${'page-ssr' as InjectedScriptStage}.js`;

export default function astroScriptsPlugin({ settings }: { settings: AstroSettings }): VitePlugin {
	return {
		name: 'astro:scripts',

		resolveId: {
			filter: {
				id: new RegExp(`^${SCRIPT_ID_PREFIX}`),
			},
			handler(id) {
				return id;
			},
		},

		load: {
			filter: {
				id: new RegExp(`^(${BEFORE_HYDRATION_SCRIPT_ID}|${PAGE_SCRIPT_ID}|${PAGE_SSR_SCRIPT_ID})$`),
			},
			handler(id) {
				if (id === BEFORE_HYDRATION_SCRIPT_ID) {
					return {
						code: settings.scripts
							.filter((s) => s.stage === 'before-hydration')
							.map((s) => s.content)
							.join('\n'),
					};
				}
				if (id === PAGE_SCRIPT_ID) {
					return {
						code: settings.scripts
							.filter((s) => s.stage === 'page')
							.map((s) => s.content)
							.join('\n'),
					};
				}
				if (id === PAGE_SSR_SCRIPT_ID) {
					return {
						code: settings.scripts
							.filter((s) => s.stage === 'page-ssr')
							.map((s) => s.content)
							.join('\n'),
					};
				}
			},
		},
		buildStart() {
			const hasHydrationScripts = settings.scripts.some((s) => s.stage === 'before-hydration');
			if (
				hasHydrationScripts &&
				(this.environment.name === ASTRO_VITE_ENVIRONMENT_NAMES.prerender ||
					this.environment.name === ASTRO_VITE_ENVIRONMENT_NAMES.ssr)
			) {
				this.emitFile({
					type: 'chunk',
					id: BEFORE_HYDRATION_SCRIPT_ID,
					name: BEFORE_HYDRATION_SCRIPT_ID,
				});
			}
		},
	};
}

Domain

Subdomains

Dependencies

  • ../core/constants.js
  • ../types/astro.js
  • ../types/public/integrations.js
  • vite

Frequently Asked Questions

What does index.ts do?
index.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 index.ts?
index.ts defines 1 function(s): astroScriptsPlugin.
What does index.ts depend on?
index.ts imports 4 module(s): ../core/constants.js, ../types/astro.js, ../types/public/integrations.js, vite.
Where is index.ts in the architecture?
index.ts is located at packages/astro/src/vite-plugin-scripts/index.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/vite-plugin-scripts).

Analyze Your Own Codebase

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

Try Supermodel Free