mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-29 09:02:03 +00:00
feat(kit): add new addServerScanDir
composable (#24001)
This commit is contained in:
parent
5877e11c89
commit
c5ff169c3a
@ -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'))
|
||||
}
|
||||
})
|
||||
```
|
||||
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user