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) { if (options.hmr) {
const accept_fn_body = [ 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) { if (analysis.css.hash) {
@ -438,13 +444,27 @@ export function client_component(source, analysis, options) {
} }
const hmr = b.block([ const hmr = b.block([
b.const(b.id('s'), b.call('$.source', b.id(analysis.name))), b.stmt(
b.const(b.id('filename'), b.member(b.id(analysis.name), b.id('filename'))), 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.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.stmt(b.assignment('=', b.member(b.id(analysis.name), b.id('filename')), b.id('filename'))), 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, // 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.stmt(
b.assignment( b.assignment(
'=', '=',

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

Loading…
Cancel
Save