Home / File/ slots.ts — astro Source File

slots.ts — astro Source File

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

File typescript CoreAstro RenderingEngine 7 imports 1 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  8991a42b_851a_4b20_3c51_bc8fbe6ed0e7["slots.ts"]
  9e624f62_bcfa_4352_e9a0_dd454823f706["../../runtime/server/index.js"]
  8991a42b_851a_4b20_3c51_bc8fbe6ed0e7 --> 9e624f62_bcfa_4352_e9a0_dd454823f706
  a03b2583_c174_d857_8273_b0c5341bd79f["../../runtime/server/jsx.js"]
  8991a42b_851a_4b20_3c51_bc8fbe6ed0e7 --> a03b2583_c174_d857_8273_b0c5341bd79f
  331fe5de_39c8_462f_a089_e3ee4a1fcd11["./index.js"]
  8991a42b_851a_4b20_3c51_bc8fbe6ed0e7 --> 331fe5de_39c8_462f_a089_e3ee4a1fcd11
  ff0be6cd_e34f_b283_f541_4038fdcadce5["./instruction.js"]
  8991a42b_851a_4b20_3c51_bc8fbe6ed0e7 --> ff0be6cd_e34f_b283_f541_4038fdcadce5
  10d4e39f_edb6_3e34_aa93_ae1211e7da05["../types/public/internal.js"]
  8991a42b_851a_4b20_3c51_bc8fbe6ed0e7 --> 10d4e39f_edb6_3e34_aa93_ae1211e7da05
  ef8a1e3f_e350_75a6_b92d_62a8566d8db9["../core/errors/index.js"]
  8991a42b_851a_4b20_3c51_bc8fbe6ed0e7 --> ef8a1e3f_e350_75a6_b92d_62a8566d8db9
  d3861967_b647_84d2_ff48_15013353bd56["../core/logger/core.js"]
  8991a42b_851a_4b20_3c51_bc8fbe6ed0e7 --> d3861967_b647_84d2_ff48_15013353bd56
  style 8991a42b_851a_4b20_3c51_bc8fbe6ed0e7 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { type ComponentSlots, renderSlotToString } from '../../runtime/server/index.js';
import { renderJSX } from '../../runtime/server/jsx.js';
import { chunkToString } from '../../runtime/server/render/index.js';
import { isRenderInstruction } from '../../runtime/server/render/instruction.js';
import type { SSRResult } from '../../types/public/internal.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
import type { Logger } from '../logger/core.js';

function getFunctionExpression(slot: any) {
	if (!slot) return;
	const expressions = slot?.expressions?.filter((e: unknown) => isRenderInstruction(e) === false);
	if (expressions?.length !== 1) return;
	return expressions[0] as (...args: any[]) => any;
}

export class Slots {
	#result: SSRResult;
	#slots: ComponentSlots | null;
	#logger: Logger;

	constructor(result: SSRResult, slots: ComponentSlots | null, logger: Logger) {
		this.#result = result;
		this.#slots = slots;
		this.#logger = logger;

		if (slots) {
			for (const key of Object.keys(slots)) {
				if ((this as any)[key] !== undefined) {
					throw new AstroError({
						...AstroErrorData.ReservedSlotName,
						message: AstroErrorData.ReservedSlotName.message(key),
					});
				}
				Object.defineProperty(this, key, {
					get() {
						return true;
					},
					enumerable: true,
				});
			}
		}
	}

	public has(name: string) {
		if (!this.#slots) return false;
		return Boolean(this.#slots[name]);
	}

	public async render(name: string, args: any[] = []) {
		if (!this.#slots || !this.has(name)) return;

		const result = this.#result;
		if (!Array.isArray(args)) {
			this.#logger.warn(
				null,
				`Expected second parameter to be an array, received a ${typeof args}. If you're trying to pass an array as a single argument and getting unexpected results, make sure you're passing your array as a item of an array. Ex: Astro.slots.render('default', [["Hello", "World"]])`,
			);
		} else if (args.length > 0) {
			const slotValue = this.#slots[name];
			const component = typeof slotValue === 'function' ? await slotValue(result) : await slotValue;

			// Astro
			const expression = getFunctionExpression(component);
			if (expression) {
				const slot = async () =>
					typeof expression === 'function' ? expression(...args) : expression;
				return await renderSlotToString(result, slot).then((res) => {
					return res;
				});
			}
			// JSX
			if (typeof component === 'function') {
				return await renderJSX(result, (component as any)(...args)).then((res) =>
					res != null ? String(res) : res,
				);
			}
		}

		const content = await renderSlotToString(result, this.#slots[name]);
		const outHTML = chunkToString(result, content);

		return outHTML;
	}
}

Domain

Subdomains

Classes

Dependencies

  • ../../runtime/server/index.js
  • ../../runtime/server/jsx.js
  • ../core/errors/index.js
  • ../core/logger/core.js
  • ../types/public/internal.js
  • ./index.js
  • ./instruction.js

Frequently Asked Questions

What does slots.ts do?
slots.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 slots.ts?
slots.ts defines 1 function(s): getFunctionExpression.
What does slots.ts depend on?
slots.ts imports 7 module(s): ../../runtime/server/index.js, ../../runtime/server/jsx.js, ../core/errors/index.js, ../core/logger/core.js, ../types/public/internal.js, ./index.js, ./instruction.js.
Where is slots.ts in the architecture?
slots.ts is located at packages/astro/src/core/render/slots.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/core/render).

Analyze Your Own Codebase

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

Try Supermodel Free