Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit de81a4e

Browse files
committedFeb 27, 2025
feat(nuxt): automatically register stores dir in all layers
1 parent 6a02b7a commit de81a4e

File tree

6 files changed

+31
-2
lines changed

6 files changed

+31
-2
lines changed
 
+11
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+
})
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/nuxt.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default defineNuxtConfig({
1313
telemetry: {
1414
enabled: false,
1515
},
16+
extends: ['../example-layer'],
1617

1718
pinia: {
1819
storesDirs: ['./stores/**', './domain/*/stores'],

‎packages/nuxt/playground/pages/index.vue

+3
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/src/module.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ const module: NuxtModule<ModuleOptions> = defineNuxtModule<ModuleOptions>({
6767
])
6868

6969
if (!options.storesDirs) {
70-
// resolve it against the src dir which is the root by default
71-
options.storesDirs = [resolve(nuxt.options.srcDir, 'stores')]
70+
// Add stores directory for each layer, including the main src dir
71+
options.storesDirs = []
72+
for (const layer of nuxt.options._layers) {
73+
console.log(layer)
74+
options.storesDirs.push(resolve(layer.config.srcDir, 'stores'))
75+
}
7276
}
7377

7478
if (options.storesDirs) {

‎packages/nuxt/test/nuxt.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe('works with nuxt', async () => {
2626
it('works on ssr', async () => {
2727
const html = await $fetch('/')
2828
expect(html).toContain('Count: 101')
29+
expect(html).toContain('Layer: store state')
2930
})
3031

3132
it('drops state that is marked with skipHydrate', async () => {

0 commit comments

Comments
 (0)
Please sign in to comment.