mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
fix(nuxt): scan folder indices for middleware (#27187)
This commit is contained in:
parent
41874cb35c
commit
89baee0493
@ -252,6 +252,34 @@ However, if you are a module author using the `builder:watch` hook and wishing t
|
|||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Directory index scanning
|
||||||
|
|
||||||
|
🚦 **Impact Level**: Medium
|
||||||
|
|
||||||
|
##### What Changed
|
||||||
|
|
||||||
|
Child folders in your `middleware/` folder are also scanned for `index` files and these are now also registered as middleware in your project.
|
||||||
|
|
||||||
|
##### Reasons for Change
|
||||||
|
|
||||||
|
Nuxt scans a number of folders automatically, including `middleware/` and `plugins/`.
|
||||||
|
|
||||||
|
Child folders in your `plugins/` folder are scanned for `index` files and we wanted to make this behavior consistent between scanned directories.
|
||||||
|
|
||||||
|
##### Migration Steps
|
||||||
|
|
||||||
|
Probably no migration is necessary but if you wish to revert to previous behavior you can add a hook to filter out these middleware:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
export default defineNuxtConfig({
|
||||||
|
hooks: {
|
||||||
|
'app:resolve'(app) {
|
||||||
|
app.middleware = app.middleware.filter(mw => !/\/index\.[^/]+$/.test(mw.path))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
#### Template Compilation Changes
|
#### Template Compilation Changes
|
||||||
|
|
||||||
🚦 **Impact Level**: Minimal
|
🚦 **Impact Level**: Minimal
|
||||||
|
@ -3,7 +3,6 @@ import type { NuxtPlugin, NuxtPluginTemplate } from '@nuxt/schema'
|
|||||||
import { useNuxt } from './context'
|
import { useNuxt } from './context'
|
||||||
import { addTemplate } from './template'
|
import { addTemplate } from './template'
|
||||||
import { resolveAlias } from './resolve'
|
import { resolveAlias } from './resolve'
|
||||||
import { logger } from './logger'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Normalize a nuxt plugin object
|
* Normalize a nuxt plugin object
|
||||||
@ -20,12 +19,6 @@ export function normalizePlugin (plugin: NuxtPlugin | string): NuxtPlugin {
|
|||||||
throw new Error('Invalid plugin. src option is required: ' + JSON.stringify(plugin))
|
throw new Error('Invalid plugin. src option is required: ' + JSON.stringify(plugin))
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: only scan top-level files #18418
|
|
||||||
const nonTopLevelPlugin = plugin.src.match(/\/plugins\/[^/]+\/index\.[^/]+$/i)
|
|
||||||
if (nonTopLevelPlugin && nonTopLevelPlugin.length > 0 && !useNuxt().options.plugins.find(i => (typeof i === 'string' ? i : i.src).endsWith(nonTopLevelPlugin[0]))) {
|
|
||||||
logger.warn(`[deprecation] You are using a plugin that is within a subfolder of your plugins directory without adding it to your config explicitly. You can move it to the top-level plugins directory, or include the file '~${nonTopLevelPlugin[0]}' in your plugins config (https://nuxt.com/docs/api/nuxt-config#plugins-1) to remove this warning.`)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Normalize full path to plugin
|
// Normalize full path to plugin
|
||||||
plugin.src = normalize(resolveAlias(plugin.src))
|
plugin.src = normalize(resolveAlias(plugin.src))
|
||||||
|
|
||||||
|
@ -179,7 +179,12 @@ export async function resolveApp (nuxt: Nuxt, app: NuxtApp) {
|
|||||||
app.middleware = []
|
app.middleware = []
|
||||||
for (const config of reversedConfigs) {
|
for (const config of reversedConfigs) {
|
||||||
const middlewareDir = (config.rootDir === nuxt.options.rootDir ? nuxt.options : config).dir?.middleware || 'middleware'
|
const middlewareDir = (config.rootDir === nuxt.options.rootDir ? nuxt.options : config).dir?.middleware || 'middleware'
|
||||||
const middlewareFiles = await resolveFiles(config.srcDir, `${middlewareDir}/*{${nuxt.options.extensions.join(',')}}`)
|
const middlewareFiles = await resolveFiles(config.srcDir, [
|
||||||
|
`${middlewareDir}/*{${nuxt.options.extensions.join(',')}}`,
|
||||||
|
...nuxt.options.future.compatibilityVersion === 4
|
||||||
|
? [`${middlewareDir}/*/index{${nuxt.options.extensions.join(',')}}`]
|
||||||
|
: [],
|
||||||
|
])
|
||||||
for (const file of middlewareFiles) {
|
for (const file of middlewareFiles) {
|
||||||
const name = getNameFromPath(file)
|
const name = getNameFromPath(file)
|
||||||
if (!name) {
|
if (!name) {
|
||||||
@ -200,7 +205,7 @@ export async function resolveApp (nuxt: Nuxt, app: NuxtApp) {
|
|||||||
...config.srcDir
|
...config.srcDir
|
||||||
? await resolveFiles(config.srcDir, [
|
? await resolveFiles(config.srcDir, [
|
||||||
`${pluginDir}/*{${nuxt.options.extensions.join(',')}}`,
|
`${pluginDir}/*{${nuxt.options.extensions.join(',')}}`,
|
||||||
`${pluginDir}/*/index{${nuxt.options.extensions.join(',')}}`, // TODO: remove, only scan top-level plugins #18418
|
`${pluginDir}/*/index{${nuxt.options.extensions.join(',')}}`,
|
||||||
])
|
])
|
||||||
: [],
|
: [],
|
||||||
].map(plugin => normalizePlugin(plugin as NuxtPlugin)))
|
].map(plugin => normalizePlugin(plugin as NuxtPlugin)))
|
||||||
|
Loading…
Reference in New Issue
Block a user