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
Changes from 1 commit
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
11 changes: 9 additions & 2 deletions packages/nuxt/src/core/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import escapeRE from 'escape-string-regexp'
import { hash } from 'ohash'
import { camelCase } from 'scule'
import { filename } from 'pathe/utils'
import type { NuxtTemplate } from 'nuxt/schema'
import type { NuxtMiddleware, NuxtTemplate } from 'nuxt/schema'
import { annotatePlugins, checkForCircularDependencies } from './app'

export const vueShim: NuxtTemplate = {
Expand Down Expand Up @@ -208,14 +208,21 @@ export const layoutTemplate: NuxtTemplate = {
export const middlewareTemplate: NuxtTemplate = {
filename: 'middleware.mjs',
getContents ({ app }) {
const globalMiddleware = app.middleware.filter(mw => mw.global)
const globalMiddleware = sortGlobalMiddleware(app.middleware.filter(mw => mw.global))
const namedMiddleware = app.middleware.filter(mw => !mw.global)
const namedMiddlewareObject = genObjectFromRawEntries(namedMiddleware.map(mw => [mw.name, genDynamicImport(mw.path)]))

return [
...globalMiddleware.map(mw => genImport(mw.path, genSafeVariableName(mw.name))),
`export const globalMiddleware = ${genArrayFromRaw(globalMiddleware.map(mw => genSafeVariableName(mw.name)))}`,
`export const namedMiddleware = ${namedMiddlewareObject}`
].join('\n')
function sortGlobalMiddleware (globalMiddleware: NuxtMiddleware[]): NuxtMiddleware[] {
const reg = /^\d/
const withOrdergGobalMiddleware = globalMiddleware.filter(m => reg.test(m.name)).toSorted((l, r) => l.name > r.name ? 1 : -1)
const withoutOrdergGobalMiddleware = globalMiddleware.filter(m => !reg.test(m.name))
return [...withOrdergGobalMiddleware, ...withoutOrdergGobalMiddleware]
markthree marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

Expand Down