webpack-deadcode-plugin

raw JSON →
0.1.17 verified Sat Apr 25 auth: no javascript

Webpack plugin (v0.1.17) that detects unused files and unused exports in used files during the webpack build process. It outputs warnings to the terminal without failing the build unless configured otherwise. Works with webpack 3, 4, and 5 via peer dependency. Key differentiators: supports both file and export detection, integrates with TypeScript loaders (with caveats), offers granular logging and JSON export. Active development with periodic releases.

error Cannot find module 'webpack-deadcode-plugin'
cause Package not installed or CommonJS require used in ESM module context.
fix
npm install webpack-deadcode-plugin --save-dev
error TypeError: DeadCodePlugin is not a constructor
cause Missing 'new' keyword or destructured import incorrectly.
fix
Use: const DeadCodePlugin = require('webpack-deadcode-plugin'); new DeadCodePlugin(options);
error Unused files not reported
cause Patterns option too restrictive or exclude option incorrectly set.
fix
Ensure patterns match your source files, e.g., patterns: ['src/**/*.(js|jsx|ts|tsx|css)']
error Unused exports not detected when using babel
cause Babel transforms modules and loses tracking. Need modules: false.
fix
Set modules: false in babel presets as described in documentation.
gotcha When using babel-loader, must set modules: false in .babelrc or babel-loader options to prevent module transformation that would break dead code detection.
fix In .babelrc: { "presets": [["env", { "modules": false }]] } or in webpack config: { loader: 'babel-loader', options: { presets: [['env', { modules: false }]] } }
gotcha With TypeScript loaders (ts-loader, awesome-typescript-loader), enabling transpileOnly/happyPackMode may produce incorrect output due to ts-loader issue #783.
fix Disable transpileOnly or switch to awesome-typescript-loader for production.
breaking The plugin was originally designed for webpack 3 and 4; webpack 5 support was added in v0.1.13, but users must ensure version compatibility.
fix Upgrade to v0.1.13 or later for webpack 5 compatibility.
gotcha The plugin outputs warnings only and does not fail the build by default. To fail on unused code, set failOnHint: true.
fix Add failOnHint: true to options if you want the build to error out.
npm install webpack-deadcode-plugin
yarn add webpack-deadcode-plugin
pnpm add webpack-deadcode-plugin

Basic webpack config with deadcode plugin to detect unused files/exports.

// webpack.config.js
const DeadCodePlugin = require('webpack-deadcode-plugin');

module.exports = {
  entry: './src/index.js',
  output: { filename: 'bundle.js' },
  plugins: [
    new DeadCodePlugin({
      patterns: ['src/**/*.(js|jsx|css)'],
      exclude: ['**/*.(stories|spec).(js|jsx)'],
      failOnHint: true,
    }),
  ],
};