FredKSchott / esm-hmr

a Hot Module Replacement (HMR) API for your ESM-based dev server.
MIT License
413 stars 11 forks source link

`bubbled` info in accept handlers #13

Open rixo opened 4 years ago

rixo commented 4 years ago

I've learned empirically that it can be useful to have a way to tell, from an accept handler, whether it is called because of a direct update hitting the auto accepting module, or if has bubbled from a dependency.

In rollup-plugin-hot, it's implemented as a bubbled argument that is passed to the handler.

The practical use case is CSS injection in svelte-hmr. When a Svelte component is modified, it checks whether only the CSS part has actually changed and, in this case, it does a softer CSS injection -- which is some orders of magnitude safer than recreating stateful JS components and tinkering with DOM elements.

However, it can only do that safely if it is sure that the update came from the component's file itself, and not one of its dependency. If the update has bubbled from a dependency, then the module still need to be reloaded to reflect the change.

FredKSchott commented 4 years ago

I'm +1 on this! The reasoning makes sense