fix(nuxt): warn about legacy and invalid plugins (#5857)

Co-authored-by: Pooya Parsa <pooya@pi0.io>
This commit is contained in:
Daniel Roe 2022-07-12 18:06:55 +01:00 committed by GitHub
parent aa5a97ace2
commit 1ced224389
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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`.
*