mirror of
https://github.com/nuxt/nuxt.git
synced 2025-03-09 03:03:18 +00:00
fix(nuxt): warn about legacy and invalid plugins (#5857)
Co-authored-by: Pooya Parsa <pooya@pi0.io>
This commit is contained in:
parent
aa5a97ace2
commit
1ced224389
@ -181,12 +181,39 @@ export async function applyPlugins (nuxtApp: NuxtApp, plugins: Plugin[]) {
|
||||
}
|
||||
|
||||
export function normalizePlugins (_plugins: Plugin[]) {
|
||||
const unwrappedPlugins = []
|
||||
const legacyInjectPlugins = []
|
||||
const invalidPlugins = []
|
||||
|
||||
const plugins = _plugins.map((plugin) => {
|
||||
if (typeof plugin !== 'function') {
|
||||
return () => {}
|
||||
invalidPlugins.push(plugin)
|
||||
return null
|
||||
}
|
||||
if (plugin.length > 1) {
|
||||
legacyInjectPlugins.push(plugin)
|
||||
// Allow usage without wrapper but warn
|
||||
// TODO: Skip invalid in next releases
|
||||
// @ts-ignore
|
||||
return (nuxtApp: NuxtApp) => plugin(nuxtApp, nuxtApp.provide)
|
||||
// return null
|
||||
}
|
||||
if (!isNuxtPlugin(plugin)) {
|
||||
unwrappedPlugins.push(plugin)
|
||||
// Allow usage without wrapper but warn
|
||||
}
|
||||
return plugin
|
||||
})
|
||||
}).filter(Boolean)
|
||||
|
||||
if (process.dev && legacyInjectPlugins.length) {
|
||||
console.warn('[warn] [nuxt] You are using a plugin with legacy Nuxt 2 format (context, inject) which is likely to be broken. In the future they will be ignored:', legacyInjectPlugins.map(p => p.name || p).join(','))
|
||||
}
|
||||
if (process.dev && invalidPlugins.length) {
|
||||
console.warn('[warn] [nuxt] Some plugins are not exposing a function and skipped:', invalidPlugins)
|
||||
}
|
||||
if (process.dev && unwrappedPlugins.length) {
|
||||
console.warn('[warn] [nuxt] You are using a plugin that has not been wrapped in `defineNuxtPlugin`. It is advised to wrap your plugins as in the future this may enable enhancements:', unwrappedPlugins.map(p => p.name || p).join(','))
|
||||
}
|
||||
|
||||
return plugins as Plugin[]
|
||||
}
|
||||
@ -196,6 +223,10 @@ export function defineNuxtPlugin<T> (plugin: Plugin<T>) {
|
||||
return plugin
|
||||
}
|
||||
|
||||
export function isNuxtPlugin (plugin: unknown) {
|
||||
return typeof plugin === 'function' && NuxtPluginIndicator in plugin
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that the setup function passed in has access to the Nuxt instance via `useNuxt`.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user