-
-
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
Merged
posva
merged 7 commits into
vuejs:v3
from
BobbieGoede:feat/nuxt-automatic-hmr-code-append
Nov 4, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8a65eb7
chore: up nuxt 4
posva 41ef45c
feat: add auto hmr registration plugin to nuxt module
BobbieGoede 1a13067
chore: update playground to demonstrate plugin works
BobbieGoede aaab95f
refactor: import types
posva 10c0cc3
chore: playground
posva 6c1f66e
fix: add import so it works with autoimports: false
posva 0b05246
refactor: simplify rootDir option
posva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import type { VariableDeclarator } from 'estree' | ||
| import type { Nuxt } from 'nuxt/schema' | ||
| import type { Plugin } from 'vite' | ||
|
|
||
| function getStoreDeclaration(nodes?: VariableDeclarator[]) { | ||
| return nodes?.find( | ||
| (x) => | ||
| x.init?.type === 'CallExpression' && | ||
| x.init.callee.type === 'Identifier' && | ||
| x.init.callee.name === 'defineStore' | ||
| ) | ||
| } | ||
|
|
||
| function nameFromDeclaration(node?: VariableDeclarator) { | ||
| return node?.id.type === 'Identifier' && node.id.name | ||
| } | ||
|
|
||
| export function autoRegisterHMRPlugin(rootDir: string) { | ||
| return { | ||
| name: 'pinia:auto-hmr-registration', | ||
|
|
||
| transform(code, id) { | ||
| if (id.startsWith('\x00')) return | ||
| if (!id.startsWith(rootDir)) return | ||
| if (!code.includes('defineStore') || code.includes('acceptHMRUpdate')) { | ||
| return | ||
| } | ||
|
|
||
| const ast = this.parse(code) | ||
|
|
||
| // walk top-level nodes | ||
| for (const n of ast.body) { | ||
| if ( | ||
| n.type === 'VariableDeclaration' || | ||
| n.type === 'ExportNamedDeclaration' | ||
| ) { | ||
| // find export or variable declaration that uses `defineStore` | ||
| const storeDeclaration = getStoreDeclaration( | ||
| n.type === 'VariableDeclaration' | ||
| ? n.declarations | ||
| : n.declaration?.type === 'VariableDeclaration' | ||
| ? n.declaration?.declarations | ||
| : undefined | ||
| ) | ||
|
|
||
| // retrieve the variable name | ||
| const storeName = nameFromDeclaration(storeDeclaration) | ||
| if (storeName) { | ||
| // append HMR code | ||
| return { | ||
| code: [ | ||
| `import { acceptHMRUpdate } from 'pinia'`, | ||
| code, | ||
| 'if (import.meta.hot) {', | ||
| ` import.meta.hot.accept(acceptHMRUpdate(${storeName}, import.meta.hot))`, | ||
| '}', | ||
| ].join('\n'), | ||
| } | ||
posva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
| }, | ||
| } satisfies Plugin | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this so it works with
autoImports: false. Let me know I overlooked anythingUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay, these changes look good to me.
Might be worth checking if this works (or causes issues) with projects using webpack/rspack or if those needs some special treatment.Nevermind, I just noticed the changelog says vite only and this is only added a vite plugin. I could look into making this work for those webpack/rspack if there's demand for it though.