Description
Follow up to recent conversation on discord regarding v3 support for HMR.
Let me list out the steps we need for a successful hot reload, and then maybe we can discuss if the core needs to change, or whether we can just monkey-patch stuff.
I'm going to gloss over the component resolution part, as I think we already have that handled in the v2 HMR code.
These steps happen when a hot reload process starts:
Step 1
Get the current state of the component and keep it somewhere.
Looks like component.$$.ctx
gives us the whole state.
However, seems like it also returns computed properties, along with handlers and bindings?
Can we work out a way to just get the props (exported or otherwise)?
Step 2
Get the position in the DOM where the component is mounted
We normally add a comment marker just above the component, and store a reference to that comment node.
I think this can be achieved by the compiler using mount_component_dev
.
mount_component_dev
would first call mount_component
, and then store a reference to target
and anchor
on $$
.
Step 3
Destroy the old component
This can be easily achieved using component.$destroy()
.
We don't need any changes to support this.
Step 4
Render updated component in place of the old component
Here, we can mount the new component with mount_component_dev
, using the target
and anchor
we collected on Step 2.
Step 5
Apply state from old component on to the new component
This I'm not sure at all.
We need to find a way to set the state.
Then we need to ensure that all computeds are up to date.
Will calling $$.update()
re-run the computations?
I'll need your help/direction on step 1 and step 5.
I think I can do a PR for Step 2 and Step 4