diff --git a/packages/kit/package.json b/packages/kit/package.json index 6bae02a3d2..54272eba32 100644 --- a/packages/kit/package.json +++ b/packages/kit/package.json @@ -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.5", diff --git a/packages/kit/src/plugin.ts b/packages/kit/src/plugin.ts index 116721214f..b89d9642be 100644 --- a/packages/kit/src/plugin.ts +++ b/packages/kit/src/plugin.ts @@ -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 } diff --git a/packages/nuxt/test/app.test.ts b/packages/nuxt/test/app.test.ts index 1bdcb5f8aa..40ba3b75bf 100644 --- a/packages/nuxt/test/app.test.ts +++ b/packages/nuxt/test/app.test.ts @@ -292,6 +292,8 @@ async function getResolvedApp (files: Array