From c7eb891e12344f53410dc629b0478fc3d1253295 Mon Sep 17 00:00:00 2001 From: Inesh Bose <56732164+ineshbose@users.noreply.github.com> Date: Wed, 1 Mar 2023 12:08:58 +0000 Subject: [PATCH] fix(nuxt): deprecate scanning directory index plugins (#18418) --- docs/2.guide/2.directory-structure/1.plugins.md | 10 +++++----- packages/kit/src/plugin.ts | 6 ++++++ packages/nuxt/src/core/app.ts | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/docs/2.guide/2.directory-structure/1.plugins.md b/docs/2.guide/2.directory-structure/1.plugins.md index 85ebd33b48..544b7dec9b 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 a15b55b0c6..89929c9bed 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 fc0ce984b8..32a3b10f7b 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)))