feat(nuxt3): extends support for server/ directory (#3673)

This commit is contained in:
Kevin Marrec 2022-03-15 17:15:19 +01:00 committed by GitHub
parent fa8daefb6e
commit 227ceedebb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 13 deletions

View File

@ -0,0 +1 @@
export default () => 'base'

View File

@ -0,0 +1 @@
export default () => 'hello'

View File

@ -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,7 +180,13 @@ function startRollupWatcher (nitroContext: NitroContext) {
async function _watch (nitroContext: NitroContext) {
let watcher = startRollupWatcher(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,
(middleware, event) => {
nitroContext.scannedMiddleware = middleware
if (['add', 'addDir'].includes(event)) {
@ -181,6 +195,8 @@ async function _watch (nitroContext: NitroContext) {
watcher = startRollupWatcher(nitroContext)
}
}
)
)))
).flat().sort((a, b) => b.route.localeCompare(a.route))
await writeTypes(nitroContext)
}

View File

@ -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'

View File

@ -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)
})