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
6.18k stars 140 forks source link

Using Knip with `vite.config` #696

Closed awixor closed 4 hours ago

awixor commented 3 weeks ago

This is my vite.config:

// https://vitejs.dev/config/#conditional-config
export default defineConfig(({ command }) => {
    if (command === 'serve') {
        return serveConfig;
    }

    if (command === 'build') {
        return buildConfig;
    }

    throw new Error('unrecognized vite command');
});

When running pnpm run knip it throws Error: unrecognized vite command, because knip uses the dev command.

is there a way to fix this without return serveConfig as a default in vite.config

webpro commented 3 weeks ago

Unfortunately there is not, Knip runs all command variations: https://github.com/webpro-nl/knip/blob/978577c364a4a95c3a0d3de67013a8097730ee52/packages/knip/src/plugins/vitest/index.ts#L64

It does this to find all dependencies. There's currently no custom options per plugin or so.

One could argue "dev" is a valid command, though, or is it not?

webpro commented 4 hours ago

Closing due to inactivity, and a fix can be implemented downstream.