vite-plugin-unused-code
raw JSON → 0.1.8 verified Mon Apr 27 auth: no javascript
A Vite/Rollup plugin to detect unused files and unused exports in used files (dead code elimination analysis). Current stable version is 0.1.8, released March 2026, with frequent bug fixes and Vite 8/Rolldown compatibility updates. Ported from webpack-deadcode-plugin; uses micromatch patterns for file matching. Key differentiator: provides both unused file and unused export detection in a single plugin for Vite/Rollup ecosystems. Limitations: cannot detect unused CSS or TypeScript types, and unused exports will not be reported in Rolldown (Vite 8+).
Common errors
error TypeError: (0 , vite_plugin_unused_code.default) is not a function ↓
cause Using CommonJS require() on an ESM-only package.
fix
Use import statement: import unusedCode from 'vite-plugin-unused-code'
error Error: The plugin 'vite-plugin-unused-code' is not compatible with Vite 8. Please upgrade to a newer version. ↓
cause Using an older version of the plugin with Vite 8 (Rolldown).
fix
Upgrade to vite-plugin-unused-code >=0.1.7
error The exclude option is deprecated and will be removed in a future version. Use micromatch negation patterns instead. ↓
cause Using the deprecated 'exclude' option.
fix
Replace exclude with patterns: patterns: ['**/*.ts', '!ignore/**']
Warnings
gotcha Cannot detect unused CSS or TypeScript type exports – only JavaScript/TypeScript runtime exports are checked. ↓
fix Manually review or use additional tools (e.g., PurgeCSS, ts-prune) for CSS and type unused detection.
breaking Rolldown (Vite 8+) does not report removed exports during tree shaking; unused exports will never be shown. ↓
fix Use detectUnusedFiles only, or downgrade to Vite 7 or earlier for unused export detection.
deprecated The 'exclude' option is deprecated; use micromatch negation patterns in 'patterns' instead. ↓
fix Replace exclude with patterns like ['!excluded/**'].
gotcha Files matched by patterns are only considered unused if they have no imports or are not referenced. Dynamic imports (import()) may not be fully tracked. ↓
fix Verify detection results manually for dynamic imports.
Install
npm install vite-plugin-unused-code yarn add vite-plugin-unused-code pnpm add vite-plugin-unused-code Imports
- default (unusedCode) wrong
const unusedCode = require('vite-plugin-unused-code')correctimport unusedCode from 'vite-plugin-unused-code' - VitePluginUnusedCodeOptions wrong
import { VitePluginUnusedCodeOptions } from 'vite-plugin-unused-code'correctimport type { VitePluginUnusedCodeOptions } from 'vite-plugin-unused-code' - default export with options wrong
unusedCode.new({ patterns: ['src/**/*.*'] })correctunusedCode({ patterns: ['src/**/*.*'] })
Quickstart
// vite.config.js
import { defineConfig } from 'vite';
import unusedCode from 'vite-plugin-unused-code';
export default defineConfig({
plugins: [
unusedCode({
patterns: ['src/**/*.*'],
exclude: ['src/legacy/**'],
detectUnusedFiles: true,
detectUnusedExport: true,
failOnHint: false,
exportJSON: false,
log: 'unused',
}),
],
});