mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-18 17:35:57 +00:00
fix(kit): fully resolve plugin paths when normalising them (#30540)
This commit is contained in:
parent
50ebb8be1e
commit
a6ee58911b
@ -43,6 +43,7 @@
|
|||||||
"pkg-types": "^1.3.0",
|
"pkg-types": "^1.3.0",
|
||||||
"scule": "^1.3.0",
|
"scule": "^1.3.0",
|
||||||
"semver": "^7.6.3",
|
"semver": "^7.6.3",
|
||||||
|
"std-env": "^3.8.0",
|
||||||
"ufo": "^1.5.4",
|
"ufo": "^1.5.4",
|
||||||
"unctx": "^2.4.1",
|
"unctx": "^2.4.1",
|
||||||
"unimport": "^3.14.6",
|
"unimport": "^3.14.6",
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
|
import { existsSync } from 'node:fs'
|
||||||
|
import { isAbsolute } from 'node:path'
|
||||||
|
import { pathToFileURL } from 'node:url'
|
||||||
import { normalize } from 'pathe'
|
import { normalize } from 'pathe'
|
||||||
import type { NuxtPlugin, NuxtPluginTemplate } from '@nuxt/schema'
|
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 { addTemplate } from './template'
|
||||||
import { resolveAlias } from './resolve'
|
import { resolveAlias } from './resolve'
|
||||||
import { MODE_RE } from './utils'
|
import { MODE_RE } from './utils'
|
||||||
@ -8,6 +13,7 @@ import { MODE_RE } from './utils'
|
|||||||
/**
|
/**
|
||||||
* Normalize a nuxt plugin object
|
* Normalize a nuxt plugin object
|
||||||
*/
|
*/
|
||||||
|
const pluginSymbol = Symbol.for('nuxt plugin')
|
||||||
export function normalizePlugin (plugin: NuxtPlugin | string): NuxtPlugin {
|
export function normalizePlugin (plugin: NuxtPlugin | string): NuxtPlugin {
|
||||||
// Normalize src
|
// Normalize src
|
||||||
if (typeof plugin === 'string') {
|
if (typeof plugin === 'string') {
|
||||||
@ -16,6 +22,10 @@ export function normalizePlugin (plugin: NuxtPlugin | string): NuxtPlugin {
|
|||||||
plugin = { ...plugin }
|
plugin = { ...plugin }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (pluginSymbol in plugin) {
|
||||||
|
return plugin
|
||||||
|
}
|
||||||
|
|
||||||
if (!plugin.src) {
|
if (!plugin.src) {
|
||||||
throw new Error('Invalid plugin. src option is required: ' + JSON.stringify(plugin))
|
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
|
// Normalize full path to plugin
|
||||||
plugin.src = normalize(resolveAlias(plugin.src))
|
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
|
// Normalize mode
|
||||||
if (plugin.ssr) {
|
if (plugin.ssr) {
|
||||||
plugin.mode = 'server'
|
plugin.mode = 'server'
|
||||||
@ -32,6 +50,9 @@ export function normalizePlugin (plugin: NuxtPlugin | string): NuxtPlugin {
|
|||||||
plugin.mode = mode as 'all' | 'client' | 'server'
|
plugin.mode = mode as 'all' | 'client' | 'server'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @ts-expect-error not adding symbol to types to avoid conflicts
|
||||||
|
plugin[pluginSymbol] = true
|
||||||
|
|
||||||
return plugin
|
return plugin
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -292,6 +292,8 @@ async function getResolvedApp (files: Array<string | { name: string, contents: s
|
|||||||
}
|
}
|
||||||
for (const plugin of app.plugins) {
|
for (const plugin of app.plugins) {
|
||||||
plugin.src = normaliseToRepo(plugin.src)!
|
plugin.src = normaliseToRepo(plugin.src)!
|
||||||
|
// @ts-expect-error untyped symbol
|
||||||
|
delete plugin[Symbol.for('nuxt plugin')]
|
||||||
}
|
}
|
||||||
for (const mw of app.middleware) {
|
for (const mw of app.middleware) {
|
||||||
mw.path = normaliseToRepo(mw.path)!
|
mw.path = normaliseToRepo(mw.path)!
|
||||||
|
@ -245,6 +245,9 @@ importers:
|
|||||||
semver:
|
semver:
|
||||||
specifier: ^7.6.3
|
specifier: ^7.6.3
|
||||||
version: 7.6.3
|
version: 7.6.3
|
||||||
|
std-env:
|
||||||
|
specifier: ^3.8.0
|
||||||
|
version: 3.8.0
|
||||||
ufo:
|
ufo:
|
||||||
specifier: 1.5.4
|
specifier: 1.5.4
|
||||||
version: 1.5.4
|
version: 1.5.4
|
||||||
|
Loading…
Reference in New Issue
Block a user