Home / Function/ hmr() — svelte Function Reference

hmr() — svelte Function Reference

Architecture documentation for the hmr() function in hmr.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  3d4087a1_f844_f105_4e79_f5f1a65e1203["hmr()"]
  dd70ba52_70b9_64ef_66d0_34ac556ec3da["hmr.js"]
  3d4087a1_f844_f105_4e79_f5f1a65e1203 -->|defined in| dd70ba52_70b9_64ef_66d0_34ac556ec3da
  1e2f7428_6050_5cb7_69db_bf5db719f6d1["source()"]
  3d4087a1_f844_f105_4e79_f5f1a65e1203 -->|calls| 1e2f7428_6050_5cb7_69db_bf5db719f6d1
  1bd7dd6f_4c22_6f44_9747_fc5ea0deaa7b["block()"]
  3d4087a1_f844_f105_4e79_f5f1a65e1203 -->|calls| 1bd7dd6f_4c22_6f44_9747_fc5ea0deaa7b
  a08b6cc5_af73_1be4_d02f_3113cf8a8305["get()"]
  3d4087a1_f844_f105_4e79_f5f1a65e1203 -->|calls| a08b6cc5_af73_1be4_d02f_3113cf8a8305
  410f774f_2d1a_7114_fcba_b292ed7cae3a["destroy_effect()"]
  3d4087a1_f844_f105_4e79_f5f1a65e1203 -->|calls| 410f774f_2d1a_7114_fcba_b292ed7cae3a
  4ca1b5f2_087e_afec_72d9_534a30fbfe1f["branch()"]
  3d4087a1_f844_f105_4e79_f5f1a65e1203 -->|calls| 4ca1b5f2_087e_afec_72d9_534a30fbfe1f
  d4f2071b_0f08_019b_a9cb_af73d3474607["set_should_intro()"]
  3d4087a1_f844_f105_4e79_f5f1a65e1203 -->|calls| d4f2071b_0f08_019b_a9cb_af73d3474607
  63ee8247_ada4_9f1d_e139_0c1167cd5b1c["set()"]
  3d4087a1_f844_f105_4e79_f5f1a65e1203 -->|calls| 63ee8247_ada4_9f1d_e139_0c1167cd5b1c
  style 3d4087a1_f844_f105_4e79_f5f1a65e1203 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/internal/client/dev/hmr.js lines 14–89

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

Frequently Asked Questions

What does hmr() do?
hmr() is a function in the svelte codebase, defined in packages/svelte/src/internal/client/dev/hmr.js.
Where is hmr() defined?
hmr() is defined in packages/svelte/src/internal/client/dev/hmr.js at line 14.
What does hmr() call?
hmr() calls 7 function(s): block, branch, destroy_effect, get, set, set_should_intro, source.

Analyze Your Own Codebase

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

Try Supermodel Free