Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kit): fix addServerImportsDir implementation #24000

Merged
merged 3 commits into from Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions docs/3.api/5.kit/11.nitro.md
Expand Up @@ -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'))
}
})
```
11 changes: 5 additions & 6 deletions packages/kit/src/nitro.ts
Expand Up @@ -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 || {}
danielroe marked this conversation as resolved.
Show resolved Hide resolved
config.imports.dirs = config.imports.dirs || []
config.imports.dirs[opts.prepend ? 'unshift' : 'push'](..._dirs)
})
}
2 changes: 1 addition & 1 deletion test/fixtures/basic/modules/auto-registered/index.ts
Expand Up @@ -21,6 +21,6 @@ export default defineNuxtModule({
name: 'someUtils'
}])

addServerImportsDir(resolver.resolve('./runtime/server'))
addServerImportsDir(resolver.resolve('./runtime/server/utils'))
}
})