From: Eduardo San Martin Morote Date: Mon, 31 Jul 2023 06:59:19 +0000 (+0200) Subject: feat(imports): add storeDirs auto import X-Git-Tag: @pinia/nuxt@0.5.0~50 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=70a95ba9b1d6a55aeea72088dfedd478aa4db766;p=thirdparty%2Fvuejs%2Fpinia.git feat(imports): add storeDirs auto import 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. --- diff --git a/packages/nuxt/src/module.ts b/packages/nuxt/src/module.ts index 86d4914f..72d47337 100644 --- a/packages/nuxt/src/module.ts +++ b/packages/nuxt/src/module.ts @@ -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 + storesDirs?: string[] } const module: NuxtModule = defineNuxtModule({ @@ -48,7 +41,7 @@ const module: NuxtModule = defineNuxtModule({ }, defaults: { disableVuex: true, - autoImports: [], + storesDirs: ['./stores'], }, setup(options, nuxt) { const resolver = createResolver(import.meta.url) @@ -71,6 +64,7 @@ const module: NuxtModule = defineNuxtModule({ // 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 = defineNuxtModule({ // 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)) + } + } }, })