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 all 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
58 changes: 54 additions & 4 deletions packages/nuxt/src/core/app.ts
@@ -1,8 +1,9 @@
import { promises as fsp, mkdirSync, writeFileSync } from 'node:fs'
import { dirname, join, relative, resolve } from 'pathe'
import { filename } from 'pathe/utils'
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 @@ -173,9 +174,10 @@ export async function resolveApp (nuxt: Nuxt, app: NuxtApp) {
}
}

// Normalize and de-duplicate plugins and middleware
app.middleware = uniqueBy(await resolvePaths([...app.middleware].reverse(), 'path'), 'name').reverse()
app.plugins = uniqueBy(await resolvePaths(app.plugins, 'src'), 'src')
// Normalize and de-duplicate plugins and middleware and hoist (and sort)
// middleware/plugin files that begin with a number
app.middleware = sortMiddleware(uniqueBy(await resolvePaths([...app.middleware].reverse(), 'path'), 'name').reverse())
app.plugins = sortPlugins(uniquePlugins(await resolvePaths(app.plugins, 'src')))

// Resolve app.config
app.configs = []
Expand Down Expand Up @@ -252,3 +254,51 @@ export function checkForCircularDependencies (_plugins: Array<NuxtPlugin & Omit<
checkDeps(name)
}
}

const ORDERED_FILE_RE = /^\d+\./

function sortMiddleware (middleware: NuxtMiddleware[]) {
const orderedMiddleware: NuxtMiddleware[] = []
const unorderedMiddleware: NuxtMiddleware[] = []

for (const mw of middleware) {
const bucket = mw.global && ORDERED_FILE_RE.test(filename(mw.path)) ? orderedMiddleware : unorderedMiddleware
bucket.push(mw)
}

return [
...orderedMiddleware.sort((l, r) => filename(l.path).localeCompare(filename(r.path))),
...unorderedMiddleware
]
}

function sortPlugins (plugins: NuxtPlugin[]) {
const orderedPlugins: NuxtPlugin[] = []
const unorderedPlugins: NuxtPlugin[] = []

for (const plugin of plugins) {
const bucket = ORDERED_FILE_RE.test(filename(plugin.src)) ? orderedPlugins : unorderedPlugins
bucket.push(plugin)
}

return [
...orderedPlugins.sort((l, r) => filename(l.src).localeCompare(filename(r.src))),
...unorderedPlugins
]
}

function uniquePlugins (plugins: NuxtPlugin[]) {
const pluginFlags = new Set<string>()
const bucket: NuxtPlugin[] = []
for (const plugin of [...plugins].reverse()) {
const name = plugin.name ? plugin.name : filename(plugin.src)
const mode = plugin.mode ? plugin.mode : 'all'
const flag = `${name}.${mode}`
if (pluginFlags.has(flag)) {
continue
}
pluginFlags.add(flag)
bucket.push(plugin)
}
return bucket.reverse()
}
16 changes: 9 additions & 7 deletions packages/nuxt/test/app.test.ts
Expand Up @@ -134,16 +134,12 @@ describe('resolveApp', () => {
}
])
const fixturePlugins = app.plugins.filter(p => !('getContents' in p) && p.src.includes('<rootDir>')).map(p => p.src)
// TODO: support overriding named plugins
expect(fixturePlugins).toMatchInlineSnapshot(`
[
"<rootDir>/layer1/plugins/02.plugin.ts",
"<rootDir>/layer1/plugins/object-named.ts",
"<rootDir>/layer1/plugins/override-test.ts",
"<rootDir>/plugins/00.plugin.ts",
"<rootDir>/layer2/plugins/01.plugin.ts",
"<rootDir>/layer2/plugins/object-named.ts",
"<rootDir>/layer1/plugins/02.plugin.ts",
"<rootDir>/layer2/plugins/override-test.ts",
"<rootDir>/plugins/00.plugin.ts",
"<rootDir>/plugins/object-named.ts",
]
`)
Expand All @@ -152,17 +148,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 +170,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