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

Commit

Permalink
refactor: normalize plugins and middleware after app:resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Aug 4, 2022
1 parent f423665 commit 2ad5a55
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
18 changes: 15 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,21 @@ export async function resolveApp (nuxt: Nuxt, app: NuxtApp) {
])
].map(plugin => normalizePlugin(plugin as NuxtPlugin)))
}
app.plugins = uniqueBy(app.plugins, 'src')

// Extend app
await nuxt.callHook('app:resolve', app)

// Normalize and de-duplicate plugins and middleware
app.middleware = uniqueBy(await normalizePaths(app.middleware, 'path'), 'name')
app.plugins = uniqueBy(await normalizePaths(app.plugins, 'src'), 'src')
}

const normalizePaths = <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]))
}
}))
}
4 changes: 1 addition & 3 deletions 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 => matchesPath(id, i.src)) || app.middleware.find(m => matchesPath(id, m.path)))
return app?.plugins.some(i => i.src === id) || app.middleware.some(m => m.path === id)
},
transform (code, id) {
const result = transformer.transform(code)
Expand All @@ -27,5 +27,3 @@ export const UnctxTransformPlugin = (nuxt: Nuxt) => {
}
}))
}

const matchesPath = (path: string, withoutExt: string) => path === withoutExt || path.startsWith(withoutExt + '.')

0 comments on commit 2ad5a55

Please sign in to comment.