Skip to content
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

feat: automatic HMR code (nuxt only) #2954

Draft
wants to merge 2 commits into
base: v3
Choose a base branch
from

Conversation

BobbieGoede
Copy link

@BobbieGoede BobbieGoede commented Mar 23, 2025

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:

// my-store.ts
export const myStore = defineStore('my-store', /* ... */)

The plugin checks if it contains defineStore and does not contain acceptHMRUpdate, then walks the top level nodes of the parsed code AST to find the export/variable declaration which uses the defineStore 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:

// my-store.ts
export const myStore = defineStore('my-store', /* ... */)
+if (import.meta.hot) {
+  import.meta.hot.accept(
+    acceptHMRUpdate(myStore, import.meta.hot)
+  )
+}

Copy link

netlify bot commented Mar 23, 2025

Deploy Preview for pinia-official canceled.

Name Link
🔨 Latest commit 36f8a4b
🔍 Latest deploy log https://app.netlify.com/sites/pinia-official/deploys/67e07dd45a16de00080f7d81

@BobbieGoede BobbieGoede changed the title feat: automatic hmr code for nuxt module feat: automatic HMR code (nuxt only) Mar 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant