MCPcopy
hub / github.com/sveltejs/svelte / hmr

Function hmr

packages/svelte/src/internal/client/dev/hmr.js:14–97  ·  view source on GitHub ↗
(fn)

Source from the content-addressed store, hash-verified

12 * @param {Component} fn
13 */
14export function hmr(fn) {
15 const current = source(fn);
16
17 /**
18 * @param {TemplateNode} initial_anchor
19 * @param {any} props
20 */
21 function wrapper(initial_anchor, props) {
22 let component = {};
23 let instance = {};
24
25 /** @type {Effect} */
26 let effect;
27
28 let ran = false;
29 let anchor = initial_anchor;
30
31 block(() => {
32 if (component === (component = get(current))) {
33 return;
34 }
35
36 if (effect) {
37 // @ts-ignore
38 for (var k in instance) delete instance[k];
39 destroy_effect(effect);
40 }
41
42 effect = branch(() => {
43 anchor = /** @type {any} */ (anchor)[HMR_ANCHOR] ?? anchor;
44
45 // when the component is invalidated, replace it without transitions
46 if (ran) set_should_intro(false);
47
48 // preserve getters/setters
49 var result =
50 // @ts-expect-error
51 new.target ? new component(anchor, props) : component(anchor, props);
52 // a component is not guaranteed to return something and we can't invoke getOwnPropertyDescriptors on undefined
53 if (result) {
54 Object.defineProperties(instance, Object.getOwnPropertyDescriptors(result));
55 }
56
57 if (ran) set_should_intro(true);
58 });
59
60 // Forward the nodes from the inner effect to the outer active effect which would
61 // get them if the HMR wrapper wasn't there. Do this inside the block not outside
62 // so that HMR updates to the component will also update the nodes on the active effect.
63 /** @type {Effect} */ (active_effect).nodes = effect.nodes;
64 }, EFFECT_TRANSPARENT);
65
66 ran = true;
67
68 if (hydrating) {
69 anchor = hydrate_node;
70 }
71

Callers

nothing calls this directly

Calls 2

sourceFunction · 0.90
setFunction · 0.90

Tested by

no test coverage detected