From ebb5767b9cfc2fd0d5cd45ae1a2fc1350f9f3dff Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 31 Oct 2023 14:16:01 +0100 Subject: [PATCH] fix(kit): fix `addServerImportsDir` implementation (#24000) --- docs/3.api/5.kit/11.nitro.md | 37 +++++++++++++++++++ packages/kit/src/nitro.ts | 11 +++--- .../basic/modules/auto-registered/index.ts | 2 +- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/docs/3.api/5.kit/11.nitro.md b/docs/3.api/5.kit/11.nitro.md index 535ad03473..22f793595f 100644 --- a/docs/3.api/5.kit/11.nitro.md +++ b/docs/3.api/5.kit/11.nitro.md @@ -358,3 +358,40 @@ export default defineNuxtModule({ } }) ``` + +## `addServerImportsDir` + +Add a directory to be scanned for auto-imports by Nitro. + +### Type + +```ts +function function addServerImportsDir (dirs: string | string[], opts: { prepend?: boolean }): void +``` + +### Parameters + +#### `dirs` + +**Type**: `string | string[]` + +**Required**: `true` + +A directory or an array of directories to register to be scanned by Nitro + +### Examples + +```ts +import { defineNuxtModule, addServerImportsDir } from '@nuxt/kit' + +export default defineNuxtModule({ + meta: { + name: 'my-module', + configKey: 'myModule', + }, + setup(options) { + const resolver = createResolver(import.meta.url) + addServerImportsDir(resolver.resolve('./runtime/server/utils')) + } +}) +``` diff --git a/packages/kit/src/nitro.ts b/packages/kit/src/nitro.ts index be642efc4b..124b3bded2 100644 --- a/packages/kit/src/nitro.ts +++ b/packages/kit/src/nitro.ts @@ -99,15 +99,14 @@ export function addServerImports (imports: Import[]) { } /** - * Add directories to be scanned by Nitro + * Add directories to be scanned for auto-imports by Nitro */ export function addServerImportsDir (dirs: string | string[], opts: { prepend?: boolean } = {}) { const nuxt = useNuxt() + const _dirs = Array.isArray(dirs) ? dirs : [dirs] nuxt.hook('nitro:config', (config) => { - config.scanDirs = config.scanDirs || [] - - for (const dir of (Array.isArray(dirs) ? dirs : [dirs])) { - config.scanDirs[opts.prepend ? 'unshift' : 'push'](dir) - } + config.imports = config.imports || {} + config.imports.dirs = config.imports.dirs || [] + config.imports.dirs[opts.prepend ? 'unshift' : 'push'](..._dirs) }) } diff --git a/test/fixtures/basic/modules/auto-registered/index.ts b/test/fixtures/basic/modules/auto-registered/index.ts index a254abaad1..f0e5a5b00a 100644 --- a/test/fixtures/basic/modules/auto-registered/index.ts +++ b/test/fixtures/basic/modules/auto-registered/index.ts @@ -21,6 +21,6 @@ export default defineNuxtModule({ name: 'someUtils' }]) - addServerImportsDir(resolver.resolve('./runtime/server')) + addServerImportsDir(resolver.resolve('./runtime/server/utils')) } })