Skip to content

Commit 5cf66cf

Browse files
committed
feat: use pinia instance from nuxt only in dev mode
1 parent 1f3d17a commit 5cf66cf

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

packages/nuxt/global.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Global compile-time constants
2+
declare var __TEST__: boolean

packages/nuxt/src/runtime/composables.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,24 @@ export * from 'pinia'
88

99
export const usePinia = () => useNuxtApp().$pinia as Pinia | undefined
1010

11-
export const defineStore: typeof _defineStore = (
12-
...args: [idOrOptions: any, setup?: any, setupOptions?: any]
13-
) => {
14-
if (!import.meta.server) {
15-
return _defineStore(...args)
16-
}
11+
export const defineStore: typeof _defineStore =
12+
process.env.NODE_ENV === 'production' && !__TEST__
13+
? _defineStore
14+
: (...args: [idOrOptions: any, setup?: any, setupOptions?: any]) => {
15+
if (!import.meta.server) {
16+
return _defineStore(...args)
17+
}
1718

18-
const originalUseStore = _defineStore(...args)
19-
function useStore(pinia?: Pinia | null, hot?: StoreGeneric): StoreGeneric {
20-
return originalUseStore(pinia || usePinia(), hot)
21-
}
19+
const originalUseStore = _defineStore(...args)
20+
function useStore(
21+
pinia?: Pinia | null,
22+
hot?: StoreGeneric
23+
): StoreGeneric {
24+
return originalUseStore(pinia || usePinia(), hot)
25+
}
2226

23-
useStore.$id = originalUseStore.$id
24-
useStore._pinia = originalUseStore._pinia
27+
useStore.$id = originalUseStore.$id
28+
useStore._pinia = originalUseStore._pinia
2529

26-
return useStore
27-
}
30+
return useStore
31+
}

packages/nuxt/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"extends": "./playground/.nuxt/tsconfig.json",
33
"include": [
44
"./shims.d.ts",
5+
"./global.d.ts",
56
// missing in the playground
67
"./src"
78
]

0 commit comments

Comments
 (0)