/vite-plugin-deadfile

find unused source files with vite

Primary LanguageTypeScriptMIT LicenseMIT

vite-plugin-deadfile npm

This plugin helps you find unused source file(dead files) in your project.

// vite.config.js
import { defineConfig } from 'vite';
import deadFile from 'vite-plugin-deadfile';

export default defineConfig({
  plugins: [deadFile()],
});

Output format

All source files: 123
Used source files: 120
Unused source files: 3
  ./path/to/unused/file-a
  ./path/to/unused/file-b
  ./path/to/unused/file-c

Options

include

An array of source files/folders to be compared with files referenced during compilation.

If no value is provided, all files in the root directory will be considered as source files.

import { defineConfig } from 'vite';
import deadFile from 'vite-plugin-deadfile';

export default defineConfig({
  plugins: [deadFile({
    include: ['src']
  })],
});

projectRoot

A string. Project root directory. Can be an absolute path, or a path relative to the current working directory. Defaults to '.'

exclude

An array of files/folders to be configured as non-source files, so they won't appear in the result.

import { defineConfig } from 'vite';
import deadFile from 'vite-plugin-deadfile';

export default defineConfig({
  plugins: [deadFile({
    include: ['src'],
    exclude: ['src/vendors']
  })],
});

node_modules and hidden files (file with a name start with .) are excluded by default.

output

Output file name may only contains number/letter/hyphen/underscore.

If no output file name is provided, the result will be printed on console.

import { defineConfig } from 'vite';
import deadFile from 'vite-plugin-deadfile';

export default defineConfig({
  plugins: [deadFile({
    output: 'dead-files.txt'
  })],
});

Caveats

Pure Type Reference can NOT be traced

Imported typescript files only have their interfaces or types being referenced will not be marked as used.

In the following example, interface-a.ts will NOT be marked as used.

// interface-a.ts
export interface A {}

// index.ts
import type { A } from '.interface-a';
export function main(param: A) {}

This is because vite use rollup to build a project. Since rollup only build javascript files, a typescript file must be transformed into javascript before handing to rollup, vite does this with esbuild plugin in transform hook:

  // vite/src/node/plugins/esbuild.ts
  async transform(code, id) {
    if (filter(id) || filter(cleanUrl(id))) {
      // transform ts into js with esbuild
      const result = await transformWithEsbuild(code, id, transformOptions)
      if (result.warnings.length) {
        result.warnings.forEach((m) => {
          this.warn(prettifyMessage(m, code))
        })
      }
      if (jsxInject && jsxExtensionsRE.test(id)) {
        result.code = jsxInject + ';' + result.code
      }
      return {
        code: result.code,
        map: result.map,
      }
    }
  },

Similarly, @rollup/plugin-typescript uses the content of pre-compiled javascript files of requested typescript files in load hook to do the trick.

Either way, the imports of pure type references are lost after files are compiled into javascript. So they will be wrongly considered as not used.

There are several tsconfig about the elimination of type imports: verbatimModuleSyntax, preserveValueImports, importsNotUsedAsValues. It seems they are either not useful or conflicting with vite, so it not possible to trace the references of pure types for now.

Check before delete

Some unreferenced files such as markdowns may be useful, check again before deleting those files.