From 227ceedebb810f9bbe00556804ef3d386080ba5f Mon Sep 17 00:00:00 2001 From: Kevin Marrec Date: Tue, 15 Mar 2022 17:15:19 +0100 Subject: [PATCH] feat(nuxt3): extends support for `server/` directory (#3673) --- .../config-extends/base/server/api/base.ts | 1 + examples/config-extends/server/api/hello.ts | 1 + packages/nitro/src/build.ts | 36 +++++++++++++------ packages/nitro/src/context.ts | 10 ++++-- packages/nuxt3/src/core/nitro-legacy.ts | 10 +++++- 5 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 examples/config-extends/base/server/api/base.ts create mode 100644 examples/config-extends/server/api/hello.ts diff --git a/examples/config-extends/base/server/api/base.ts b/examples/config-extends/base/server/api/base.ts new file mode 100644 index 00000000000..b10fe1c156c --- /dev/null +++ b/examples/config-extends/base/server/api/base.ts @@ -0,0 +1 @@ +export default () => 'base' diff --git a/examples/config-extends/server/api/hello.ts b/examples/config-extends/server/api/hello.ts new file mode 100644 index 00000000000..5c4739d2b23 --- /dev/null +++ b/examples/config-extends/server/api/hello.ts @@ -0,0 +1 @@ +export default () => 'hello' diff --git a/packages/nitro/src/build.ts b/packages/nitro/src/build.ts index e6cdf5bc43e..343a6c658fc 100644 --- a/packages/nitro/src/build.ts +++ b/packages/nitro/src/build.ts @@ -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) } diff --git a/packages/nitro/src/context.ts b/packages/nitro/src/context.ts index bb13c97f22d..ea8b7da2273 100644 --- a/packages/nitro/src/context.ts +++ b/packages/nitro/src/context.ts @@ -83,7 +83,10 @@ export interface NitroContext { _internal: { runtimeDir: string hooks: Hookable - } + }, + _extends: Array<{ + serverDir: string + }> } type DeepPartial = T extends Record ? { [P in keyof T]?: DeepPartial | T[P] } : T @@ -154,7 +157,10 @@ export function getNitroContext (nuxtOptions: NuxtOptions, input: NitroInput): N _internal: { runtimeDir, hooks: createHooks() - } + }, + _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' diff --git a/packages/nuxt3/src/core/nitro-legacy.ts b/packages/nuxt3/src/core/nitro-legacy.ts index 353c3840493..ca00a104245 100644 --- a/packages/nuxt3/src/core/nitro-legacy.ts +++ b/packages/nuxt3/src/core/nitro-legacy.ts @@ -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) })