Skip to content

Commit 2418e52

Browse files
committed
feat(nuxt): automatically register stores dir in all layers
1 parent 70d9b66 commit 2418e52

File tree

6 files changed

+32
-3
lines changed

6 files changed

+32
-3
lines changed

packages/nuxt/__tests__/nuxt.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ describe('works with nuxt', async () => {
3232
it('works on ssr', async () => {
3333
const html = await $fetch('/')
3434
expect(html).toContain('Count: 101')
35+
expect(html).toContain('Layer: store state')
3536
})
3637
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { fileURLToPath } from 'node:url'
2+
import { defineNuxtConfig } from 'nuxt/config'
3+
import { dirname, join } from 'path'
4+
5+
const currentDir = dirname(fileURLToPath(import.meta.url))
6+
7+
export default defineNuxtConfig({
8+
pinia: {
9+
storesDirs: [join(currentDir, './stores/**')],
10+
},
11+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ref } from 'vue'
2+
3+
export const useLayerStore = defineStore('layerStore', () => {
4+
console.log('I was defined within a stores directory in example-layer')
5+
const state = ref('store state')
6+
return {
7+
state,
8+
}
9+
})

packages/nuxt/playground/app.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const counter = useCounter()
66
useTestStore()
77
useSomeStoreStore()
88
9+
const layerStore = useLayerStore()
910
// await useAsyncData('counter', () => counter.asyncIncrement().then(() => true))
1011
1112
if (import.meta.server) {
@@ -17,5 +18,7 @@ if (import.meta.server) {
1718
<div>
1819
<p>Count: {{ counter.$state.count }}</p>
1920
<button @click="counter.increment()">+</button>
21+
22+
<p>Layer: {{ layerStore.state }}</p>
2023
</div>
2124
</template>

packages/nuxt/playground/nuxt.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export default defineNuxtConfig({
88
},
99
modules: [piniaModule],
1010

11+
extends: ['../example-layer'],
12+
1113
pinia: {
1214
storesDirs: ['./stores/**', './domain/*/stores'],
1315
},

packages/nuxt/src/module.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,12 @@ const module: NuxtModule<ModuleOptions> = defineNuxtModule<ModuleOptions>({
9191
{ from: composables, name: 'storeToRefs' },
9292
])
9393

94-
if (!options.storesDirs) {
95-
// resolve it against the src dir which is the root by default
96-
options.storesDirs = [resolver.resolve(nuxt.options.srcDir, 'stores')]
94+
if (options.storesDirs == null) {
95+
// Add stores directory for each layer, including the main src dir
96+
options.storesDirs = []
97+
for (const layer of nuxt.options._layers) {
98+
options.storesDirs.push(resolver.resolve(layer.config.srcDir, 'stores'))
99+
}
97100
}
98101

99102
if (options.storesDirs) {

0 commit comments

Comments
 (0)