Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ekhaled committed Mar 8, 2018
1 parent 6d93789 commit e85d715
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
9 changes: 4 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
Expand Down
15 changes: 13 additions & 2 deletions lib/hot-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}

0 comments on commit e85d715

Please sign in to comment.