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(
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('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.member(b.id(analysis.name), b.id('filename')), b.id('filename'))
),
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))
);
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(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.call('import.meta.hot.accept', b.arrow([b.id('module')], b.block(accept_fn_body))))
]);
body.push(component, b.if(b.id('import.meta.hot'), hmr), b.export_default(b.id(analysis.name)));
} else {
body.push(b.export_default(component));
}

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