Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: always pass original component to HMR wrapper #12454

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should filename and original be behind a symbol, or at least non-enumerable? Didn't occur to me until now but it's probably a bit weird that we're adding these properties for anyone to look at


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

Expand Down
Loading