Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@
"prettier --parser=typescript --write"
]
},
"resolutions": {
"@nuxt/schema": "^3.9.0"
},
"pnpm": {
"onlyBuiltDependencies": [
"esbuild",
Expand Down
6 changes: 3 additions & 3 deletions packages/nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path . -l @pinia/nuxt -r 1"
},
"dependencies": {
"@nuxt/kit": "^3.20.0"
"@nuxt/kit": "^4.2.0"
},
"peerDependencies": {
"pinia": "workspace:^"
},
"devDependencies": {
"@nuxt/module-builder": "1.0.2",
"@nuxt/schema": "^3.20.0",
"@nuxt/schema": "^4.2.0",
"@nuxt/test-utils": "^3.20.1",
"nuxt": "^3.20.0",
"nuxt": "^4.2.0",
"pinia": "workspace:^",
"typescript": "^5.9.3",
"vue-tsc": "^3.1.3"
Expand Down
5 changes: 4 additions & 1 deletion packages/nuxt/playground/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ if (import.meta.server) {

<template>
<div>
<p>Count: {{ counter.$state.count }}</p>
<p>Count: {{ counter.count }} x 2 = {{ counter.double }}</p>
<button @click="counter.increment()">+</button>
<pre>{{ counter.$state }}</pre>

<hr />

<p>Layer store: {{ layerStore.count }}</p>
</div>
Expand Down
6 changes: 1 addition & 5 deletions packages/nuxt/playground/stores/counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ export const useCounter = defineStore('counter', {
},
},
getters: {
getCount: (state) => state.count,
double: (state) => state.count * 2,
},
})

if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useCounter, import.meta.hot))
}
4 changes: 0 additions & 4 deletions packages/nuxt/playground/stores/nested/some-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,3 @@ export const useSomeStoreStore = defineStore('some-store', () => {
// console.log('I was defined within a nested store directory')
return {}
})

if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useSomeStoreStore, import.meta.hot))
}
6 changes: 0 additions & 6 deletions packages/nuxt/playground/stores/with-skip-hydrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,3 @@ export const useWithSkipHydrateStore = defineStore('with-skip-hydrate', () => {
)
return { skipped }
})

if (import.meta.hot) {
import.meta.hot.accept(
acceptHMRUpdate(useWithSkipHydrateStore, import.meta.hot)
)
}
64 changes: 64 additions & 0 deletions packages/nuxt/src/auto-hmr-plugin.ts
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'`,
Copy link
Member

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 anything

Copy link
Contributor Author

@BobbieGoede BobbieGoede Nov 5, 2025

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.

code,
'if (import.meta.hot) {',
` import.meta.hot.accept(acceptHMRUpdate(${storeName}, import.meta.hot))`,
'}',
].join('\n'),
}
}
}
}
},
} satisfies Plugin
}
9 changes: 8 additions & 1 deletion packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import {
createResolver,
addImportsDir,
getLayerDirectories,
addVitePlugin,
} from '@nuxt/kit'
import type { NuxtModule } from '@nuxt/schema'
import { fileURLToPath } from 'node:url'
import { autoRegisterHMRPlugin } from './auto-hmr-plugin'

export interface ModuleOptions {
/**
* Automatically add stores dirs to the auto imports. This is the same as
* directly adding the dirs to the `imports.dirs` option. If you want to
* also import nested stores, you can use the glob pattern `./stores/**`
* also import nested stores, you can use the glob pattern `stores/**`
* (on Nuxt 3) or `app/stores/**` (on Nuxt 4+)
*
* @default `['stores']`
Expand Down Expand Up @@ -82,6 +84,11 @@ const module: NuxtModule<ModuleOptions> = defineNuxtModule<ModuleOptions>({
}
}
}

// Register automatic hmr code plugin - dev mode only
if (nuxt.options.dev) {
addVitePlugin(autoRegisterHMRPlugin(resolve(nuxt.options.rootDir)))
}
},
})

Expand Down
Loading
Loading