Skip to content

fix: add dummy anchor for dynamic component HMR wrappers #12252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -653,9 +653,10 @@ function collect_parent_each_blocks(context) {
* @param {import('#compiler').Component | import('#compiler').SvelteComponent | import('#compiler').SvelteSelf} node
* @param {string} component_name
* @param {import('../types.js').ComponentContext} context
* @param {import('estree').Expression} anchor
* @returns {import('estree').Statement}
*/
function serialize_inline_component(node, component_name, context) {
function serialize_inline_component(node, component_name, context, anchor = context.state.node) {
/** @type {Array<import('estree').Property[] | import('estree').Expression>} */
const props_and_spreads = [];

Expand Down Expand Up @@ -946,13 +947,13 @@ function serialize_inline_component(node, component_name, context) {
node_id,
b.thunk(/** @type {import('estree').Expression} */ (context.visit(node.expression))),
b.arrow(
[b.id(component_name)],
[b.id('$$anchor'), b.id(component_name)],
b.block([
...binding_initializers,
b.stmt(
context.state.options.dev
? b.call('$.validate_dynamic_component', b.thunk(prev(node_id)))
: prev(node_id)
? b.call('$.validate_dynamic_component', b.thunk(prev(b.id('$$anchor'))))
: prev(b.id('$$anchor'))
)
])
)
Expand All @@ -970,12 +971,12 @@ function serialize_inline_component(node, component_name, context) {
);

statements.push(
b.stmt(b.call('$.css_props', context.state.node, b.thunk(b.object(custom_css_props)))),
b.stmt(fn(b.member(context.state.node, b.id('lastChild'))))
b.stmt(b.call('$.css_props', anchor, b.thunk(b.object(custom_css_props)))),
b.stmt(fn(b.member(anchor, b.id('lastChild'))))
);
} else {
context.state.template.push('<!>');
statements.push(b.stmt(fn(context.state.node)));
statements.push(b.stmt(fn(anchor)));
}

return statements.length > 1 ? b.block(statements) : statements[0];
Expand Down Expand Up @@ -3025,7 +3026,7 @@ export const template_visitors = {
Component(node, context) {
if (node.metadata.dynamic) {
// Handle dynamic references to what seems like static inline components
const component = serialize_inline_component(node, '$$component', context);
const component = serialize_inline_component(node, '$$component', context, b.id('$$anchor'));
context.state.init.push(
b.stmt(
b.call(
Expand All @@ -3036,7 +3037,7 @@ export const template_visitors = {
b.thunk(
/** @type {import('estree').Expression} */ (context.visit(b.member_id(node.name)))
),
b.arrow([b.id('$$component')], b.block([component]))
b.arrow([b.id('$$anchor'), b.id('$$component')], b.block([component]))
)
)
);
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/dev/hmr.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function hmr(source) {
/** @type {import("#client").Effect} */
let effect;

block(null, 0, () => {
block(anchor, 0, () => {
const component = get(source);

if (effect) {
Expand Down
12 changes: 10 additions & 2 deletions packages/svelte/src/internal/client/dom/blocks/svelte-component.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { DEV } from 'esm-env';
import { block, branch, pause_effect } from '../../reactivity/effects.js';
import { empty } from '../operations.js';

/**
* @template P
* @template {(props: P) => void} C
* @param {import('#client').TemplateNode} anchor
* @param {() => C} get_component
* @param {(component: C) => import('#client').Dom | void} render_fn
* @param {(anchor: import('#client').TemplateNode, component: C) => import('#client').Dom | void} render_fn
* @returns {void}
*/
export function component(anchor, get_component, render_fn) {
Expand All @@ -15,6 +17,11 @@ export function component(anchor, get_component, render_fn) {
/** @type {import('#client').Effect | null} */
let effect;

var component_anchor = anchor;

// create a dummy anchor for the HMR wrapper, if such there be
if (DEV) component_anchor = empty();

block(anchor, 0, () => {
if (component === (component = get_component())) return;

Expand All @@ -24,7 +31,8 @@ export function component(anchor, get_component, render_fn) {
}

if (component) {
effect = branch(() => render_fn(component));
if (DEV) anchor.before(component_anchor);
effect = branch(() => render_fn(component_anchor, component));
}
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>hello</p>
21 changes: 21 additions & 0 deletions packages/svelte/tests/runtime-runes/samples/hmr-removal/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
compileOptions: {
dev: true,
hmr: true
},

html: `<button>toggle</button>`,

test({ assert, target }) {
const button = target.querySelector('button');

flushSync(() => button?.click());
assert.htmlEqual(target.innerHTML, `<button>toggle</button><p>hello</p>`);

flushSync(() => button?.click());
assert.htmlEqual(target.innerHTML, `<button>toggle</button>`);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
import Child from './Child.svelte';

let open = $state(false);
</script>

<button onclick={() => (open = !open)}>toggle</button>

{#if open}
<Child />
{/if}
Loading