Home / Function/ loadFallbackPlugin() — astro Function Reference

loadFallbackPlugin() — astro Function Reference

Architecture documentation for the loadFallbackPlugin() function in index.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  f42a566d_b8ef_464a_2633_775943a00cff["loadFallbackPlugin()"]
  637cf538_f5d4_a8da_0224_fde311a92d23["index.ts"]
  f42a566d_b8ef_464a_2633_775943a00cff -->|defined in| 637cf538_f5d4_a8da_0224_fde311a92d23
  style f42a566d_b8ef_464a_2633_775943a00cff fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/vite-plugin-load-fallback/index.ts lines 16–90

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

Frequently Asked Questions

What does loadFallbackPlugin() do?
loadFallbackPlugin() is a function in the astro codebase, defined in packages/astro/src/vite-plugin-load-fallback/index.ts.
Where is loadFallbackPlugin() defined?
loadFallbackPlugin() is defined in packages/astro/src/vite-plugin-load-fallback/index.ts at line 16.

Analyze Your Own Codebase

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

Try Supermodel Free