From dd907089a688742d609bfd24c182cb7b6d6df375 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Mon, 16 Oct 2023 11:46:35 +0200 Subject: [PATCH] fix(nuxt): use srcDir by default for storeDirs Fix #2447 --- .../docs/cookbook/hot-module-replacement.md | 2 +- packages/docs/ssr/nuxt.md | 20 +++++++++++-------- packages/nuxt/src/module.ts | 8 ++++++-- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/packages/docs/cookbook/hot-module-replacement.md b/packages/docs/cookbook/hot-module-replacement.md index 0f32c675..37bce74d 100644 --- a/packages/docs/cookbook/hot-module-replacement.md +++ b/packages/docs/cookbook/hot-module-replacement.md @@ -9,7 +9,7 @@ You need to add this snippet of code next to any store declaration. Let's say yo // auth.js import { defineStore, acceptHMRUpdate } from 'pinia' -const useAuth = defineStore('auth', { +export const useAuth = defineStore('auth', { // options... }) diff --git a/packages/docs/ssr/nuxt.md b/packages/docs/ssr/nuxt.md index 946e20fb..6d73bb0a 100644 --- a/packages/docs/ssr/nuxt.md +++ b/packages/docs/ssr/nuxt.md @@ -61,23 +61,27 @@ const { data } = await useAsyncData('user', () => store.fetchUser()) ## Auto imports -By default `@pinia/nuxt` exposes one single auto import: `usePinia()`, which is similar to `getActivePinia()` but works better with Nuxt. You can add auto imports to make your life easier: +By default `@pinia/nuxt` exposes a few auto imports: -```js -// nuxt.config.js +- `usePinia()`, which is similar to `getActivePinia()` but works better with Nuxt. You can add auto imports to make your life easier: +- `defineStore()` +- `acceptHMRUpdate()` for [hot module replacement](../cookbook/hot-module-replacement.md) + +It also automatically imports **all stores** defined withing your `stores` folder. It doesn't lookup for nested stores though. You can customize this behavior by setting the `storeDirs` option: + +```ts +// nuxt.config.ts export default defineNuxtConfig({ // ... other options modules: ['@pinia/nuxt'], pinia: { - autoImports: [ - // automatically imports `defineStore` - 'defineStore', // import { defineStore } from 'pinia' - ['defineStore', 'definePiniaStore'], // import { defineStore as definePiniaStore } from 'pinia' - ], + storeDirs: ['./stores/**', './custom-folder/stores/**'], }, }) ``` +Note the folders are relative to the root of your project. If you change the `srcDir` option, you need to adapt the paths accordingly. + ## Nuxt 2 without bridge Pinia supports Nuxt 2 until `@pinia/nuxt` v0.2.1. Make sure to also install [`@nuxtjs/composition-api`](https://composition-api.nuxtjs.org/) alongside `pinia`: diff --git a/packages/nuxt/src/module.ts b/packages/nuxt/src/module.ts index e02f6117..b373d4c4 100644 --- a/packages/nuxt/src/module.ts +++ b/packages/nuxt/src/module.ts @@ -26,7 +26,7 @@ export interface ModuleOptions { * directly adding the dirs to the `imports.dirs` option. If you want to * also import nested stores, you can use the glob pattern `./stores/**` * - * @default `['./stores']` + * @default `['stores']` */ storesDirs?: string[] } @@ -42,7 +42,6 @@ const module: NuxtModule = defineNuxtModule({ }, defaults: { disableVuex: true, - storesDirs: ['./stores'], }, setup(options, nuxt) { const resolver = createResolver(import.meta.url) @@ -92,6 +91,11 @@ const module: NuxtModule = defineNuxtModule({ { from: composables, name: 'usePinia' }, ]) + if (!options.storesDirs) { + // resolve it against the src dir which is the root by default + options.storesDirs = [resolver.resolve(nuxt.options.srcDir, 'stores')] + } + if (options.storesDirs) { for (const storeDir of options.storesDirs) { addImportsDir(resolver.resolve(nuxt.options.rootDir, storeDir)) -- 2.47.2