Skip to content

Commit

Permalink
fix: always pass original component to HMR wrapper (#12454)
Browse files Browse the repository at this point in the history
When Vite calls `hot.accept`, it passes the wrapped component, which means we're passing the HMR wrapper along as the new component. Avoid that by maintaining a reference to the original component.
  • Loading branch information
dummdidumm authored Jul 16, 2024
1 parent 1ce4cd5 commit 436cc99
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-lemons-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: always pass original component to HMR wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ export function client_component(source, analysis, options) {

if (options.hmr) {
const accept_fn_body = [
b.stmt(b.call('$.set', b.id('s'), b.member(b.id('module'), b.id('default'))))
b.stmt(b.call('$.set', b.id('s'), b.member(b.id('module.default'), b.id('original'))))
];

if (analysis.css.hash) {
Expand All @@ -440,8 +440,14 @@ export function client_component(source, analysis, options) {
const hmr = b.block([
b.const(b.id('s'), b.call('$.source', b.id(analysis.name))),
b.const(b.id('filename'), b.member(b.id(analysis.name), b.id('filename'))),
b.const(b.id('$$original'), b.id(analysis.name)),
b.stmt(b.assignment('=', b.id(analysis.name), b.call('$.hmr', b.id('s')))),
b.stmt(b.assignment('=', b.member(b.id(analysis.name), b.id('filename')), b.id('filename'))),
// Assign the original component to the wrapper so we can use it on hot reload patching,
// else we would call the HMR function two times
b.stmt(
b.assignment('=', b.member(b.id(analysis.name), b.id('original')), b.id('$$original'))
),
b.stmt(b.call('import.meta.hot.accept', b.arrow([b.id('module')], b.block(accept_fn_body))))
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ function Hmr($$anchor) {
if (import.meta.hot) {
const s = $.source(Hmr);
const filename = Hmr.filename;
const $$original = Hmr;

Hmr = $.hmr(s);
Hmr.filename = filename;
Hmr.original = $$original;

import.meta.hot.accept((module) => {
$.set(s, module.default);
$.set(s, module.default.original);
});
}

Expand Down

0 comments on commit 436cc99

Please sign in to comment.