Skip to content

Commit 28f3755

Browse files
authored
fix: use acceptExports to support partial hmr (#11453)
* fix: use acceptExports to support partial hmr * fix: add condition to only use acceptExports when it is available * fix: update test snapshot * fix: format
1 parent 0cf6d56 commit 28f3755

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

.changeset/slimy-onions-approve.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 import.meta.hot.acceptExports when available to support partial hmr in vite

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -429,23 +429,23 @@ export function client_component(source, analysis, options) {
429429
);
430430

431431
if (options.hmr) {
432+
const accept_fn = b.arrow(
433+
[b.id('module')],
434+
b.block([b.stmt(b.call('$.set', b.id('s'), b.member(b.id('module'), b.id('default'))))])
435+
);
432436
body.push(
433437
component,
434438
b.if(
435439
b.id('import.meta.hot'),
436440
b.block([
437441
b.const(b.id('s'), b.call('$.source', b.id(analysis.name))),
438442
b.stmt(b.assignment('=', b.id(analysis.name), b.call('$.hmr', b.id('s')))),
439-
b.stmt(
440-
b.call(
441-
'import.meta.hot.accept',
442-
b.arrow(
443-
[b.id('module')],
444-
b.block([
445-
b.stmt(b.call('$.set', b.id('s'), b.member(b.id('module'), b.id('default'))))
446-
])
447-
)
448-
)
443+
b.if(
444+
b.id('import.meta.hot.acceptExports'),
445+
b.stmt(
446+
b.call('import.meta.hot.acceptExports', b.array([b.literal('default')]), accept_fn)
447+
),
448+
b.stmt(b.call('import.meta.hot.accept', accept_fn))
449449
)
450450
])
451451
),

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ if (import.meta.hot) {
1414

1515
Hmr = $.hmr(s);
1616

17-
import.meta.hot.accept((module) => {
17+
if (import.meta.hot.acceptExports) import.meta.hot.acceptExports(["default"], (module) => {
18+
$.set(s, module.default);
19+
}); else import.meta.hot.accept((module) => {
1820
$.set(s, module.default);
1921
});
2022
}
2123

22-
export default Hmr;
24+
export default Hmr;

0 commit comments

Comments
 (0)