fix(kit): fully resolve plugin paths when normalising them (#30540)

This commit is contained in:
Daniel Roe 2025-01-14 10:47:36 +00:00 committed by GitHub
parent 50ebb8be1e
commit a6ee58911b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 28 additions and 1 deletions

View File

@ -43,6 +43,7 @@
"pkg-types": "^1.3.0",
"scule": "^1.3.0",
"semver": "^7.6.3",
"std-env": "^3.8.0",
"ufo": "^1.5.4",
"unctx": "^2.4.1",
"unimport": "^3.14.6",

View File

@ -1,6 +1,11 @@
import { existsSync } from 'node:fs'
import { isAbsolute } from 'node:path'
import { pathToFileURL } from 'node:url'
import { normalize } from 'pathe'
import type { NuxtPlugin, NuxtPluginTemplate } from '@nuxt/schema'
import { useNuxt } from './context'
import { resolvePathSync } from 'mlly'
import { isWindows } from 'std-env'
import { tryUseNuxt, useNuxt } from './context'
import { addTemplate } from './template'
import { resolveAlias } from './resolve'
import { MODE_RE } from './utils'
@ -8,6 +13,7 @@ import { MODE_RE } from './utils'
/**
* Normalize a nuxt plugin object
*/
const pluginSymbol = Symbol.for('nuxt plugin')
export function normalizePlugin (plugin: NuxtPlugin | string): NuxtPlugin {
// Normalize src
if (typeof plugin === 'string') {
@ -16,6 +22,10 @@ export function normalizePlugin (plugin: NuxtPlugin | string): NuxtPlugin {
plugin = { ...plugin }
}
if (pluginSymbol in plugin) {
return plugin
}
if (!plugin.src) {
throw new Error('Invalid plugin. src option is required: ' + JSON.stringify(plugin))
}
@ -23,6 +33,14 @@ export function normalizePlugin (plugin: NuxtPlugin | string): NuxtPlugin {
// Normalize full path to plugin
plugin.src = normalize(resolveAlias(plugin.src))
if (!existsSync(plugin.src) && isAbsolute(plugin.src)) {
try {
plugin.src = resolvePathSync(isWindows ? pathToFileURL(plugin.src).href : plugin.src, { extensions: tryUseNuxt()?.options.extensions })
} catch {
// ignore errors as the file may be in the nuxt vfs
}
}
// Normalize mode
if (plugin.ssr) {
plugin.mode = 'server'
@ -32,6 +50,9 @@ export function normalizePlugin (plugin: NuxtPlugin | string): NuxtPlugin {
plugin.mode = mode as 'all' | 'client' | 'server'
}
// @ts-expect-error not adding symbol to types to avoid conflicts
plugin[pluginSymbol] = true
return plugin
}

View File

@ -292,6 +292,8 @@ async function getResolvedApp (files: Array<string | { name: string, contents: s
}
for (const plugin of app.plugins) {
plugin.src = normaliseToRepo(plugin.src)!
// @ts-expect-error untyped symbol
delete plugin[Symbol.for('nuxt plugin')]
}
for (const mw of app.middleware) {
mw.path = normaliseToRepo(mw.path)!

View File

@ -245,6 +245,9 @@ importers:
semver:
specifier: ^7.6.3
version: 7.6.3
std-env:
specifier: ^3.8.0
version: 3.8.0
ufo:
specifier: 1.5.4
version: 1.5.4