Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

fix(nuxt): resolve plugins and middleware to their full path #6350

Merged
merged 6 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions packages/nuxt/src/core/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 = [
Expand All @@ -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]))
}
}))
}
2 changes: 1 addition & 1 deletion packages/nuxt/src/core/plugins/unctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 12 additions & 1 deletion test/fixtures/basic/modules/example.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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)),
pi0 marked this conversation as resolved.
Show resolved Hide resolved
global: true
})
})
}
})
4 changes: 4 additions & 0 deletions test/fixtures/basic/modules/runtime/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default defineNuxtRouteMiddleware(async () => {
await new Promise(resolve => setTimeout(resolve, 1))
useNuxtApp()
})
4 changes: 4 additions & 0 deletions test/fixtures/basic/modules/runtime/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default defineNuxtPlugin(async () => {
await new Promise(resolve => setTimeout(resolve, 1))
useNuxtApp()
})