diff --git a/docs/2.guide/2.directory-structure/1.plugins.md b/docs/2.guide/2.directory-structure/1.plugins.md index 85ebd33b4..544b7dec9 100644 --- a/docs/2.guide/2.directory-structure/1.plugins.md +++ b/docs/2.guide/2.directory-structure/1.plugins.md @@ -21,14 +21,14 @@ For example: ```bash plugins - | - myPlugin.ts + | - myPlugin.ts // scanned | - myOtherPlugin - | --- supportingFile.ts - | --- componentToRegister.vue - | --- index.ts + | --- supportingFile.ts // not scanned + | --- componentToRegister.vue // not scanned + | --- index.ts // currently scanned but deprecated ``` -Only `myPlugin.ts` and `myOtherPlugin/index.ts` would be registered. +Only `myPlugin.ts` and `myOtherPlugin/index.ts` would be registered. You can configure [`plugins`](/docs/api/configuration/nuxt-config#plugins-1) to include unscanned files. ## Creating Plugins diff --git a/packages/kit/src/plugin.ts b/packages/kit/src/plugin.ts index a15b55b0c..89929c9be 100644 --- a/packages/kit/src/plugin.ts +++ b/packages/kit/src/plugin.ts @@ -19,6 +19,12 @@ export function normalizePlugin (plugin: NuxtPlugin | string): NuxtPlugin { throw new Error('Invalid plugin. src option is required: ' + JSON.stringify(plugin)) } + // TODO: only scan top-level files #18418 + const nonTopLevelPlugin = plugin.src.match(/\/plugins\/[^/]+\/index\.[^/]+$/i) + if (nonTopLevelPlugin && nonTopLevelPlugin.length > 0 && !useNuxt().options.plugins.find(i => (typeof i === 'string' ? i : i.src).endsWith(nonTopLevelPlugin[0]))) { + console.warn(`[warn] [nuxt] [deprecation] You are using a plugin that is within a subfolder of your plugins directory without adding it to your config explicitly. You can move it to the top-level plugins directory, or include the file '~${nonTopLevelPlugin[0]}' in your plugins config (https://nuxt.com/docs/api/configuration/nuxt-config#plugins-1) to remove this warning.`) + } + // Normalize full path to plugin plugin.src = normalize(resolveAlias(plugin.src)) diff --git a/packages/nuxt/src/core/app.ts b/packages/nuxt/src/core/app.ts index fc0ce984b..32a3b10f7 100644 --- a/packages/nuxt/src/core/app.ts +++ b/packages/nuxt/src/core/app.ts @@ -112,7 +112,7 @@ export async function resolveApp (nuxt: Nuxt, app: NuxtApp) { ...config.srcDir ? await resolveFiles(config.srcDir, [ `${config.dir?.plugins || 'plugins'}/*.{ts,js,mjs,cjs,mts,cts}`, - `${config.dir?.plugins || 'plugins'}/*/index.*{ts,js,mjs,cjs,mts,cts}` + `${config.dir?.plugins || 'plugins'}/*/index.*{ts,js,mjs,cjs,mts,cts}` // TODO: remove, only scan top-level plugins #18418 ]) : [] ].map(plugin => normalizePlugin(plugin as NuxtPlugin)))