mirror of https://github.com/sveltejs/svelte
feat: hot module reloading support for Svelte 5 (#11106)
* feat: hot module reloading support for Svelte 5 * fix lockfile * tweaks * types * lint * lint * tweaks * add hmr flag * tweak * tweaks * move HMR logic into its own module * simplify * tidy up types * fix test * lint * need some indirection here or references break * prevent transitions during HMR update --------- Co-authored-by: Rich Harris <rich.harris@vercel.com>pull/11111/head
parent
e1b2d29eda
commit
afe1d11a5b
@ -0,0 +1,5 @@
|
||||
---
|
||||
"svelte": patch
|
||||
---
|
||||
|
||||
feat: hot module reloading support for Svelte 5
|
@ -0,0 +1,44 @@
|
||||
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 {{ source: import("#client").Source<Component>; wrapper: Component; }} data
|
||||
* @param {Component} component
|
||||
*/
|
||||
export function hmr(data, component) {
|
||||
if (data.source) {
|
||||
set(data.source, component);
|
||||
} else {
|
||||
data.source = source(component);
|
||||
}
|
||||
|
||||
return (data.wrapper ??= /** @type {Component} */ (
|
||||
(anchor, props) => {
|
||||
let instance = {};
|
||||
|
||||
/** @type {import("#client").Effect} */
|
||||
let effect;
|
||||
|
||||
block(() => {
|
||||
const component = get(data.source);
|
||||
|
||||
if (effect) {
|
||||
// @ts-ignore
|
||||
for (var k in instance) delete instance[k];
|
||||
destroy_effect(effect);
|
||||
}
|
||||
|
||||
effect = branch(() => {
|
||||
set_should_intro(false);
|
||||
Object.assign(instance, component(anchor, props));
|
||||
set_should_intro(true);
|
||||
});
|
||||
});
|
||||
|
||||
return instance;
|
||||
}
|
||||
));
|
||||
}
|
Loading…
Reference in new issue