alternative HMR update fix

pull/12542/head
Rich Harris 2 years ago
parent 346cf96599
commit 8d0a6b471b

@ -418,7 +418,13 @@ 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.default'), b.id('$.ORIGINAL'), true)))
b.stmt(
b.call(
'$.set',
b.id('import.meta.hot.data.source'),
b.member(b.id('module.default'), b.id('$.ORIGINAL'), true)
)
)
];
if (analysis.css.hash) {
@ -438,13 +444,27 @@ 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.stmt(
b.assignment(
'??=',
b.id('import.meta.hot.data.source'),
b.call('$.source', b.id(analysis.name))
)
),
b.const(b.id('$$filename'), b.member(b.id(analysis.name), b.id('$.FILENAME'), true)),
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'))),
b.stmt(
b.assignment('=', b.id(analysis.name), b.call('$.hmr', b.id('import.meta.hot.data.source')))
),
b.stmt(
b.assignment(
'=',
b.member(b.id(analysis.name), b.id('$.FILENAME'), true),
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
// else we would create a matryoshka doll of HMR wrappers
b.stmt(
b.assignment(
'=',

@ -10,16 +10,17 @@ function Hmr($$anchor) {
}
if (import.meta.hot) {
const s = $.source(Hmr);
const filename = Hmr.filename;
import.meta.hot.data.source ??= $.source(Hmr);
const $$filename = Hmr[$.FILENAME];
const $$original = Hmr;
Hmr = $.hmr(s);
Hmr.filename = filename;
Hmr = $.hmr(import.meta.hot.data.source);
Hmr[$.FILENAME] = $$filename;
Hmr[$.ORIGINAL] = $$original;
import.meta.hot.accept((module) => {
$.set(s, module.default[$.ORIGINAL]);
$.set(import.meta.hot.data.source, module.default[$.ORIGINAL]);
});
}

Loading…
Cancel
Save