snippet.js — svelte Source File
Architecture documentation for snippet.js, a javascript file in the svelte codebase. 22 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 55f8ed50_b15a_01d0_494d_397077cb618a["snippet.js"] 1ae6fa4e_16ee_acdf_5e28_17eb0819fddb["effects.js"] 55f8ed50_b15a_01d0_494d_397077cb618a --> 1ae6fa4e_16ee_acdf_5e28_17eb0819fddb 1bd7dd6f_4c22_6f44_9747_fc5ea0deaa7b["block"] 55f8ed50_b15a_01d0_494d_397077cb618a --> 1bd7dd6f_4c22_6f44_9747_fc5ea0deaa7b 20340432_01a2_6741_abf4_60ccab51cdb3["teardown"] 55f8ed50_b15a_01d0_494d_397077cb618a --> 20340432_01a2_6741_abf4_60ccab51cdb3 48cf26f8_bf34_fd7a_3d52_cc963051e167["context.js"] 55f8ed50_b15a_01d0_494d_397077cb618a --> 48cf26f8_bf34_fd7a_3d52_cc963051e167 3b5b04b8_bc64_dc87_37e2_2191011816f5["set_dev_current_component_function"] 55f8ed50_b15a_01d0_494d_397077cb618a --> 3b5b04b8_bc64_dc87_37e2_2191011816f5 f3948b0d_b92a_0767_ba6c_832767f4e2bb["hydration.js"] 55f8ed50_b15a_01d0_494d_397077cb618a --> f3948b0d_b92a_0767_ba6c_832767f4e2bb b31601aa_35ce_7827_5394_99fb97fa27d2["hydrate_next"] 55f8ed50_b15a_01d0_494d_397077cb618a --> b31601aa_35ce_7827_5394_99fb97fa27d2 a156992a_4c74_fff0_8871_1aae7581b781["reconciler.js"] 55f8ed50_b15a_01d0_494d_397077cb618a --> a156992a_4c74_fff0_8871_1aae7581b781 0fd60e7d_ad18_dc03_875d_13156cdee4b4["create_fragment_from_html"] 55f8ed50_b15a_01d0_494d_397077cb618a --> 0fd60e7d_ad18_dc03_875d_13156cdee4b4 2ab7c579_f011_f472_7847_c9e9979c6b2a["template.js"] 55f8ed50_b15a_01d0_494d_397077cb618a --> 2ab7c579_f011_f472_7847_c9e9979c6b2a e23e5c5c_05b1_afa5_e280_5c89012b55a7["assign_nodes"] 55f8ed50_b15a_01d0_494d_397077cb618a --> e23e5c5c_05b1_afa5_e280_5c89012b55a7 df278ca2_0a6c_fefe_09f2_b397500fe3c2["warnings.js"] 55f8ed50_b15a_01d0_494d_397077cb618a --> df278ca2_0a6c_fefe_09f2_b397500fe3c2 ff387d97_d6d2_81e0_e731_656552709d27["errors.js"] 55f8ed50_b15a_01d0_494d_397077cb618a --> ff387d97_d6d2_81e0_e731_656552709d27 9a9bbc27_46b6_021c_6d77_f736ed4b40f0["operations.js"] 55f8ed50_b15a_01d0_494d_397077cb618a --> 9a9bbc27_46b6_021c_6d77_f736ed4b40f0 style 55f8ed50_b15a_01d0_494d_397077cb618a fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
/** @import { Snippet } from 'svelte' */
/** @import { TemplateNode } from '#client' */
/** @import { Getters } from '#shared' */
import { EFFECT_TRANSPARENT, ELEMENT_NODE } from '#client/constants';
import { block, teardown } from '../../reactivity/effects.js';
import {
dev_current_component_function,
set_dev_current_component_function
} from '../../context.js';
import { hydrate_next, hydrate_node, hydrating } from '../hydration.js';
import { create_fragment_from_html } from '../reconciler.js';
import { assign_nodes } from '../template.js';
import * as w from '../../warnings.js';
import * as e from '../../errors.js';
import { DEV } from 'esm-env';
import { get_first_child, get_next_sibling } from '../operations.js';
import { prevent_snippet_stringification } from '../../../shared/validate.js';
import { BranchManager } from './branches.js';
/**
* @template {(node: TemplateNode, ...args: any[]) => void} SnippetFn
* @param {TemplateNode} node
* @param {() => SnippetFn | null | undefined} get_snippet
* @param {(() => any)[]} args
* @returns {void}
*/
export function snippet(node, get_snippet, ...args) {
var branches = new BranchManager(node);
block(() => {
const snippet = get_snippet() ?? null;
if (DEV && snippet == null) {
e.invalid_snippet();
}
branches.ensure(snippet, snippet && ((anchor) => snippet(anchor, ...args)));
}, EFFECT_TRANSPARENT);
}
/**
* In development, wrap the snippet function so that it passes validation, and so that the
* correct component context is set for ownership checks
* @param {any} component
* @param {(node: TemplateNode, ...args: any[]) => void} fn
*/
export function wrap_snippet(component, fn) {
const snippet = (/** @type {TemplateNode} */ node, /** @type {any[]} */ ...args) => {
var previous_component_function = dev_current_component_function;
set_dev_current_component_function(component);
try {
return fn(node, ...args);
} finally {
set_dev_current_component_function(previous_component_function);
}
};
prevent_snippet_stringification(snippet);
return snippet;
}
/**
* Create a snippet programmatically
* @template {unknown[]} Params
* @param {(...params: Getters<Params>) => {
* render: () => string
* setup?: (element: Element) => void | (() => void)
* }} fn
* @returns {Snippet<Params>}
*/
export function createRawSnippet(fn) {
// @ts-expect-error the types are a lie
return (/** @type {TemplateNode} */ anchor, /** @type {Getters<Params>} */ ...params) => {
var snippet = fn(...params);
/** @type {Element} */
var element;
if (hydrating) {
element = /** @type {Element} */ (hydrate_node);
hydrate_next();
} else {
var html = snippet.render().trim();
var fragment = create_fragment_from_html(html);
element = /** @type {Element} */ (get_first_child(fragment));
if (DEV && (get_next_sibling(element) !== null || element.nodeType !== ELEMENT_NODE)) {
w.invalid_raw_snippet_render();
}
anchor.before(element);
}
const result = snippet.setup?.(element);
assign_nodes(element, element);
if (typeof result === 'function') {
teardown(result);
}
};
}
Domain
Subdomains
Dependencies
Source
Frequently Asked Questions
What does snippet.js do?
snippet.js is a source file in the svelte codebase, written in javascript. It belongs to the ClientRuntime domain, Hydration subdomain.
What functions are defined in snippet.js?
snippet.js defines 3 function(s): createRawSnippet, snippet, wrap_snippet.
What does snippet.js depend on?
snippet.js imports 22 module(s): BranchManager, assign_nodes, block, branches.js, constants, context.js, create_fragment_from_html, effects.js, and 14 more.
Where is snippet.js in the architecture?
snippet.js is located at packages/svelte/src/internal/client/dom/blocks/snippet.js (domain: ClientRuntime, subdomain: Hydration, directory: packages/svelte/src/internal/client/dom/blocks).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free