Home / File/ index.ts — astro Source File

index.ts — astro Source File

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

File typescript CoreAstro RenderingEngine 5 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  637cf538_f5d4_a8da_0224_fde311a92d23["index.ts"]
  7e4494c0_5563_4329_1bff_a84be66e1bc2["../core/path.js"]
  637cf538_f5d4_a8da_0224_fde311a92d23 --> 7e4494c0_5563_4329_1bff_a84be66e1bc2
  87530382_6d99_2339_182d_074e3de33bc8["../vite-plugin-utils/index.js"]
  637cf538_f5d4_a8da_0224_fde311a92d23 --> 87530382_6d99_2339_182d_074e3de33bc8
  e16a223b_37f3_6b25_1ee1_2b7bcb9d9415["node:fs"]
  637cf538_f5d4_a8da_0224_fde311a92d23 --> e16a223b_37f3_6b25_1ee1_2b7bcb9d9415
  c52a5f83_66e3_37d7_9ebb_767f7129bc62["node:path"]
  637cf538_f5d4_a8da_0224_fde311a92d23 --> c52a5f83_66e3_37d7_9ebb_767f7129bc62
  263e522e_1aa5_ebc3_e7d6_45ebc51671f7["vite"]
  637cf538_f5d4_a8da_0224_fde311a92d23 --> 263e522e_1aa5_ebc3_e7d6_45ebc51671f7
  style 637cf538_f5d4_a8da_0224_fde311a92d23 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import nodeFs from 'node:fs';
import npath from 'node:path';
import type * as vite from 'vite';
import { slash } from '../core/path.js';
import { cleanUrl } from '../vite-plugin-utils/index.js';

type NodeFileSystemModule = typeof nodeFs;

interface LoadFallbackPluginParams {
	fs?: NodeFileSystemModule;
	root: URL;
}

const FALLBACK_FLAG = 'astroFallbackFlag';

export default function loadFallbackPlugin({
	fs,
	root,
}: LoadFallbackPluginParams): vite.Plugin[] | false {
	// Only add this plugin if a custom fs implementation is provided.
	// Also check for `fs.default` because `import * as fs from 'node:fs'` will
	// export as so, which only it's `.default` would === `nodeFs`.
	// @ts-expect-error check default
	if (!fs || fs === nodeFs || fs.default === nodeFs) {
		return false;
	}

	const tryLoadModule = async (id: string) => {
		try {
			// await is necessary for the catch
			return await fs.promises.readFile(cleanUrl(id), 'utf-8');
		} catch {
			try {
				return await fs.promises.readFile(id, 'utf-8');
			} catch {
				try {
					const fullpath = new URL('.' + id, root);
					return await fs.promises.readFile(fullpath, 'utf-8');
				} catch {
					// Let fall through to the next
				}
			}
		}
	};

	return [
		{
			name: 'astro:load-fallback',
			enforce: 'post',
			async resolveId(id, parent) {
				// See if this can be loaded from our fs
				if (parent) {
					const candidateId = npath.posix.join(npath.posix.dirname(slash(parent)), id);
					try {
						// Check to see if this file exists and is not a directory.
						const stats = await fs.promises.stat(candidateId);
						if (!stats.isDirectory()) {
							const params = new URLSearchParams(FALLBACK_FLAG);
							return `${candidateId}?${params.toString()}`;
						}
					} catch {}
				}
			},
			load: {
				filter: {
					id: new RegExp(`(?:\\?|&)${FALLBACK_FLAG}(?:&|=|$)`),
				},
				async handler(id) {
					const code = await tryLoadModule(id.slice(0, -(1 + FALLBACK_FLAG.length)));
					if (code) {
						return { code };
					}
				},
			},
		},
		{
			name: 'astro:load-fallback-hmr',
			enforce: 'pre',
			handleHotUpdate(context) {
				// Wrap context.read so it checks our filesystem first.
				const read = context.read;
				context.read = async () => {
					const source = await tryLoadModule(context.file);
					if (source) return source;
					return read.call(context);
				};
			},
		},
	];
}

Domain

Subdomains

Dependencies

  • ../core/path.js
  • ../vite-plugin-utils/index.js
  • node:fs
  • node:path
  • 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): loadFallbackPlugin.
What does index.ts depend on?
index.ts imports 5 module(s): ../core/path.js, ../vite-plugin-utils/index.js, node:fs, node:path, vite.
Where is index.ts in the architecture?
index.ts is located at packages/astro/src/vite-plugin-load-fallback/index.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/vite-plugin-load-fallback).

Analyze Your Own Codebase

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

Try Supermodel Free