fix(nuxt): allow changing dirs within modules (#23133)

This commit is contained in:
Daniel Roe 2023-09-12 15:27:28 +01:00 committed by GitHub
parent 842a2e5cf9
commit 9892d5cd3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 18 deletions

View File

@ -108,7 +108,8 @@ async function resolveApp (nuxt: Nuxt, app: NuxtApp) {
// Resolve layouts/ from all config layers // Resolve layouts/ from all config layers
app.layouts = {} app.layouts = {}
for (const config of nuxt.options._layers.map(layer => layer.config)) { 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) { for (const file of layoutFiles) {
const name = getNameFromPath(file) const name = getNameFromPath(file)
app.layouts[name] = app.layouts[name] || { name, 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 // Resolve middleware/ from all config layers
app.middleware = [] app.middleware = []
for (const config of nuxt.options._layers.map(layer => layer.config)) { 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) => { app.middleware.push(...middlewareFiles.map((file) => {
const name = getNameFromPath(file) const name = getNameFromPath(file)
return { name, path: file, global: hasSuffix(file, '.global') } 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 // Resolve plugins, first extended layers and then base
app.plugins = [] app.plugins = []
for (const config of nuxt.options._layers.map(layer => layer.config).reverse()) { 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(...[ app.plugins.push(...[
...(config.plugins || []), ...(config.plugins || []),
...config.srcDir ...config.srcDir
? await resolveFiles(config.srcDir, [ ? await resolveFiles(config.srcDir, [
`${config.dir?.plugins || 'plugins'}/*.{ts,js,mjs,cjs,mts,cts}`, `${pluginDir}/*.{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}/*/index.*{ts,js,mjs,cjs,mts,cts}` // TODO: remove, only scan top-level plugins #18418
]) ])
: [] : []
].map(plugin => normalizePlugin(plugin as NuxtPlugin))) ].map(plugin => normalizePlugin(plugin as NuxtPlugin)))

View File

@ -121,7 +121,7 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
baseURL: nuxt.options.app.buildAssetsDir baseURL: nuxt.options.app.buildAssetsDir
}, },
...nuxt.options._layers ...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)) .filter(dir => existsSync(dir))
.map(dir => ({ dir })) .map(dir => ({ dir }))
], ],

View File

@ -190,9 +190,10 @@ async function initNuxt (nuxt: Nuxt) {
// Automatically register user modules // Automatically register user modules
for (const config of nuxt.options._layers.map(layer => layer.config).reverse()) { 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, [ const layerModules = await resolveFiles(config.srcDir, [
`${config.dir?.modules || 'modules'}/*{${nuxt.options.extensions.join(',')}}`, `${modulesDir}/*{${nuxt.options.extensions.join(',')}}`,
`${config.dir?.modules || 'modules'}/*/index{${nuxt.options.extensions.join(',')}}` `${modulesDir}/*/index{${nuxt.options.extensions.join(',')}}`
]) ])
for (const mod of layerModules) { for (const mod of layerModules) {
watchedPaths.add(mod) watchedPaths.add(mod)

View File

@ -28,7 +28,7 @@ export default defineNuxtModule({
const useExperimentalTypedPages = nuxt.options.experimental.typedPages const useExperimentalTypedPages = nuxt.options.experimental.typedPages
const pagesDirs = nuxt.options._layers.map( 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 // 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() nuxt.options.pages = await isPagesEnabled()
// Restart Nuxt when pages dir is added or removed // Restart Nuxt when pages dir is added or removed
const restartPaths = nuxt.options._layers.flatMap(layer => [ const restartPaths = nuxt.options._layers.flatMap((layer) => {
join(layer.config.srcDir || layer.cwd, 'app/router.options.ts'), const pagesDir = (layer.config.rootDir === nuxt.options.rootDir ? nuxt.options : layer.config).dir?.pages || 'pages'
join(layer.config.srcDir || layer.cwd, 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) => { nuxt.hooks.hook('builder:watch', async (event, relativePath) => {
const path = resolve(nuxt.options.srcDir, relativePath) const path = resolve(nuxt.options.srcDir, relativePath)
@ -178,11 +181,14 @@ export default defineNuxtModule({
}) })
// Regenerate templates when adding or removing pages // Regenerate templates when adding or removing pages
const updateTemplatePaths = nuxt.options._layers.flatMap(l => [ const updateTemplatePaths = nuxt.options._layers.flatMap((l) => {
join(l.config.srcDir || l.cwd, l.config.dir?.pages || 'pages') + '/', const dir = (l.config.rootDir === nuxt.options.rootDir ? nuxt.options : l.config).dir
join(l.config.srcDir || l.cwd, l.config.dir?.layouts || 'layouts') + '/', return [
join(l.config.srcDir || l.cwd, l.config.dir?.middleware || 'middleware') + '/' 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) => { nuxt.hook('builder:watch', async (event, relativePath) => {
if (event === 'change') { return } if (event === 'change') { return }

View File

@ -42,7 +42,7 @@ export async function resolvePagesRoutes (): Promise<NuxtPage[]> {
const nuxt = useNuxt() const nuxt = useNuxt()
const pagesDirs = nuxt.options._layers.map( 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[] = [] const scannedFiles: ScannedFile[] = []