Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for ekhaled/svelte-hot-loader#10 #44

Merged
merged 1 commit into from
Mar 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line was causing the problem

$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;
}