Home / File/ client.svelte.ts — astro Source File

client.svelte.ts — astro Source File

Architecture documentation for client.svelte.ts, a typescript file in the astro codebase. 1 imports, 0 dependents.

File typescript CoreAstro CoreMiddleware 1 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  52a45147_83e6_043c_25ea_3c8f58cef854["client.svelte.ts"]
  0a9ef33f_55e1_95ff_380a_be44d01c743f["svelte"]
  52a45147_83e6_043c_25ea_3c8f58cef854 --> 0a9ef33f_55e1_95ff_380a_be44d01c743f
  style 52a45147_83e6_043c_25ea_3c8f58cef854 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { createRawSnippet, hydrate, mount, unmount } from 'svelte';

const existingApplications = new WeakMap<HTMLElement, ReturnType<typeof createComponent>>();

export default (element: HTMLElement) => {
	return async (
		Component: any,
		props: Record<string, any>,
		slotted: Record<string, any>,
		{ client }: Record<string, string>,
	) => {
		if (!element.hasAttribute('ssr')) return;

		let children = undefined;
		let _$$slots: Record<string, any> | undefined = undefined;
		let renderFns: Record<string, any> = {};

		for (const [key, value] of Object.entries(slotted)) {
			// Legacy slot support
			_$$slots ??= {};
			if (key === 'default') {
				_$$slots.default = true;
				children = createRawSnippet(() => ({
					render: () => `<astro-slot>${value}</astro-slot>`,
				}));
			} else {
				_$$slots[key] = createRawSnippet(() => ({
					render: () => `<astro-slot name="${key}">${value}</astro-slot>`,
				}));
			}
			// @render support for Svelte ^5.0
			if (key === 'default') {
				renderFns.children = createRawSnippet(() => ({
					render: () => `<astro-slot>${value}</astro-slot>`,
				}));
			} else {
				renderFns[key] = createRawSnippet(() => ({
					render: () => `<astro-slot name="${key}">${value}</astro-slot>`,
				}));
			}
		}

		const resolvedProps = {
			...props,
			children,
			$$slots: _$$slots,
			...renderFns,
		};
		if (existingApplications.has(element)) {
			existingApplications.get(element)!.setProps(resolvedProps);
		} else {
			const component = createComponent(Component, element, resolvedProps, client !== 'only');
			existingApplications.set(element, component);
			element.addEventListener('astro:unmount', () => component.destroy(), { once: true });
		}
	};
};

function createComponent(
	Component: any,
	target: HTMLElement,
	props: Record<string, any>,
	shouldHydrate: boolean,
) {
	let propsState = $state(props);
	const bootstrap = shouldHydrate ? hydrate : mount;
	if (!shouldHydrate) {
		target.innerHTML = '';
	}
	const component = bootstrap(Component, { target, props: propsState });
	return {
		setProps(newProps: Record<string, any>) {
			Object.assign(propsState, newProps);
			// Remove props in `propsState` but not in `newProps`
			for (const key in propsState) {
				if (!(key in newProps)) {
					delete propsState[key];
				}
			}
		},
		destroy() {
			unmount(component);
		},
	};
}

Domain

Subdomains

Functions

Dependencies

  • svelte

Frequently Asked Questions

What does client.svelte.ts do?
client.svelte.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 client.svelte.ts?
client.svelte.ts defines 1 function(s): createComponent.
What does client.svelte.ts depend on?
client.svelte.ts imports 1 module(s): svelte.
Where is client.svelte.ts in the architecture?
client.svelte.ts is located at packages/integrations/svelte/src/client.svelte.ts (domain: CoreAstro, subdomain: CoreMiddleware, directory: packages/integrations/svelte/src).

Analyze Your Own Codebase

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

Try Supermodel Free