webpro-nl / knip

✂️ Find unused files, dependencies and exports in your JavaScript and TypeScript projects. Knip it before you ship it!
https://knip.dev
ISC License
7.06k stars 177 forks source link

🐛 Webpack config not found when it's not in root folder #781

Closed lucasrmendonca closed 2 months ago

lucasrmendonca commented 2 months ago

Prerequisites

Reproduction url

https://codesandbox.io/p/devbox/x53r2v

Reproduction access

Description of the issue

If we move the webpack.config.js file to a sub-folder such as in path .src/some-folder/webpack.config.js, Knip says the dependencies mentioned in that file are unused.

If this is a valid issue, and you need help with a PR, I am willing to work on it, but I'd appreciate some guidance on how to fix it ...

Current output:

Unused devDependencies (3)
esbuild         package.json
esbuild-loader  package.json
webpack-cli     package.json

Expected output:

✂️  Excellent, Knip found no issues.
webpro commented 2 months ago

You can modify the location where the webpack plugin should look for the config file like so:

export default {
  entry: [],
  webpack: { config: ['path/to/webpack.config.js'] }
};

or shorthand:

export default {
  entry: [],
  webpack: 'path/to/webpack.config.js'
};
lucasrmendonca commented 2 months ago

Hi @webpro thank you for the quick response.

I've updated the knip.config.js file from the sandbox to be:

import path from 'node:path';

const PROJECT_PATH = path.resolve(process.cwd(), 'src', 'project-that-uses-webpack');

export default {
    entry: ['*/**/*.js'],
    webpack: path.resolve(PROJECT_PATH, 'webpack.config.js'),
};

Now it recognized esbuild-loader and webpack-cli but it's still not recognizing esbuild:

Unused devDependencies (1)
esbuild  package.json

Also, what if we changed the default path for the webpack plugin to an array of globs, so it recursively checks for webpack configs in every folder. Would this be something interesting or is it a bad idea?

"config": [
     "*/**/webpack.config*.{js,ts,mjs,cjs,mts,cts}"
]
webpro commented 2 months ago

Now it recognized esbuild-loader and webpack-cli but it's still not recognizing esbuild:

That looks like expected behavior. Why/how do you think it should recognize `esbuild?

Unused devDependencies (1)
esbuild  package.json

Also, what if we changed the default path for the webpack plugin to an array of globs, so it recursively checks for webpack configs in every folder. Would this be something interesting or is it a bad idea?

"config": [
     "*/**/webpack.config*.{js,ts,mjs,cjs,mts,cts}"
]

You can configure this as you like, but I think it's a bad idea. Especially the */** at the start is unnecessarily expensive. More specific means less work and less surprises.

lucasrmendonca commented 2 months ago

That looks like expected behavior. Why/how do you think it should recognize `esbuild?

You're correct! esbuild used to be a peerDependency of esbuild-loader, but in most recent versions it's not a peer dep anymore.

You can configure this as you like, but I think it's a bad idea. Especially the */** at the start is unnecessarily expensive. More specific means less work and less surprises.

That makes sense. I guess if the user wants, they can import the glob library and write an expensive checker themselves if the trade-off is worth it, but having it as default inside the library might be too error-prone.