Skip to content

fix: create wrapper template for components in HMR mode #12304

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 1 commit into from
Jul 4, 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
3 changes: 2 additions & 1 deletion packages/svelte/src/compiler/phases/3-transform/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function sort_const_tags(nodes, state) {
* @param {Compiler.SvelteNode[]} nodes
* @param {Compiler.SvelteNode[]} path
* @param {Compiler.Namespace} namespace
* @param {TransformState} state
* @param {TransformState & { options: Compiler.ValidatedCompileOptions }} state
* @param {boolean} preserve_whitespace
* @param {boolean} preserve_comments
*/
Expand Down Expand Up @@ -279,6 +279,7 @@ export function clean_nodes(
trimmed.length === 1 &&
((first.type === 'RenderTag' && !first.metadata.dynamic) ||
(first.type === 'Component' &&
!state.options.hmr &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably good to have a comment explaining a bit why we can't go without a wrapper in HMR mode

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's marge this for now so it unblocks people, we can always have a follow up PR to add comments.

Copy link
Member

@dummdidumm dummdidumm Jul 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My proposal for a comment:

Suggested change
!state.options.hmr &&
// HMR components can be destroyed/recreated independently. In that sense they're a bit
// like a dynamic Svelte component and therefore the anchor is necessary.
!state.options.hmr &&

!first.attributes.some(
(attribute) => attribute.type === 'Attribute' && attribute.name.startsWith('--')
)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>child</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
html: `<button>unshift</button>`,

compileOptions: {
dev: true,
hmr: true
},

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

flushSync(() => btn?.click());
assert.htmlEqual(
target.innerHTML,
`
<button>unshift</button>
<p>child</p>
`
);

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

let uid = 0;

/** @type {Array<{ id: number }>} */
let items = $state([]);
</script>

<button onclick={() => items.unshift({ id: uid++ })}>unshift</button>

{#each items as item (item.id)}
<Child />
{/each}
Loading