Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

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

Merged
merged 9 commits into from
Jul 12, 2022
35 changes: 33 additions & 2 deletions packages/nuxt/src/app/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
}
Expand All @@ -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`.
*
Expand Down