mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-30 23:32:38 +00:00
feat(nuxt3): extends support for server/
directory (#3673)
This commit is contained in:
parent
fa8daefb6e
commit
227ceedebb
1
examples/config-extends/base/server/api/base.ts
Normal file
1
examples/config-extends/base/server/api/base.ts
Normal file
@ -0,0 +1 @@
|
||||
export default () => 'base'
|
1
examples/config-extends/server/api/hello.ts
Normal file
1
examples/config-extends/server/api/hello.ts
Normal file
@ -0,0 +1 @@
|
||||
export default () => 'hello'
|
@ -93,7 +93,15 @@ export async function writeTypes (nitroContext: NitroContext) {
|
||||
}
|
||||
|
||||
async function _build (nitroContext: NitroContext) {
|
||||
nitroContext.scannedMiddleware = await scanMiddleware(nitroContext._nuxt.serverDir)
|
||||
const serverDirs = [
|
||||
...nitroContext._extends.map(layer => layer.serverDir),
|
||||
nitroContext._nuxt.serverDir
|
||||
]
|
||||
|
||||
nitroContext.scannedMiddleware = (
|
||||
await Promise.all(serverDirs.map(async dir => await scanMiddleware(dir)))
|
||||
).flat().sort((a, b) => b.route.localeCompare(a.route))
|
||||
|
||||
await writeTypes(nitroContext)
|
||||
|
||||
logger.start('Building server...')
|
||||
@ -172,15 +180,23 @@ function startRollupWatcher (nitroContext: NitroContext) {
|
||||
async function _watch (nitroContext: NitroContext) {
|
||||
let watcher = startRollupWatcher(nitroContext)
|
||||
|
||||
nitroContext.scannedMiddleware = await scanMiddleware(nitroContext._nuxt.serverDir,
|
||||
(middleware, event) => {
|
||||
nitroContext.scannedMiddleware = middleware
|
||||
if (['add', 'addDir'].includes(event)) {
|
||||
watcher.close()
|
||||
writeTypes(nitroContext).catch(console.error)
|
||||
watcher = startRollupWatcher(nitroContext)
|
||||
const serverDirs = [
|
||||
...nitroContext._extends.map(layer => layer.serverDir),
|
||||
nitroContext._nuxt.serverDir
|
||||
]
|
||||
|
||||
nitroContext.scannedMiddleware = (
|
||||
await Promise.all(serverDirs.map(async dir => await scanMiddleware(dir,
|
||||
(middleware, event) => {
|
||||
nitroContext.scannedMiddleware = middleware
|
||||
if (['add', 'addDir'].includes(event)) {
|
||||
watcher.close()
|
||||
writeTypes(nitroContext).catch(console.error)
|
||||
watcher = startRollupWatcher(nitroContext)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
)))
|
||||
).flat().sort((a, b) => b.route.localeCompare(a.route))
|
||||
|
||||
await writeTypes(nitroContext)
|
||||
}
|
||||
|
@ -83,7 +83,10 @@ export interface NitroContext {
|
||||
_internal: {
|
||||
runtimeDir: string
|
||||
hooks: Hookable<NitroHooks>
|
||||
}
|
||||
},
|
||||
_extends: Array<{
|
||||
serverDir: string
|
||||
}>
|
||||
}
|
||||
|
||||
type DeepPartial<T> = T extends Record<string, any> ? { [P in keyof T]?: DeepPartial<T[P]> | T[P] } : T
|
||||
@ -154,7 +157,10 @@ export function getNitroContext (nuxtOptions: NuxtOptions, input: NitroInput): N
|
||||
_internal: {
|
||||
runtimeDir,
|
||||
hooks: createHooks<NitroHooks>()
|
||||
}
|
||||
},
|
||||
_extends: nuxtOptions._extends.map(layer => ({
|
||||
serverDir: resolve(layer.config.srcDir, (layer.config.dir as any)?.server || 'server')
|
||||
}))
|
||||
}
|
||||
|
||||
defaults.preset = input.preset || process.env.NITRO_PRESET || detectTarget() || 'server'
|
||||
|
@ -77,7 +77,15 @@ export function initNitro (nuxt: Nuxt) {
|
||||
})
|
||||
|
||||
nuxt.hook('build:before', async () => {
|
||||
nitroDevContext.scannedMiddleware = await scanMiddleware(nitroDevContext._nuxt.serverDir)
|
||||
const serverDirs = [
|
||||
...nitroDevContext._extends.map(layer => layer.serverDir),
|
||||
nitroDevContext._nuxt.serverDir
|
||||
]
|
||||
|
||||
nitroDevContext.scannedMiddleware = (
|
||||
await Promise.all(serverDirs.map(async dir => await scanMiddleware(dir)))
|
||||
).flat().sort((a, b) => b.route.localeCompare(a.route))
|
||||
|
||||
await writeTypes(nitroDevContext)
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user