Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nuxt): sort numbered middleware/plugins from all layers #25906

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/nuxt/src/core/app.ts
Expand Up @@ -2,7 +2,7 @@
import { dirname, join, relative, resolve } from 'pathe'
import { defu } from 'defu'
import { compileTemplate, findPath, logger, normalizePlugin, normalizeTemplate, resolveAlias, resolveFiles, resolvePath, templateUtils, tryResolveModule } from '@nuxt/kit'
import type { Nuxt, NuxtApp, NuxtPlugin, NuxtTemplate, ResolvedNuxtTemplate } from 'nuxt/schema'
import type { Nuxt, NuxtApp, NuxtMiddleware, NuxtPlugin, NuxtTemplate, ResolvedNuxtTemplate } from 'nuxt/schema'

import * as defaultTemplates from './templates'
import { getNameFromPath, hasSuffix, uniqueBy } from './utils'
Expand Down Expand Up @@ -141,6 +141,7 @@
app.middleware.push({ name, path: file, global: hasSuffix(file, '.global') })
}
}
app.middleware = sortMiddleware(app.middleware)

// Resolve plugins, first extended layers and then base
app.plugins = []
Expand Down Expand Up @@ -220,7 +221,7 @@
for (const plugin of _plugins) {
// Make sure dependency plugins are registered
if (plugin.dependsOn && plugin.dependsOn.some(name => !pluginNames.includes(name))) {
console.error(`Plugin \`${plugin.name}\` depends on \`${plugin.dependsOn.filter(name => !pluginNames.includes(name)).join(', ')}\` but they are not registered.`)

Check warning on line 224 in packages/nuxt/src/core/app.ts

View workflow job for this annotation

GitHub Actions / code

Unexpected console statement
}
// Make graph to detect circular dependencies
if (plugin.name) {
Expand All @@ -229,7 +230,7 @@
}
const checkDeps = (name: string, visited: string[] = []): string[] => {
if (visited.includes(name)) {
console.error(`Circular dependency detected in plugins: ${visited.join(' -> ')} -> ${name}`)

Check warning on line 233 in packages/nuxt/src/core/app.ts

View workflow job for this annotation

GitHub Actions / code

Unexpected console statement
return []
}
visited.push(name)
Expand All @@ -239,3 +240,19 @@
checkDeps(name)
}
}


function sortMiddleware(middleware: NuxtMiddleware[]) {
const globalMiddleware = middleware.filter(mw => mw.global)
const namedMiddleware = middleware.filter(mw => !mw.global)
return [
...sortOrderedMiddleware(globalMiddleware),
...sortOrderedMiddleware(namedMiddleware)
]
function sortOrderedMiddleware (middleware: NuxtMiddleware[]) {
const reg = /^\d/
const orderedMiddleware = middleware.filter(m => reg.test(m.name)).toSorted((l, r) => l.name > r.name ? 1 : -1)
markthree marked this conversation as resolved.
Show resolved Hide resolved
const unorderedMiddleware = middleware.filter(m => !reg.test(m.name))
return [...orderedMiddleware, ...unorderedMiddleware]
}
}
8 changes: 7 additions & 1 deletion packages/nuxt/test/app.test.ts
Expand Up @@ -152,17 +152,20 @@ describe('resolveApp', () => {
it('resolves layer middleware in correct order', async () => {
const app = await getResolvedApp([
// layer 1
'layer1/middleware/01.order.global.ts',
'layer1/middleware/global.global.ts',
'layer1/middleware/named-from-layer.ts',
'layer1/middleware/named-override.ts',
'layer1/nuxt.config.ts',
// layer 2
'layer2/middleware/02.order.global.ts',
'layer2/middleware/global.global.ts',
'layer2/middleware/named-from-layer.ts',
'layer2/middleware/named-override.ts',
'layer2/plugins/override-test.ts',
'layer2/nuxt.config.ts',
// final (user) layer
'middleware/00.order.global.ts',
'middleware/named-override.ts',
'middleware/named.ts',
{
Expand All @@ -171,9 +174,12 @@ describe('resolveApp', () => {
}
])
const fixtureMiddleware = app.middleware.filter(p => p.path.includes('<rootDir>')).map(p => p.path)
// TODO: fix this

expect(fixtureMiddleware).toMatchInlineSnapshot(`
[
"<rootDir>/middleware/00.order.global.ts",
"<rootDir>/layer1/middleware/01.order.global.ts",
"<rootDir>/layer2/middleware/02.order.global.ts",
"<rootDir>/layer2/middleware/global.global.ts",
"<rootDir>/layer2/middleware/named-from-layer.ts",
"<rootDir>/middleware/named-override.ts",
Expand Down