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
pull/11512/head
Dominik G 8 months ago committed by GitHub
parent 0cf6d56ffe
commit 28f3755e40
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: use import.meta.hot.acceptExports when available to support partial hmr in vite

@ -429,6 +429,10 @@ export function client_component(source, analysis, options) {
); );
if (options.hmr) { if (options.hmr) {
const accept_fn = b.arrow(
[b.id('module')],
b.block([b.stmt(b.call('$.set', b.id('s'), b.member(b.id('module'), b.id('default'))))])
);
body.push( body.push(
component, component,
b.if( b.if(
@ -436,16 +440,12 @@ export function client_component(source, analysis, options) {
b.block([ b.block([
b.const(b.id('s'), b.call('$.source', b.id(analysis.name))), b.const(b.id('s'), b.call('$.source', b.id(analysis.name))),
b.stmt(b.assignment('=', b.id(analysis.name), b.call('$.hmr', b.id('s')))), b.stmt(b.assignment('=', b.id(analysis.name), b.call('$.hmr', b.id('s')))),
b.stmt( b.if(
b.call( b.id('import.meta.hot.acceptExports'),
'import.meta.hot.accept', b.stmt(
b.arrow( b.call('import.meta.hot.acceptExports', b.array([b.literal('default')]), accept_fn)
[b.id('module')], ),
b.block([ b.stmt(b.call('import.meta.hot.accept', accept_fn))
b.stmt(b.call('$.set', b.id('s'), b.member(b.id('module'), b.id('default'))))
])
)
)
) )
]) ])
), ),

@ -14,9 +14,11 @@ if (import.meta.hot) {
Hmr = $.hmr(s); Hmr = $.hmr(s);
import.meta.hot.accept((module) => { if (import.meta.hot.acceptExports) import.meta.hot.acceptExports(["default"], (module) => {
$.set(s, module.default);
}); else import.meta.hot.accept((module) => {
$.set(s, module.default); $.set(s, module.default);
}); });
} }
export default Hmr; export default Hmr;

Loading…
Cancel
Save