]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat(imports): add storeDirs auto import
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 31 Jul 2023 06:59:19 +0000 (08:59 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 31 Jul 2023 06:59:19 +0000 (08:59 +0200)
Close #1604

BREAKING CHANGE: the option `autoImports` has been removed as it offered
no value comparetd to the existing `imports` option in Nuxt. Instead, we
are automatically adding `defineStore()`, and `acceptHMRUpdate()` to the
list of auto imported functions. We are also adding the `./stores` dirs
to auto imports now, so if you were manually adding that option, it can
be removed.

packages/nuxt/src/module.ts

index 86d4914ffd0e12a04ab9a0d9f4a97361c411180c..72d473374d016c646f4f95c69cb6988ec9a371d5 100644 (file)
@@ -8,6 +8,7 @@ import {
   addImports,
   createResolver,
   resolveModule,
+  addImportsDir,
 } from '@nuxt/kit'
 import type { NuxtModule } from '@nuxt/schema'
 
@@ -21,20 +22,12 @@ export interface ModuleOptions {
   disableVuex?: boolean
 
   /**
-   * Array of auto imports to be added to the nuxt.config.js file.
-   *
-   * @example
-   * ```js
-   * autoImports: [
-   *  // automatically import `defineStore`
-   *  'defineStore',
-   *  // automatically import `defineStore` as `definePiniaStore`
-   *  ['defineStore', 'definePiniaStore',
-   * ]
-   * ```
+   * Automatically add stores dirs to the auto imports. This is the same as
+   * directly adding the dirs to the `imports.dirs` option.
    *
+   * @default `['./stores']`
    */
-  autoImports?: Array<string | [string, string]>
+  storesDirs?: string[]
 }
 
 const module: NuxtModule<ModuleOptions> = defineNuxtModule<ModuleOptions>({
@@ -48,7 +41,7 @@ const module: NuxtModule<ModuleOptions> = defineNuxtModule<ModuleOptions>({
   },
   defaults: {
     disableVuex: true,
-    autoImports: [],
+    storesDirs: ['./stores'],
   },
   setup(options, nuxt) {
     const resolver = createResolver(import.meta.url)
@@ -71,6 +64,7 @@ const module: NuxtModule<ModuleOptions> = defineNuxtModule<ModuleOptions>({
     // Make sure we use the mjs build for pinia
     nuxt.options.alias.pinia =
       nuxt.options.alias.pinia ||
+      // FIXME: remove this deprecated call. Ensure it works in Nuxt 2 to 3
       resolveModule('pinia/dist/pinia.mjs', {
         paths: [nuxt.options.rootDir, import.meta.url],
       })
@@ -92,13 +86,16 @@ const module: NuxtModule<ModuleOptions> = defineNuxtModule<ModuleOptions>({
     // Add auto imports
     const composables = resolver.resolve('./runtime/composables')
     addImports([
+      { from: composables, name: 'defineStore' },
+      { from: composables, name: 'acceptHMRUpdate' },
       { from: composables, name: 'usePinia' },
-      ...options.autoImports!.map((imports) =>
-        typeof imports === 'string'
-          ? { from: composables, name: imports }
-          : { from: composables, name: imports[0], as: imports[1] }
-      ),
     ])
+
+    if (options.storesDirs) {
+      for (const storeDir of options.storesDirs) {
+        addImportsDir(resolver.resolve(nuxt.options.rootDir, storeDir))
+      }
+    }
   },
 })