Skip to content

Commit b01af74

Browse files
authored
fix: use keys for hmr modules (#11123)
* fix: use keys for hmr modules * simplify * lint * ts
1 parent 1183984 commit b01af74

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

.changeset/brave-gorillas-end.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: use keys for hmr modules

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,12 @@ export function client_component(source, analysis, options) {
427427
b.export_default(
428428
b.conditional(
429429
b.import_meta_hot(),
430-
b.call('$.hmr', b.member(b.import_meta_hot(), b.id('data')), b.id(analysis.name)),
430+
b.call(
431+
'$.hmr',
432+
b.member(b.import_meta_hot(), b.id('data')),
433+
b.id(analysis.name),
434+
b.member(b.member(b.literal('import'), b.literal('meta')), b.literal('url'))
435+
),
431436
b.id(analysis.name)
432437
)
433438
),

packages/svelte/src/internal/client/dev/hmr.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,26 @@ import { get } from '../runtime.js';
55

66
/**
77
* @template {(anchor: Comment, props: any) => any} Component
8-
* @param {{ source: import("#client").Source<Component>; wrapper: Component; }} data
8+
* @param {{ components: Map<string, { source: import("#client").Source<Component>; wrapper: null | Component; }> }} hot_data
9+
* @param {string} key
910
* @param {Component} component
1011
*/
11-
export function hmr(data, component) {
12-
if (data.source) {
13-
set(data.source, component);
12+
export function hmr(hot_data, component, key) {
13+
var components = (hot_data.components ??= new Map());
14+
var data = components.get(key);
15+
16+
if (data === undefined) {
17+
components.set(
18+
key,
19+
(data = {
20+
source: source(component),
21+
wrapper: null
22+
})
23+
);
1424
} else {
15-
data.source = source(component);
25+
set(data.source, component);
1626
}
27+
const component_source = data.source;
1728

1829
return (data.wrapper ??= /** @type {Component} */ (
1930
(anchor, props) => {
@@ -23,7 +34,7 @@ export function hmr(data, component) {
2334
let effect;
2435

2536
block(() => {
26-
const component = get(data.source);
37+
const component = get(component_source);
2738

2839
if (effect) {
2940
// @ts-ignore

0 commit comments

Comments
 (0)