Skip to content

Webpack plugin to detect unused files and unused exports in used files

License

Notifications You must be signed in to change notification settings

lakerswgq/webpack-deadcode-plugin

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Webpack Deadcode Plugin

Webpack plugin to detect unused files and unused exports in used files

npm node deps licenses

Installation

Via npm:

$ npm install @tfboys/webpack-deadcode-plugin --save-dev

Via yarn:

$ yarn add -D @tfboys/webpack-deadcode-plugin

output

New Feature

This plugin supports delete unused files and output path of unused files to a json file, then you can easily exclude files you want to ingore.

prompt

Usage

The plugin will report unused files and unused exports into your terminal but those are not part of your webpack build process, therefore, it will not fail your build (warning you). Simple add into your webpack config as follows:

✍️ If you use babel-loader, you have to set modules: false to make it works

# in .babelrc
{
  "presets": [
    ["env", { modules: false }]
  ]
}

# or in webpack.config.js -> module/rules
{
  loader: 'babel-loader',
  options: {
    presets: [
      ['env', { modules: false }
    ]
  }
}

Webpack 3

const DeadCodePlugin = require('webpack-deadcode-plugin');

const webpackConfig = {
  ...
  plugins: [
    new DeadCodePlugin({
      patterns: [
        'src/**/*.(js|jsx|css)',
      ],
      exclude: [
        '**/*.(stories|spec).(js|jsx)',
      ],
    })
  ]
}

Webpack 4

const DeadCodePlugin = require('webpack-deadcode-plugin');

const webpackConfig = {
  ...
  optimization: {
    usedExports: true,
  },
  plugins: [
    new DeadCodePlugin({
      patterns: [
        'src/**/*.(js|jsx|css)',
      ],
      exclude: [
        '**/*.(stories|spec).(js|jsx)',
      ],
    })
  ]
}

Using non-existent css class names

To detect using non-existent class names in your codebase, you have to use es6-css-loader instead of style-loader/mini-css-extract-plugin. They are quite similiar in term of api except es6-css-loader supports to detect non-existent css class names.

non-existent css class names

You can check samples folder, how to to config webpack-deadcode-plugin and es6-css-loader.

Typescript

Using with typescript loader (ts-loader, awesome-typescript-loader), if you enable transpileOnly/happyPackMode, output might be not correct due to this issue. In case of incorrect output, the workaround solution is disabling transpileOnly, it will slow down webpack compiling time.

✍ Under some circumstances and production mode, if your output displays incorrect unused files, we encourage switching to awesome-typescript-loader.

Configuration

new DeadCodePlugin(options);

options.patterns (default: ["**/*.*"])

The array of patterns to look for unused files and unused export in used files. Directly pass to fast-glob

options.exclude (default: [])

The array of patterns to not look at.

options.outputFile

The path of file which contains unused files' path

options.context

Current working directoy for patterns above. If you don't set explicitly, your webpack context will be used.

options.failOnHint (default: false)

Deadcode does not interrupt the compilation by default. If you want to cancel the compilation, set it true, it throws a fatal error and stops the compilation.

options.detectUnusedFiles (default: true)

Whether to run unused files detection or not.

options.detectUnusedExport (default: true)

Whether to run unsed export detection or not.

About

Webpack plugin to detect unused files and unused exports in used files

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%