Fix hot reloading / hot module replacement
I am happy to work on this issue if you think it's worthwhile. I use Parcel as my bundler, so that would be my focus.
I think patching the Riot core (this repo) and the https://github.com/riot/hot-reload repo would be sufficient.
Is your feature request related to a problem? Please describe. Hot module reloading is mostly broken. Here's what works and what doesn't:
✅ reload keeps tag state ✅ reload updates CSS ❌ reloaded component receives updated props from parent ❌ parent correctly renders unmounted and remounted child component after HMR ❌ reloaded components not currently rendered are updated
Describe the solution you'd like
From what I can tell, every time we compile a component, we call createComponentFromWrapper. Maybe we could change createComponentFromWrapper to return a closure that compiles the component lazily. The template chunk and the children components would be cached in a WeakMap.
Then, resolve the wrapper right before instantiating. Note that setComponentWrapperResolver can be called to update our resolver. Default resolver is just (w) => w, but the hot-reload package can override this.
// src/core/create-component-from-wrapper.js
export function createComponentFromWrapper(componentWrapper) {
return (options) =>
getCompiledComponentWrapper(resolveComponentWrapper(componentWrapper))(options)
}
// src/core/component-wrapper-resolver.js
const defaultResolver = (wrapper) => wrapper
const currentResolver = { fn: defaultResolver }
export function resolveComponentWrapper(wrapper) {
return currentResolver.fn(wrapper) || wrapper
}
export function setComponentWrapperResolver(fn) {
const previousResolver = currentResolver.fn
currentResolver.fn = fn || defaultResolver
return previousResolver // so that resolvers can be chained
}Let the tag adapter replace itself when its implementation changes. The object createComponentFromWrapper returns is exactly what a parent's TagBinding holds in this.tag.
update(parentScope, state) {
return replaceStaleComponent(parentScope)
? current.component
: current.component.update(state, parentScope)
}replaceStaleComponent re-resolves the wrapper, and if it changed, unmounts the old instance preserving its root node, instantiates the new one and mounts it with the current parentScope. Now props are recomputed from the live attribute expressions.
@riotjs/hot-reload then keeps a Map of component names to the latest wrapper, installs a resolver returning wrappers from the map, and walks up the DOM to the owning component to call update() on it.
Describe alternatives you've considered
Considered making createComponentFromWrapper consult COMPONENTS_IMPLEMENTATION_MAP by name first, so @riotjs/hot-reload could just unregister() + register(). No new API at all, but I feel like this could break namespacing, since COMPONENTS_IMPLEMENTATION_MAP is global.
Additional context
Every component update would call resolveComponentWrapper() and compare the result.
@riotjs/hot-reload could feature-detect the resolver hook, so on older versions of Riot, we keeps existing (albeit broken) behavior.
Source: riot/riot