Home / File/ hmr.js — svelte Source File

hmr.js — svelte Source File

Architecture documentation for hmr.js, a javascript file in the svelte codebase. 14 imports, 0 dependents.

File javascript ClientRuntime Hydration 14 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  dd70ba52_70b9_64ef_66d0_34ac556ec3da["hmr.js"]
  73865c3c_2786_c9ac_d34f_b51d28b3a29e["constants.js"]
  dd70ba52_70b9_64ef_66d0_34ac556ec3da --> 73865c3c_2786_c9ac_d34f_b51d28b3a29e
  f3948b0d_b92a_0767_ba6c_832767f4e2bb["hydration.js"]
  dd70ba52_70b9_64ef_66d0_34ac556ec3da --> f3948b0d_b92a_0767_ba6c_832767f4e2bb
  1ae6fa4e_16ee_acdf_5e28_17eb0819fddb["effects.js"]
  dd70ba52_70b9_64ef_66d0_34ac556ec3da --> 1ae6fa4e_16ee_acdf_5e28_17eb0819fddb
  1bd7dd6f_4c22_6f44_9747_fc5ea0deaa7b["block"]
  dd70ba52_70b9_64ef_66d0_34ac556ec3da --> 1bd7dd6f_4c22_6f44_9747_fc5ea0deaa7b
  4ca1b5f2_087e_afec_72d9_534a30fbfe1f["branch"]
  dd70ba52_70b9_64ef_66d0_34ac556ec3da --> 4ca1b5f2_087e_afec_72d9_534a30fbfe1f
  410f774f_2d1a_7114_fcba_b292ed7cae3a["destroy_effect"]
  dd70ba52_70b9_64ef_66d0_34ac556ec3da --> 410f774f_2d1a_7114_fcba_b292ed7cae3a
  e5c35d51_28d8_9054_923d_b7f82a3c8dc2["sources.js"]
  dd70ba52_70b9_64ef_66d0_34ac556ec3da --> e5c35d51_28d8_9054_923d_b7f82a3c8dc2
  63ee8247_ada4_9f1d_e139_0c1167cd5b1c["set"]
  dd70ba52_70b9_64ef_66d0_34ac556ec3da --> 63ee8247_ada4_9f1d_e139_0c1167cd5b1c
  1e2f7428_6050_5cb7_69db_bf5db719f6d1["source"]
  dd70ba52_70b9_64ef_66d0_34ac556ec3da --> 1e2f7428_6050_5cb7_69db_bf5db719f6d1
  deb813bb_c5d2_3dfd_2554_606359abaa83["render.js"]
  dd70ba52_70b9_64ef_66d0_34ac556ec3da --> deb813bb_c5d2_3dfd_2554_606359abaa83
  d4f2071b_0f08_019b_a9cb_af73d3474607["set_should_intro"]
  dd70ba52_70b9_64ef_66d0_34ac556ec3da --> d4f2071b_0f08_019b_a9cb_af73d3474607
  bde4209f_8ffc_1594_4024_b1835a44bcf6["runtime.js"]
  dd70ba52_70b9_64ef_66d0_34ac556ec3da --> bde4209f_8ffc_1594_4024_b1835a44bcf6
  a08b6cc5_af73_1be4_d02f_3113cf8a8305["get"]
  dd70ba52_70b9_64ef_66d0_34ac556ec3da --> a08b6cc5_af73_1be4_d02f_3113cf8a8305
  54c2bfce_50b6_b8cc_4371_e1e14f283fb3["constants"]
  dd70ba52_70b9_64ef_66d0_34ac556ec3da --> 54c2bfce_50b6_b8cc_4371_e1e14f283fb3
  style dd70ba52_70b9_64ef_66d0_34ac556ec3da fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/** @import { Effect, TemplateNode } from '#client' */
import { FILENAME, HMR } from '../../../constants.js';
import { EFFECT_TRANSPARENT } from '#client/constants';
import { hydrate_node, hydrating } from '../dom/hydration.js';
import { block, branch, destroy_effect } from '../reactivity/effects.js';
import { set, source } from '../reactivity/sources.js';
import { set_should_intro } from '../render.js';
import { get } from '../runtime.js';

/**
 * @template {(anchor: Comment, props: any) => any} Component
 * @param {Component} fn
 */
export function hmr(fn) {
	const current = source(fn);

	/**
	 * @param {TemplateNode} anchor
	 * @param {any} props
	 */
	function wrapper(anchor, props) {
		let component = {};
		let instance = {};

		/** @type {Effect} */
		let effect;

		let ran = false;

		block(() => {
			if (component === (component = get(current))) {
				return;
			}

			if (effect) {
				// @ts-ignore
				for (var k in instance) delete instance[k];
				destroy_effect(effect);
			}

			effect = branch(() => {
				// when the component is invalidated, replace it without transitions
				if (ran) set_should_intro(false);

				// preserve getters/setters
				Object.defineProperties(
					instance,
					Object.getOwnPropertyDescriptors(
						// @ts-expect-error
						new.target ? new component(anchor, props) : component(anchor, props)
					)
				);

				if (ran) set_should_intro(true);
			});
		}, EFFECT_TRANSPARENT);

		ran = true;

		if (hydrating) {
			anchor = hydrate_node;
		}

		return instance;
	}

	// @ts-expect-error
	wrapper[FILENAME] = fn[FILENAME];

	// @ts-ignore
	wrapper[HMR] = {
		fn,
		current,
		update: (/** @type {any} */ incoming) => {
			// This logic ensures that the first version of the component is the one
			// whose update function and therefore block effect is preserved across updates.
			// If we don't do this dance and instead just use `incoming` as the new component
			// and then update, we'll create an ever-growing stack of block effects.

			// Trigger the original block effect
			set(wrapper[HMR].current, incoming[HMR].fn);

			// Replace the incoming source with the original one
			incoming[HMR].current = wrapper[HMR].current;
		}
	};

	return wrapper;
}

Domain

Subdomains

Functions

Frequently Asked Questions

What does hmr.js do?
hmr.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 hmr.js?
hmr.js defines 1 function(s): hmr.
What does hmr.js depend on?
hmr.js imports 14 module(s): block, branch, constants, constants.js, destroy_effect, effects.js, get, hydration.js, and 6 more.
Where is hmr.js in the architecture?
hmr.js is located at packages/svelte/src/internal/client/dev/hmr.js (domain: ClientRuntime, subdomain: Hydration, directory: packages/svelte/src/internal/client/dev).

Analyze Your Own Codebase

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

Try Supermodel Free