mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
fix(nuxt): resolve plugins and middleware to their full path (#6350)
This commit is contained in:
parent
bc26cc6467
commit
746d553249
@ -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 <Item extends Record<string, any>> (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]))
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
@ -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)
|
||||
|
13
test/fixtures/basic/modules/example.ts
vendored
13
test/fixtures/basic/modules/example.ts
vendored
@ -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
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
|
4
test/fixtures/basic/modules/runtime/middleware.ts
vendored
Normal file
4
test/fixtures/basic/modules/runtime/middleware.ts
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
export default defineNuxtRouteMiddleware(async () => {
|
||||
await new Promise(resolve => setTimeout(resolve, 1))
|
||||
useNuxtApp()
|
||||
})
|
4
test/fixtures/basic/modules/runtime/plugin.ts
vendored
Normal file
4
test/fixtures/basic/modules/runtime/plugin.ts
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
export default defineNuxtPlugin(async () => {
|
||||
await new Promise(resolve => setTimeout(resolve, 1))
|
||||
useNuxtApp()
|
||||
})
|
Loading…
Reference in New Issue
Block a user