From 28f3755e407032f811c85a591dd20208097eaffa Mon Sep 17 00:00:00 2001 From: Dominik G Date: Wed, 8 May 2024 14:13:50 +0200 Subject: [PATCH] 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 --- .changeset/slimy-onions-approve.md | 5 +++++ .../3-transform/client/transform-client.js | 20 +++++++++---------- .../hmr/_expected/client/index.svelte.js | 6 ++++-- 3 files changed, 19 insertions(+), 12 deletions(-) create mode 100644 .changeset/slimy-onions-approve.md diff --git a/.changeset/slimy-onions-approve.md b/.changeset/slimy-onions-approve.md new file mode 100644 index 0000000000..5fb6d305dc --- /dev/null +++ b/.changeset/slimy-onions-approve.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +fix: use import.meta.hot.acceptExports when available to support partial hmr in vite diff --git a/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js b/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js index a32f54ea78..5495f3f6be 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js @@ -429,6 +429,10 @@ export function client_component(source, analysis, options) { ); 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( component, b.if( @@ -436,16 +440,12 @@ export function client_component(source, analysis, options) { b.block([ 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.call( - 'import.meta.hot.accept', - b.arrow( - [b.id('module')], - b.block([ - b.stmt(b.call('$.set', b.id('s'), b.member(b.id('module'), b.id('default')))) - ]) - ) - ) + b.if( + b.id('import.meta.hot.acceptExports'), + b.stmt( + b.call('import.meta.hot.acceptExports', b.array([b.literal('default')]), accept_fn) + ), + b.stmt(b.call('import.meta.hot.accept', accept_fn)) ) ]) ), diff --git a/packages/svelte/tests/snapshot/samples/hmr/_expected/client/index.svelte.js b/packages/svelte/tests/snapshot/samples/hmr/_expected/client/index.svelte.js index f1c08f4ed8..46a18e0b2b 100644 --- a/packages/svelte/tests/snapshot/samples/hmr/_expected/client/index.svelte.js +++ b/packages/svelte/tests/snapshot/samples/hmr/_expected/client/index.svelte.js @@ -14,9 +14,11 @@ if (import.meta.hot) { 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); }); } -export default Hmr; \ No newline at end of file +export default Hmr;