feat: simplify HMR logic (#12391)

pull/12387/head
Rich Harris 1 year ago committed by GitHub
parent 125156d7d5
commit f3f4ce5741
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
feat: simpler HMR logic

@ -437,36 +437,15 @@ export function client_component(source, analysis, options) {
); );
} }
body.push( const hmr = b.block([
component,
b.if(
b.id('import.meta.hot'),
b.block([
b.const(b.id('s'), b.call('$.source', b.id(analysis.name))), 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('filename'), b.member(b.id(analysis.name), b.id('filename'))),
b.const(b.id('accept'), b.arrow([b.id('module')], b.block(accept_fn_body))),
b.stmt(b.assignment('=', b.id(analysis.name), b.call('$.hmr', b.id('s')))), 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.member(b.id(analysis.name), b.id('filename')), b.id('filename')) b.stmt(b.call('import.meta.hot.accept', b.arrow([b.id('module')], b.block(accept_fn_body))))
), ]);
b.if(
b.id('import.meta.hot.acceptExports'),
b.block([
b.stmt(
b.call(
'import.meta.hot.acceptExports',
b.array([b.literal('default')]),
b.id('accept')
)
)
]),
b.block([b.stmt(b.call('import.meta.hot.accept', b.id('accept')))])
)
])
),
b.export_default(b.id(analysis.name)) body.push(component, b.if(b.id('import.meta.hot'), hmr), b.export_default(b.id(analysis.name)));
);
} else { } else {
body.push(b.export_default(component)); body.push(b.export_default(component));
} }

@ -13,18 +13,12 @@ if (import.meta.hot) {
const s = $.source(Hmr); const s = $.source(Hmr);
const filename = Hmr.filename; const filename = Hmr.filename;
const accept = (module) => {
$.set(s, module.default);
};
Hmr = $.hmr(s); Hmr = $.hmr(s);
Hmr.filename = filename; Hmr.filename = filename;
if (import.meta.hot.acceptExports) { import.meta.hot.accept((module) => {
import.meta.hot.acceptExports(["default"], accept); $.set(s, module.default);
} else { });
import.meta.hot.accept(accept);
}
} }
export default Hmr; export default Hmr;
Loading…
Cancel
Save