diff --git a/packages/nuxt/src/core/app.ts b/packages/nuxt/src/core/app.ts index fbb820e060..1ae5080960 100644 --- a/packages/nuxt/src/core/app.ts +++ b/packages/nuxt/src/core/app.ts @@ -2,7 +2,7 @@ import { promises as fsp } from 'node:fs' import { dirname, resolve } from 'pathe' import defu from 'defu' import type { Nuxt, NuxtApp, NuxtPlugin } from '@nuxt/schema' -import { findPath, resolveFiles, normalizePlugin, normalizeTemplate, compileTemplate, templateUtils, tryResolveModule } from '@nuxt/kit' +import { findPath, resolveFiles, normalizePlugin, normalizeTemplate, compileTemplate, templateUtils, tryResolveModule, resolvePath, resolveAlias } from '@nuxt/kit' import * as defaultTemplates from './templates' import { getNameFromPath, hasSuffix, uniqueBy } from './utils' @@ -94,7 +94,6 @@ export async function resolveApp (nuxt: Nuxt, app: NuxtApp) { return { name, path: file, global: hasSuffix(file, '.global') } })) } - app.middleware = uniqueBy(app.middleware, 'name') // Resolve plugins app.plugins = [ @@ -109,8 +108,25 @@ export async function resolveApp (nuxt: Nuxt, app: NuxtApp) { ]) ].map(plugin => normalizePlugin(plugin as NuxtPlugin))) } - app.plugins = uniqueBy(app.plugins, 'src') + + // Normalize and de-duplicate plugins and middleware + app.middleware = uniqueBy(await resolvePaths(app.middleware, 'path'), 'name') + app.plugins = uniqueBy(await resolvePaths(app.plugins, 'src'), 'src') // Extend app await nuxt.callHook('app:resolve', app) + + // Normalize and de-duplicate plugins and middleware + app.middleware = uniqueBy(await resolvePaths(app.middleware, 'path'), 'name') + app.plugins = uniqueBy(await resolvePaths(app.plugins, 'src'), 'src') +} + +function resolvePaths > (items: Item[], key: { [K in keyof Item]: Item[K] extends string ? K : never }[keyof Item]) { + return Promise.all(items.map(async (item) => { + if (!item[key]) { return item } + return { + ...item, + [key]: await resolvePath(resolveAlias(item[key])) + } + })) } diff --git a/packages/nuxt/src/core/plugins/unctx.ts b/packages/nuxt/src/core/plugins/unctx.ts index 6c2d4ba970..4bb7c8295d 100644 --- a/packages/nuxt/src/core/plugins/unctx.ts +++ b/packages/nuxt/src/core/plugins/unctx.ts @@ -14,7 +14,7 @@ export const UnctxTransformPlugin = (nuxt: Nuxt) => { name: 'unctx:transfrom', enforce: 'post', transformInclude (id) { - return Boolean(app?.plugins.find(i => i.src === id) || app.middleware.find(m => m.path === id)) + return app?.plugins.some(i => i.src === id) || app?.middleware.some(m => m.path === id) }, transform (code, id) { const result = transformer.transform(code) diff --git a/test/fixtures/basic/modules/example.ts b/test/fixtures/basic/modules/example.ts index d0c8fd880b..9f28b73c11 100644 --- a/test/fixtures/basic/modules/example.ts +++ b/test/fixtures/basic/modules/example.ts @@ -1,4 +1,5 @@ -import { defineNuxtModule } from '@nuxt/kit' +import { fileURLToPath } from 'node:url' +import { defineNuxtModule, addPlugin, useNuxt } from '@nuxt/kit' export default defineNuxtModule({ defaults: { @@ -7,5 +8,15 @@ export default defineNuxtModule({ meta: { name: 'my-module', configKey: 'sampleModule' + }, + setup () { + addPlugin(fileURLToPath(new URL('./runtime/plugin', import.meta.url))) + useNuxt().hook('app:resolve', (app) => { + app.middleware.push({ + name: 'unctx-test', + path: fileURLToPath(new URL('./runtime/middleware', import.meta.url)), + global: true + }) + }) } }) diff --git a/test/fixtures/basic/modules/runtime/middleware.ts b/test/fixtures/basic/modules/runtime/middleware.ts new file mode 100644 index 0000000000..7ddcb23d82 --- /dev/null +++ b/test/fixtures/basic/modules/runtime/middleware.ts @@ -0,0 +1,4 @@ +export default defineNuxtRouteMiddleware(async () => { + await new Promise(resolve => setTimeout(resolve, 1)) + useNuxtApp() +}) diff --git a/test/fixtures/basic/modules/runtime/plugin.ts b/test/fixtures/basic/modules/runtime/plugin.ts new file mode 100644 index 0000000000..61a24ed6d2 --- /dev/null +++ b/test/fixtures/basic/modules/runtime/plugin.ts @@ -0,0 +1,4 @@ +export default defineNuxtPlugin(async () => { + await new Promise(resolve => setTimeout(resolve, 1)) + useNuxtApp() +})