diff --git a/index.js b/index.js index bd985080..a06f0f90 100644 --- a/index.js +++ b/index.js @@ -8,8 +8,6 @@ function makeHot(id, code, hotOptions) { const options = JSON.stringify(hotOptions); const replacement = ` -let proxyComponent = $2; - if (module.hot) { const { configure, register, reload } = require('svelte-loader/lib/hot-api'); @@ -19,14 +17,15 @@ if (module.hot) { if (!module.hot.data) { // initial load configure(${options}); - proxyComponent = register(${id}, $2); + $2 = register(${id}, $2); } else { // hot update - reload(${id}, proxyComponent); + $2 = reload(${id}, $2); } } -export default proxyComponent; + +export default $2; `; return code.replace(/(export default ([^;]*));/, replacement); diff --git a/lib/hot-api.js b/lib/hot-api.js index 86d7247a..73567782 100644 --- a/lib/hot-api.js +++ b/lib/hot-api.js @@ -18,7 +18,15 @@ export function register(id, component) { instances: [] }); - return createProxy(id); + //create the proxy itself + const proxy = createProxy(id); + + //patch the registry record with proxy constructor + const record = Registry.get(id); + record.proxy = proxy; + Registry.set(id, record); + + return proxy; } export function reload(id, component) { @@ -33,8 +41,11 @@ export function reload(id, component) { Registry.set(id, record); - //re-render the proxies + //re-render the proxy instances record.instances.slice().forEach(function(instance) { instance && instance._rerender(); }); + + //return the original proxy constructor that was `register()`-ed + return record.proxy; } \ No newline at end of file