Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
feat(nuxt3): extends support for server/ directory (#3673)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmarrec authored Mar 15, 2022
1 parent fa8daef commit 227ceed
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
1 change: 1 addition & 0 deletions examples/config-extends/base/server/api/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => 'base'
1 change: 1 addition & 0 deletions examples/config-extends/server/api/hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => 'hello'
36 changes: 26 additions & 10 deletions packages/nitro/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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...')
Expand Down Expand Up @@ -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)
}
10 changes: 8 additions & 2 deletions packages/nitro/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down
10 changes: 9 additions & 1 deletion packages/nuxt3/src/core/nitro-legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand Down

0 comments on commit 227ceed

Please sign in to comment.