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 CoreMiddleware 4 imports 3 functions

Entity Profile

Dependency Diagram

graph LR
  887e50cb_5b90_14c0_7284_65712ec294a4["index.ts"]
  d9a92db9_c95e_9165_13ac_24b3d859d946["node:url"]
  887e50cb_5b90_14c0_7284_65712ec294a4 --> d9a92db9_c95e_9165_13ac_24b3d859d946
  32118bcc_50e8_f1c6_07c0_43710eae3090["preset-vite"]
  887e50cb_5b90_14c0_7284_65712ec294a4 --> 32118bcc_50e8_f1c6_07c0_43710eae3090
  f16d8c76_2866_6150_bd14_0347b59abfe9["astro"]
  887e50cb_5b90_14c0_7284_65712ec294a4 --> f16d8c76_2866_6150_bd14_0347b59abfe9
  263e522e_1aa5_ebc3_e7d6_45ebc51671f7["vite"]
  887e50cb_5b90_14c0_7284_65712ec294a4 --> 263e522e_1aa5_ebc3_e7d6_45ebc51671f7
  style 887e50cb_5b90_14c0_7284_65712ec294a4 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { fileURLToPath } from 'node:url';
import { preact, type PreactPluginOptions as VitePreactPluginOptions } from '@preact/preset-vite';
import type { AstroIntegration, AstroRenderer, ViteUserConfig } from 'astro';
import type { EnvironmentOptions, Plugin } from 'vite';

const babelCwd = new URL('../', import.meta.url);

function getRenderer(development: boolean): AstroRenderer {
	return {
		name: '@astrojs/preact',
		clientEntrypoint: development ? '@astrojs/preact/client-dev.js' : '@astrojs/preact/client.js',
		serverEntrypoint: '@astrojs/preact/server.js',
	};
}

export const getContainerRenderer = (): AstroRenderer => getRenderer(false);

export interface Options extends Pick<VitePreactPluginOptions, 'include' | 'exclude'> {
	compat?: boolean;
	devtools?: boolean;
}

export default function ({ include, exclude, compat, devtools }: Options = {}): AstroIntegration {
	return {
		name: '@astrojs/preact',
		hooks: {
			'astro:config:setup': ({ addRenderer, updateConfig, command, injectScript }) => {
				const preactPlugin = preact({
					reactAliasesEnabled: compat ?? false,
					include,
					exclude,
					babel: {
						cwd: fileURLToPath(babelCwd),
					},
				});

				const viteConfig: ViteUserConfig = {
					optimizeDeps: {
						// Ideally would be environment config, but
						// putting it there does not result in it being optimized
						include: ['@astrojs/preact/server.js'],
					},
				};

				viteConfig.plugins = [preactPlugin, configEnvironmentPlugin(compat)];

				addRenderer(getRenderer(command === 'dev'));
				updateConfig({
					vite: viteConfig,
				});

				if (command === 'dev' && devtools) {
					injectScript('page', 'import "preact/debug";');
				}
			},
			'astro:config:done': ({ logger, config }) => {
				const knownJsxRenderers = ['@astrojs/react', '@astrojs/preact', '@astrojs/solid-js'];
				const enabledKnownJsxRenderers = config.integrations.filter((renderer) =>
					knownJsxRenderers.includes(renderer.name),
				);

				if (enabledKnownJsxRenderers.length > 1 && !include && !exclude) {
					logger.warn(
						'More than one JSX renderer is enabled. This will lead to unexpected behavior unless you set the `include` or `exclude` option. See https://docs.astro.build/en/guides/integrations-guide/preact/#combining-multiple-jsx-frameworks for more information.',
					);
				}
			},
		},
	};
}

function configEnvironmentPlugin(compat: boolean | undefined): Plugin {
	return {
		name: '@astrojs/preact:environment',
		configEnvironment(environmentName, options) {
			const environmentOptions: EnvironmentOptions = {
				optimizeDeps: {},
				resolve: {},
			};

			if (environmentName === 'client') {
				environmentOptions.optimizeDeps!.include = [
					'@astrojs/preact/client.js',
					'preact',
					'preact/jsx-runtime',
				];
			}

			if (compat) {
				environmentOptions.resolve = {
					dedupe: ['preact/compat', 'preact'],
				};
				if (environmentName === 'client') {
					environmentOptions.optimizeDeps!.include!.push(
						'preact/compat',
						'preact/test-utils',
						'preact/compat/jsx-runtime',
					);
				}

				if (
					!options.resolve?.noExternal &&
					(environmentName === 'ssr' || environmentName === 'prerender')
				) {
					// noExternal React entrypoints to be bundled, resolved, and aliased by Vite
					environmentOptions.resolve!.noExternal = [
						'react',
						'react-dom',
						'react-dom/test-utils',
						'react/jsx-runtime',
					];
				}
			}

			return environmentOptions;
		},
	};
}

Domain

Subdomains

Types

Dependencies

  • astro
  • node:url
  • preset-vite
  • 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, CoreMiddleware subdomain.
What functions are defined in index.ts?
index.ts defines 3 function(s): configEnvironmentPlugin, getContainerRenderer, getRenderer.
What does index.ts depend on?
index.ts imports 4 module(s): astro, node:url, preset-vite, vite.
Where is index.ts in the architecture?
index.ts is located at packages/integrations/preact/src/index.ts (domain: CoreAstro, subdomain: CoreMiddleware, directory: packages/integrations/preact/src).

Analyze Your Own Codebase

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

Try Supermodel Free