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 14 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
42 changes: 38 additions & 4 deletions packages/nuxt/src/core/app.ts
Original file line number Diff line number Diff line change
@@ -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 @@
}
}

// 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(uniqueBy(await resolvePaths(app.plugins, 'src'), 'src'))

// Resolve app.config
app.configs = []
Expand Down Expand Up @@ -233,7 +235,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 238 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 @@ -242,7 +244,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 247 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 @@ -252,3 +254,35 @@
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
]
}
12 changes: 9 additions & 3 deletions packages/nuxt/test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ describe('resolveApp', () => {
// TODO: support overriding named plugins
expect(fixturePlugins).toMatchInlineSnapshot(`
[
"<rootDir>/plugins/00.plugin.ts",
"<rootDir>/layer2/plugins/01.plugin.ts",
"<rootDir>/layer1/plugins/02.plugin.ts",
"<rootDir>/layer1/plugins/object-named.ts",
"<rootDir>/layer1/plugins/override-test.ts",
"<rootDir>/layer2/plugins/01.plugin.ts",
"<rootDir>/layer2/plugins/object-named.ts",
"<rootDir>/layer2/plugins/override-test.ts",
"<rootDir>/plugins/00.plugin.ts",
"<rootDir>/plugins/object-named.ts",
]
`)
Expand All @@ -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