Skip to content

Find JavaScript plugins, middleware and app.config in the Nuxt plugin - #2312

Merged
webpro merged 4 commits into
webpro-nl:mainfrom
SwastikTripathi:fix/nuxt-js-plugins-middleware
Nov 9, 2026
Merged

webpro merged 4 commits into
webpro-nl:mainfrom
SwastikTripathi:fix/nuxt-js-plugins-middleware

Conversation

@SwastikTripathi

@SwastikTripathi SwastikTripathi commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Nuxt finds plugins, route middleware and app.config with every extension in nuxt.options.extensions, which defaults to .mjs, .js, .cjs, .mts, .ts, .cts, .tsx, .jsx (plus .vue). In nuxt 4.6.4, resolveApp scans plugins/*{${extensionGlob}} and middleware/*{${extensionGlob}}, and finds the app config with findPath(join(dirs.app, "app.config")).

The Nuxt plugin only matched plugins/**/*.ts, middleware/**/*.ts and app.config.ts, so in a JavaScript Nuxt app (or one with a stray .js plugin) plugins/analytics.js, middleware/auth.js and app.config.mjs were 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.js and app.config.mjs to the existing nuxt fixture: the test now expects processed: 10 / total: 10, and fails without the change (files: 3). All 13 Nuxt plugin tests pass, and oxfmt --check is 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.

@pkg-pr-new

pkg-pr-new Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/knip@2312
npm i https://pkg.pr.new/@knip/language-server@2312
npm i https://pkg.pr.new/@knip/mcp@2312

commit: f60c630

@webpro webpro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The extensions match what Nuxt scans, but the plugin and middleware patterns now pick up more files than Nuxt loads, see inline.

Comment thread packages/knip/src/plugins/nuxt/index.ts Outdated
Comment on lines +43 to +45
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}`);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 webpro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread packages/knip/src/plugins/nuxt/index.ts Outdated
Comment on lines +43 to +45
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}`);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
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)));

Comment thread packages/knip/src/plugins/nuxt/index.ts Outdated
Comment on lines +159 to +160
if (typeof id === 'string') addModule(id);
else if (id && typeof id.src === 'string') addModule(id.src);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Suggested change
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' }],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the string form written without ~/, the existing test covers that case:

Suggested change
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' }],

SwastikTripathi and others added 4 commits November 9, 2026 10:19
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>
@SwastikTripathi
SwastikTripathi force-pushed the fix/nuxt-js-plugins-middleware branch from 9eed213 to f60c630 Compare November 9, 2026 04:51
@SwastikTripathi

Copy link
Copy Markdown
Contributor Author

Done in f60c6309, rebased on main:

  • middleware() and plugins() now return the two patterns (*.ext and */index.ext), spread in production and looped in addAppEntries.
  • Config plugins go through toDeferResolve(..., { dir: cwd }) with production: true, so my-plugins/custom/deep resolves as a file first. The fixture uses that form now. With the old addModule call, the nuxt-config test fails with my-plugins/custom/deep.ts reported as unused.
  • Kept the two files and the issues.files assertion from main in nuxt-config.test.ts and bumped processed/total to 12.

Full node scripts/run-test.js --runtime node passes after pnpm build, and oxlint and oxfmt are clean.

@webpro webpro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Swastik, Nuxt plugins and middleware written in JavaScript, and app.config, are now found.

@webpro
webpro merged commit d3181ef into webpro-nl:main Nov 9, 2026
33 checks passed
@webpro

webpro commented Nov 10, 2026

Copy link
Copy Markdown
Member

🚀 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants