Skip to content

Commit e25e525

Browse files
rijkvanzantenposva
andauthored
fix(nuxt): resolve auto-imports in layers (#3035)
Co-authored-by: Eduardo San Martin Morote <[email protected]>
1 parent 868f6b5 commit e25e525

File tree

7 files changed

+53
-9
lines changed

7 files changed

+53
-9
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
]
7777
},
7878
"resolutions": {
79-
"@nuxt/kit": "^3.9.0",
8079
"@nuxt/schema": "^3.9.0"
8180
},
8281
"pnpm": {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default defineNuxtConfig({})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const useBasicStore = defineStore('layer-basic', () => {
2+
const count = ref(0)
3+
4+
return { count }
5+
})

packages/nuxt/playground/pages/index.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
44
const counter = useCounter()
55
6-
useTestStore()
7-
useSomeStoreStore()
6+
useTestStore() // ~/domain/one/stores/testStore.ts
7+
useSomeStoreStore() // ~/stores/nested/some-stores.ts
8+
const layerStore = useBasicStore() // ~~/layers/layer-domain/stores/basic.ts
89
910
// await useAsyncData('counter', () => counter.asyncIncrement().then(() => true))
1011
@@ -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 store: {{ layerStore.count }}</p>
2023
</div>
2124
</template>

packages/nuxt/src/module.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
addImports,
88
createResolver,
99
addImportsDir,
10+
getLayerDirectories,
1011
} from '@nuxt/kit'
1112
import type { NuxtModule } from '@nuxt/schema'
1213
import { fileURLToPath } from 'node:url'
@@ -73,8 +74,12 @@ const module: NuxtModule<ModuleOptions> = defineNuxtModule<ModuleOptions>({
7374
}
7475

7576
if (options.storesDirs) {
77+
const layers = getLayerDirectories(nuxt)
78+
7679
for (const storeDir of options.storesDirs) {
77-
addImportsDir(resolve(nuxt.options.rootDir, storeDir))
80+
for (const layer of layers) {
81+
addImportsDir(resolve(layer.app, storeDir))
82+
}
7883
}
7984
}
8085
},

packages/nuxt/test/nuxt.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,8 @@ describe('Nuxt', async () => {
3333
expect(html).not.toContain('I should not be serialized or hydrated')
3434
expect(html).toContain('skipHydrate-wrapped state is correct')
3535
})
36+
37+
it('can auto import from layers', async () => {
38+
expect(await $fetch('/')).toContain('Layer store: 0')
39+
})
3640
})

pnpm-lock.yaml

Lines changed: 32 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)