fix(nuxt): deprecate scanning directory index plugins (#18418)

This commit is contained in:
Inesh Bose 2023-03-01 12:08:58 +00:00 committed by GitHub
parent dccb733f36
commit c7eb891e12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 6 deletions

View File

@ -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

View File

@ -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))

View File

@ -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)))