feat: automatic HMR code (nuxt only) #2954
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a quick and dirty implementation of a plugin that automatically appends the HMR code to files that register a pinia store (nuxt only), this only works for very basic setups in this state.
This could be implemented using unplugin so that it works in vue projects (without nuxt) as well, in a separate repo if preferred.
Maybe this functionality was already considered, I couldn't find an old discussion or PR suggesting this 🤔
How it works
Given an example project has the following store:
The plugin checks if it contains
defineStore
and does not containacceptHMRUpdate
, then walks the top level nodes of the parsed code AST to find the export/variable declaration which uses thedefineStore
function. This code likely needs to be expanded to cover more use cases.If an export/variable declaration is found, simply append the necessary HMR code: