Skip to content

Commit 445f890

Browse files
authored
fix: create wrapper template for components in HMR mode (#12304)
1 parent 15a8518 commit 445f890

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

packages/svelte/src/compiler/phases/3-transform/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function sort_const_tags(nodes, state) {
137137
* @param {Compiler.SvelteNode[]} nodes
138138
* @param {Compiler.SvelteNode[]} path
139139
* @param {Compiler.Namespace} namespace
140-
* @param {TransformState} state
140+
* @param {TransformState & { options: Compiler.ValidatedCompileOptions }} state
141141
* @param {boolean} preserve_whitespace
142142
* @param {boolean} preserve_comments
143143
*/
@@ -279,6 +279,7 @@ export function clean_nodes(
279279
trimmed.length === 1 &&
280280
((first.type === 'RenderTag' && !first.metadata.dynamic) ||
281281
(first.type === 'Component' &&
282+
!state.options.hmr &&
282283
!first.attributes.some(
283284
(attribute) => attribute.type === 'Attribute' && attribute.name.startsWith('--')
284285
)));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>child</p>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
html: `<button>unshift</button>`,
6+
7+
compileOptions: {
8+
dev: true,
9+
hmr: true
10+
},
11+
12+
test({ assert, target }) {
13+
const btn = target.querySelector('button');
14+
15+
flushSync(() => btn?.click());
16+
assert.htmlEqual(
17+
target.innerHTML,
18+
`
19+
<button>unshift</button>
20+
<p>child</p>
21+
`
22+
);
23+
24+
flushSync(() => btn?.click());
25+
assert.htmlEqual(
26+
target.innerHTML,
27+
`
28+
<button>unshift</button>
29+
<p>child</p>
30+
<p>child</p>
31+
`
32+
);
33+
}
34+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script>
2+
import Child from './Child.svelte';
3+
4+
let uid = 0;
5+
6+
/** @type {Array<{ id: number }>} */
7+
let items = $state([]);
8+
</script>
9+
10+
<button onclick={() => items.unshift({ id: uid++ })}>unshift</button>
11+
12+
{#each items as item (item.id)}
13+
<Child />
14+
{/each}

0 commit comments

Comments
 (0)