feat(kit): add new addServerScanDir composable (#24001)

This commit is contained in:
Daniel Roe 2023-12-14 17:11:53 +00:00 committed by GitHub
parent 5877e11c89
commit c5ff169c3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 1 deletions

View File

@ -366,7 +366,7 @@ Add a directory to be scanned for auto-imports by Nitro.
### Type
```ts
function function addServerImportsDir (dirs: string | string[], opts: { prepend?: boolean }): void
function addServerImportsDir (dirs: string | string[], opts: { prepend?: boolean }): void
```
### Parameters
@ -395,3 +395,40 @@ export default defineNuxtModule({
}
})
```
## `addServerScanDir`
Add directories to be scanned by Nitro. It will check for subdirectories, which will be registered
just like the `~/server` folder is.
### Type
```ts
function addServerScanDir (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 for by Nitro as server dirs.
### Examples
```ts
import { defineNuxtModule, addServerScanDir } from '@nuxt/kit'
export default defineNuxtModule({
meta: {
name: 'my-module',
configKey: 'myModule',
},
setup(options) {
const resolver = createResolver(import.meta.url)
addServerScanDir(resolver.resolve('./runtime/server/routes'))
}
})
```

View File

@ -110,3 +110,18 @@ export function addServerImportsDir (dirs: string | string[], opts: { prepend?:
config.imports.dirs[opts.prepend ? 'unshift' : 'push'](..._dirs)
})
}
/**
* Add directories to be scanned by Nitro. It will check for subdirectories,
* which will be registered just like the `~/server` folder is.
*/
export function addServerScanDir (dirs: string | string[], opts: { prepend?: boolean } = {}) {
const nuxt = useNuxt()
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)
}
})
}