diff --git a/packages/nuxt/src/core/app.ts b/packages/nuxt/src/core/app.ts index bb6b6e6925..4a3bbca8fb 100644 --- a/packages/nuxt/src/core/app.ts +++ b/packages/nuxt/src/core/app.ts @@ -108,7 +108,8 @@ async function resolveApp (nuxt: Nuxt, app: NuxtApp) { // Resolve layouts/ from all config layers app.layouts = {} for (const config of nuxt.options._layers.map(layer => layer.config)) { - const layoutFiles = await resolveFiles(config.srcDir, `${config.dir?.layouts || 'layouts'}/*{${nuxt.options.extensions.join(',')}}`) + const layoutDir = (config.rootDir === nuxt.options.rootDir ? nuxt.options : config).dir?.layouts || 'layouts' + const layoutFiles = await resolveFiles(config.srcDir, `${layoutDir}/*{${nuxt.options.extensions.join(',')}}`) for (const file of layoutFiles) { const name = getNameFromPath(file) app.layouts[name] = app.layouts[name] || { name, file } @@ -118,7 +119,8 @@ async function resolveApp (nuxt: Nuxt, app: NuxtApp) { // Resolve middleware/ from all config layers app.middleware = [] for (const config of nuxt.options._layers.map(layer => layer.config)) { - const middlewareFiles = await resolveFiles(config.srcDir, `${config.dir?.middleware || 'middleware'}/*{${nuxt.options.extensions.join(',')}}`) + const middlewareDir = (config.rootDir === nuxt.options.rootDir ? nuxt.options : config).dir?.middleware || 'middleware' + const middlewareFiles = await resolveFiles(config.srcDir, `${middlewareDir}/*{${nuxt.options.extensions.join(',')}}`) app.middleware.push(...middlewareFiles.map((file) => { const name = getNameFromPath(file) return { name, path: file, global: hasSuffix(file, '.global') } @@ -128,12 +130,13 @@ async function resolveApp (nuxt: Nuxt, app: NuxtApp) { // Resolve plugins, first extended layers and then base app.plugins = [] for (const config of nuxt.options._layers.map(layer => layer.config).reverse()) { + const pluginDir = (config.rootDir === nuxt.options.rootDir ? nuxt.options : config).dir?.plugins || 'plugins' app.plugins.push(...[ ...(config.plugins || []), ...config.srcDir ? await resolveFiles(config.srcDir, [ - `${config.dir?.plugins || 'plugins'}/*.{ts,js,mjs,cjs,mts,cts}`, - `${config.dir?.plugins || 'plugins'}/*/index.*{ts,js,mjs,cjs,mts,cts}` // TODO: remove, only scan top-level plugins #18418 + `${pluginDir}/*.{ts,js,mjs,cjs,mts,cts}`, + `${pluginDir}/*/index.*{ts,js,mjs,cjs,mts,cts}` // TODO: remove, only scan top-level plugins #18418 ]) : [] ].map(plugin => normalizePlugin(plugin as NuxtPlugin))) diff --git a/packages/nuxt/src/core/nitro.ts b/packages/nuxt/src/core/nitro.ts index f17a253389..abe8ce002c 100644 --- a/packages/nuxt/src/core/nitro.ts +++ b/packages/nuxt/src/core/nitro.ts @@ -121,7 +121,7 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) { baseURL: nuxt.options.app.buildAssetsDir }, ...nuxt.options._layers - .map(layer => join(layer.config.srcDir, layer.config.dir?.public || 'public')) + .map(layer => join(layer.config.srcDir, (layer.config.rootDir === nuxt.options.rootDir ? nuxt.options : layer.config).dir?.public || 'public')) .filter(dir => existsSync(dir)) .map(dir => ({ dir })) ], diff --git a/packages/nuxt/src/core/nuxt.ts b/packages/nuxt/src/core/nuxt.ts index 03b4f0a398..150217f046 100644 --- a/packages/nuxt/src/core/nuxt.ts +++ b/packages/nuxt/src/core/nuxt.ts @@ -190,9 +190,10 @@ async function initNuxt (nuxt: Nuxt) { // Automatically register user modules for (const config of nuxt.options._layers.map(layer => layer.config).reverse()) { + const modulesDir = (config.rootDir === nuxt.options.rootDir ? nuxt.options : config).dir?.modules || 'modules' const layerModules = await resolveFiles(config.srcDir, [ - `${config.dir?.modules || 'modules'}/*{${nuxt.options.extensions.join(',')}}`, - `${config.dir?.modules || 'modules'}/*/index{${nuxt.options.extensions.join(',')}}` + `${modulesDir}/*{${nuxt.options.extensions.join(',')}}`, + `${modulesDir}/*/index{${nuxt.options.extensions.join(',')}}` ]) for (const mod of layerModules) { watchedPaths.add(mod) diff --git a/packages/nuxt/src/pages/module.ts b/packages/nuxt/src/pages/module.ts index b6c0f5f784..c0ef5a0bc0 100644 --- a/packages/nuxt/src/pages/module.ts +++ b/packages/nuxt/src/pages/module.ts @@ -28,7 +28,7 @@ export default defineNuxtModule({ const useExperimentalTypedPages = nuxt.options.experimental.typedPages const pagesDirs = nuxt.options._layers.map( - layer => resolve(layer.config.srcDir, layer.config.dir?.pages || 'pages') + layer => resolve(layer.config.srcDir, (layer.config.rootDir === nuxt.options.rootDir ? nuxt.options : layer.config).dir?.pages || 'pages') ) // Disable module (and use universal router) if pages dir do not exists or user has disabled it @@ -54,10 +54,13 @@ export default defineNuxtModule({ nuxt.options.pages = await isPagesEnabled() // Restart Nuxt when pages dir is added or removed - const restartPaths = nuxt.options._layers.flatMap(layer => [ - join(layer.config.srcDir || layer.cwd, 'app/router.options.ts'), - join(layer.config.srcDir || layer.cwd, layer.config.dir?.pages || 'pages') - ]) + const restartPaths = nuxt.options._layers.flatMap((layer) => { + const pagesDir = (layer.config.rootDir === nuxt.options.rootDir ? nuxt.options : layer.config).dir?.pages || 'pages' + return [ + join(layer.config.srcDir || layer.cwd, 'app/router.options.ts'), + join(layer.config.srcDir || layer.cwd, pagesDir) + ] + }) nuxt.hooks.hook('builder:watch', async (event, relativePath) => { const path = resolve(nuxt.options.srcDir, relativePath) @@ -178,11 +181,14 @@ export default defineNuxtModule({ }) // Regenerate templates when adding or removing pages - const updateTemplatePaths = nuxt.options._layers.flatMap(l => [ - join(l.config.srcDir || l.cwd, l.config.dir?.pages || 'pages') + '/', - join(l.config.srcDir || l.cwd, l.config.dir?.layouts || 'layouts') + '/', - join(l.config.srcDir || l.cwd, l.config.dir?.middleware || 'middleware') + '/' - ]) + const updateTemplatePaths = nuxt.options._layers.flatMap((l) => { + const dir = (l.config.rootDir === nuxt.options.rootDir ? nuxt.options : l.config).dir + return [ + join(l.config.srcDir || l.cwd, dir?.pages || 'pages') + '/', + join(l.config.srcDir || l.cwd, dir?.layouts || 'layouts') + '/', + join(l.config.srcDir || l.cwd, dir?.middleware || 'middleware') + '/' + ] + }) nuxt.hook('builder:watch', async (event, relativePath) => { if (event === 'change') { return } diff --git a/packages/nuxt/src/pages/utils.ts b/packages/nuxt/src/pages/utils.ts index f745c30309..d07233850e 100644 --- a/packages/nuxt/src/pages/utils.ts +++ b/packages/nuxt/src/pages/utils.ts @@ -42,7 +42,7 @@ export async function resolvePagesRoutes (): Promise { const nuxt = useNuxt() const pagesDirs = nuxt.options._layers.map( - layer => resolve(layer.config.srcDir, layer.config.dir?.pages || 'pages') + layer => resolve(layer.config.srcDir, (layer.config.rootDir === nuxt.options.rootDir ? nuxt.options : layer.config).dir?.pages || 'pages') ) const scannedFiles: ScannedFile[] = []