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

pull/12282/head
Rich Harris 4 months ago committed by GitHub
parent 15a8518b91
commit 445f890cd5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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
*/
@ -279,6 +279,7 @@ export function clean_nodes(
trimmed.length === 1 &&
((first.type === 'RenderTag' && !first.metadata.dynamic) ||
(first.type === 'Component' &&
!state.options.hmr &&
!first.attributes.some(
(attribute) => attribute.type === 'Attribute' && attribute.name.startsWith('--')
)));

@ -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>
`
);
}
});

@ -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…
Cancel
Save