Home / Function/ vitePluginServerIslands() — astro Function Reference

vitePluginServerIslands() — astro Function Reference

Architecture documentation for the vitePluginServerIslands() function in vite-plugin-server-islands.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  55b82f7a_6c17_8d2e_68ff_2c2dec44a4ef["vitePluginServerIslands()"]
  1a4e9367_8808_ab57_d785_0978b31b0a7b["vite-plugin-server-islands.ts"]
  55b82f7a_6c17_8d2e_68ff_2c2dec44a4ef -->|defined in| 1a4e9367_8808_ab57_d785_0978b31b0a7b
  style 55b82f7a_6c17_8d2e_68ff_2c2dec44a4ef fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/core/server-islands/vite-plugin-server-islands.ts lines 14–175

export function vitePluginServerIslands({ settings }: AstroPluginOptions): VitePlugin {
	let command: ConfigEnv['command'] = 'serve';
	let ssrEnvironment: DevEnvironment | null = null;
	const referenceIdMap = new Map<string, string>();
	const serverIslandMap = new Map();
	const serverIslandNameMap = new Map();
	return {
		name: 'astro:server-islands',
		enforce: 'post',
		config(_config, { command: _command }) {
			command = _command;
		},
		configureServer(server) {
			ssrEnvironment = server.environments[ASTRO_VITE_ENVIRONMENT_NAMES.ssr];
		},
		resolveId: {
			filter: {
				id: new RegExp(`^${SERVER_ISLAND_MANIFEST}$`),
			},
			handler() {
				return RESOLVED_SERVER_ISLAND_MANIFEST;
			},
		},
		load: {
			filter: {
				id: new RegExp(`^${RESOLVED_SERVER_ISLAND_MANIFEST}$`),
			},
			handler() {
				return {
					code: `export const serverIslandMap = ${serverIslandPlaceholderMap};\n\nexport const serverIslandNameMap = ${serverIslandPlaceholderNameMap};`,
				};
			},
		},

		transform: {
			filter: {
				id: {
					include: [
						// Allows server islands in astro and mdx files
						/\.(astro|mdx)$/,
						new RegExp(`^${RESOLVED_SERVER_ISLAND_MANIFEST}$`),
					],
				},
			},
			async handler(_code, id) {
				const info = this.getModuleInfo(id);

				const astro = info ? (info.meta.astro as AstroPluginMetadata['astro']) : undefined;

				if (astro) {
					for (const comp of astro.serverComponents) {
						if (!serverIslandNameMap.has(comp.resolvedPath)) {
							if (!settings.adapter) {
								throw new AstroError(AstroErrorData.NoAdapterInstalledServerIslands);
							}
							let name = comp.localName;
							let idx = 1;

							while (true) {
								// Name not taken, let's use it.
								if (!serverIslandMap.has(name)) {
									break;
								}
								// Increment a number onto the name: Avatar -> Avatar1
								name += idx++;
							}

							// Append the name map, for prod
							serverIslandNameMap.set(comp.resolvedPath, name);
							serverIslandMap.set(name, comp.resolvedPath);

							// Build mode
							if (command === 'build') {
								let referenceId = this.emitFile({
									type: 'chunk',
									id: comp.specifier,
									importer: id,
									name: comp.localName,
								});
								referenceIdMap.set(comp.resolvedPath, referenceId);
							}

Domain

Subdomains

Frequently Asked Questions

What does vitePluginServerIslands() do?
vitePluginServerIslands() is a function in the astro codebase, defined in packages/astro/src/core/server-islands/vite-plugin-server-islands.ts.
Where is vitePluginServerIslands() defined?
vitePluginServerIslands() is defined in packages/astro/src/core/server-islands/vite-plugin-server-islands.ts at line 14.

Analyze Your Own Codebase

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

Try Supermodel Free