Skip to content

Commit f3f4ce5

Browse files
authored
feat: simplify HMR logic (#12391)
1 parent 125156d commit f3f4ce5

File tree

3 files changed

+17
-39
lines changed

3 files changed

+17
-39
lines changed

.changeset/polite-tomatoes-trade.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+
feat: simpler HMR logic

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

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -437,36 +437,15 @@ export function client_component(source, analysis, options) {
437437
);
438438
}
439439

440-
body.push(
441-
component,
442-
b.if(
443-
b.id('import.meta.hot'),
444-
b.block([
445-
b.const(b.id('s'), b.call('$.source', b.id(analysis.name))),
446-
b.const(b.id('filename'), b.member(b.id(analysis.name), b.id('filename'))),
447-
b.const(b.id('accept'), b.arrow([b.id('module')], b.block(accept_fn_body))),
448-
b.stmt(b.assignment('=', b.id(analysis.name), b.call('$.hmr', b.id('s')))),
449-
b.stmt(
450-
b.assignment('=', b.member(b.id(analysis.name), b.id('filename')), b.id('filename'))
451-
),
452-
b.if(
453-
b.id('import.meta.hot.acceptExports'),
454-
b.block([
455-
b.stmt(
456-
b.call(
457-
'import.meta.hot.acceptExports',
458-
b.array([b.literal('default')]),
459-
b.id('accept')
460-
)
461-
)
462-
]),
463-
b.block([b.stmt(b.call('import.meta.hot.accept', b.id('accept')))])
464-
)
465-
])
466-
),
467-
468-
b.export_default(b.id(analysis.name))
469-
);
440+
const hmr = b.block([
441+
b.const(b.id('s'), b.call('$.source', b.id(analysis.name))),
442+
b.const(b.id('filename'), b.member(b.id(analysis.name), b.id('filename'))),
443+
b.stmt(b.assignment('=', b.id(analysis.name), b.call('$.hmr', b.id('s')))),
444+
b.stmt(b.assignment('=', b.member(b.id(analysis.name), b.id('filename')), b.id('filename'))),
445+
b.stmt(b.call('import.meta.hot.accept', b.arrow([b.id('module')], b.block(accept_fn_body))))
446+
]);
447+
448+
body.push(component, b.if(b.id('import.meta.hot'), hmr), b.export_default(b.id(analysis.name)));
470449
} else {
471450
body.push(b.export_default(component));
472451
}

packages/svelte/tests/snapshot/samples/hmr/_expected/client/index.svelte.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,12 @@ if (import.meta.hot) {
1313
const s = $.source(Hmr);
1414
const filename = Hmr.filename;
1515

16-
const accept = (module) => {
17-
$.set(s, module.default);
18-
};
19-
2016
Hmr = $.hmr(s);
2117
Hmr.filename = filename;
2218

23-
if (import.meta.hot.acceptExports) {
24-
import.meta.hot.acceptExports(["default"], accept);
25-
} else {
26-
import.meta.hot.accept(accept);
27-
}
19+
import.meta.hot.accept((module) => {
20+
$.set(s, module.default);
21+
});
2822
}
2923

3024
export default Hmr;

0 commit comments

Comments
 (0)