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

Commit

Permalink
fix(nuxt): resolve plugins and middleware to their full path (#6350)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Aug 4, 2022
1 parent bc26cc6 commit 746d553
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
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)),
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()
})

0 comments on commit 746d553

Please sign in to comment.