Repository navigation
Find JavaScript plugins, middleware and app.config in the Nuxt plugin - #2312
Conversation
commit: |
webpro
left a comment
There was a problem hiding this comment.
Thanks! The extensions match what Nuxt scans, but the plugin and middleware patterns now pick up more files than Nuxt loads, see inline.
| const middleware = (dir = 'middleware') => join(dir, `**/*.${scriptExt}`); | ||
| const pages = (dir = 'pages') => join(dir, '**/*.{vue,jsx,tsx}'); | ||
| const plugins = (dir = 'plugins') => join(dir, '**/*.ts'); | ||
| const plugins = (dir = 'plugins') => join(dir, `**/*.${scriptExt}`); |
There was a problem hiding this comment.
Nuxt only scans the top level of plugins and middleware, plus <dir>/index files (*{ext} and */index{ext}). With **/*, a helper that nothing imports, like plugins/helpers/unused.js, becomes an entry and is no longer reported. That was already the case for .ts, and now it applies to all the new extensions too:
| const middleware = (dir = 'middleware') => join(dir, `**/*.${scriptExt}`); | |
| const pages = (dir = 'pages') => join(dir, '**/*.{vue,jsx,tsx}'); | |
| const plugins = (dir = 'plugins') => join(dir, '**/*.ts'); | |
| const plugins = (dir = 'plugins') => join(dir, `**/*.${scriptExt}`); | |
| const middleware = (dir = 'middleware') => join(dir, `{*,*/index}.${scriptExt}`); | |
| const pages = (dir = 'pages') => join(dir, '**/*.{vue,jsx,tsx}'); | |
| const plugins = (dir = 'plugins') => join(dir, `{*,*/index}.${scriptExt}`); |
The recursive pattern was also what picked up plugins listed in the config, like plugins: ['~/plugins/custom/deep.js'], so resolveConfig would need to add those (strings and { src } objects, through resolveAlias). In the existing fixture, plugins/helpers/unused.js and middleware/helpers/unused.js reported as unused and plugins/nested/index.js kept as an entry would cover it.
There was a problem hiding this comment.
Done in 1a46a49f: plugins and middleware now use {*,*/index} with your suggested patterns, and resolveConfig adds plugins listed in the config (strings and { src } objects, through resolveAlias). In the nuxt fixture, plugins/helpers/unused.js and middleware/helpers/unused.js are now reported as unused files and plugins/nested/index.js stays an entry. The nuxt-config fixture lists ~/my-plugins/custom/deep and { src: '~/my-plugins/custom/client.js' }. Without the config handling, those two show up as unused files. All Nuxt plugin tests pass and tsc is clean.
webpro
left a comment
There was a problem hiding this comment.
Thanks! Helpers below plugins and middleware are reported again, and plugins listed in the config are found. Two things left, see inline, one of which corrects my own earlier suggestion. This now also conflicts with main in test/plugins/nuxt-config.test.ts: main added two files and an issues.files assertion to the nuxt-config fixture (1677171), so keep those and add your two plugin files to its counters.
| const middleware = (dir = 'middleware') => join(dir, `{*,*/index}.${scriptExt}`); | ||
| const pages = (dir = 'pages') => join(dir, '**/*.{vue,jsx,tsx}'); | ||
| const plugins = (dir = 'plugins') => join(dir, '**/*.ts'); | ||
| const plugins = (dir = 'plugins') => join(dir, `{*,*/index}.${scriptExt}`); |
There was a problem hiding this comment.
My earlier suggestion of {*,*/index} was wrong: an alternative containing / keeps the glob from skipping directories, so it reads every directory below plugins and middleware instead of only the top level (see PLUGINS.md). Nuxt itself scans these as two patterns, and the matched files are the same:
| const middleware = (dir = 'middleware') => join(dir, `{*,*/index}.${scriptExt}`); | |
| const pages = (dir = 'pages') => join(dir, '**/*.{vue,jsx,tsx}'); | |
| const plugins = (dir = 'plugins') => join(dir, '**/*.ts'); | |
| const plugins = (dir = 'plugins') => join(dir, `{*,*/index}.${scriptExt}`); | |
| const middleware = (dir = 'middleware') => [join(dir, `*.${scriptExt}`), join(dir, `*/index.${scriptExt}`)]; | |
| const pages = (dir = 'pages') => join(dir, '**/*.{vue,jsx,tsx}'); | |
| const plugins = (dir = 'plugins') => [join(dir, `*.${scriptExt}`), join(dir, `*/index.${scriptExt}`)]; |
The callers then spread them: ...middleware() and ...plugins() in production, and in addAppEntries:
for (const id of middleware(config.dir?.middleware)) inputs.push(toProductionEntry(join(srcDir, id)));
for (const id of plugins(config.dir?.plugins)) inputs.push(toProductionEntry(join(srcDir, id)));| if (typeof id === 'string') addModule(id); | ||
| else if (id && typeof id.src === 'string') addModule(id.src); |
There was a problem hiding this comment.
Nuxt resolves a plugin path without ~/, like my-plugins/custom/deep, from the root directory before trying it as a package. addModule takes it as a package name, so the file is reported as unused and my-plugins as an unlisted dependency. Resolving it as a file first, and only then as a package, handles both (with toDeferResolve added to the import):
| if (typeof id === 'string') addModule(id); | |
| else if (id && typeof id.src === 'string') addModule(id.src); | |
| const src = typeof id === 'string' ? id : id?.src; | |
| if (typeof src === 'string') | |
| inputs.push({ ...toDeferResolve(resolveAlias(src, srcDir, cwd), { dir: cwd }), production: true }); |
| dir: { | ||
| plugins: 'my-plugins', | ||
| }, | ||
| plugins: ['~/my-plugins/custom/deep', { src: '~/my-plugins/custom/client.js', mode: 'client' }], |
There was a problem hiding this comment.
With the string form written without ~/, the existing test covers that case:
| plugins: ['~/my-plugins/custom/deep', { src: '~/my-plugins/custom/client.js', mode: 'client' }], | |
| plugins: ['my-plugins/custom/deep', { src: '~/my-plugins/custom/client.js', mode: 'client' }], |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Plugins listed in the Nuxt config are added as entries, since they can live deeper in the plugins directory. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ins as files first A brace alternative with a slash keeps the glob from skipping directories, so use separate top-level and index patterns, like Nuxt does. Plugin paths in the config without ~/ are resolved from the root directory before falling back to a package. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
9eed213 to
f60c630
Compare
|
Done in
Full |
webpro
left a comment
There was a problem hiding this comment.
Thanks Swastik, Nuxt plugins and middleware written in JavaScript, and app.config, are now found.
|
🚀 This pull request is included in v6.45.0. See Release 6.45.0 for release notes. Using Knip in a commercial project? Please consider becoming a sponsor. |
Nuxt finds plugins, route middleware and
app.configwith every extension innuxt.options.extensions, which defaults to.mjs,.js,.cjs,.mts,.ts,.cts,.tsx,.jsx(plus.vue). In nuxt 4.6.4,resolveAppscansplugins/*{${extensionGlob}}andmiddleware/*{${extensionGlob}}, and finds the app config withfindPath(join(dirs.app, "app.config")).The Nuxt plugin only matched
plugins/**/*.ts,middleware/**/*.tsandapp.config.ts, so in a JavaScript Nuxt app (or one with a stray.jsplugin)plugins/analytics.js,middleware/auth.jsandapp.config.mjswere reported as unused files, along with anything only they import.These three patterns now use
{js,mjs,cjs,jsx,ts,mts,cts,tsx}. I left the server patterns alone in this PR.Verified by adding empty
plugins/analytics.js,middleware/auth.jsandapp.config.mjsto the existingnuxtfixture: the test now expectsprocessed: 10/total: 10, and fails without the change (files: 3). All 13 Nuxt plugin tests pass, andoxfmt --checkis clean.Written with Claude Code (AI). I read the scan code in the nuxt and @nuxt/schema 4.6.4 packages from npm and ran the tests above.