- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 1.2k
 
feat: automatic HMR code (nuxt only) #2954
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
base: v3
Are you sure you want to change the base?
feat: automatic HMR code (nuxt only) #2954
Conversation
          ✅ Deploy Preview for pinia-official canceled.
  | 
    
          ✅ Deploy Preview for pinia-official canceled.
  | 
    
| 
          
 Note Currently processing new changes in this PR. This may take a few minutes, please wait... 📒 Files selected for processing (6)
 Tip You can get early access to new features in CodeRabbit.Enable the  ✨ Finishing touches
 🧪 Generate unit tests (beta)
 Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment   | 
    
          
commit:   | 
    
          Codecov Report❌ Patch coverage is  
 Additional details and impacted files@@            Coverage Diff             @@
##               v3    #2954      +/-   ##
==========================================
- Coverage   91.34%   90.84%   -0.50%     
==========================================
  Files          17       19       +2     
  Lines        1398     1672     +274     
  Branches      211      232      +21     
==========================================
+ Hits         1277     1519     +242     
- Misses        120      151      +31     
- Partials        1        2       +1     ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
  | 
    
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
defineStoreand does not containacceptHMRUpdate, then walks the top level nodes of the parsed code AST to find the export/variable declaration which uses thedefineStorefunction. 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:
// my-store.ts export const myStore = defineStore('my-store', /* ... */) +if (import.meta.hot) { + import.meta.hot.accept( + acceptHMRUpdate(myStore, import.meta.hot) + ) +}Summary by CodeRabbit