Webpack plugin to detect unused files and unused exports in used files
Via npm:
$ npm install webpack-deadcode-plugin --save-dev
Via yarn:
$ yarn add -D webpack-deadcode-plugin
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)',
],
})
]
}
new DeadCodePlugin(options)
The array of patterns to look for unused files and unused export in used files.
- Default:
["**/*.*"]
- Directly pass to
fast-glob
The array of patterns to not look at.
Current working directoy for patterns above. If you don't set explicitly, your webpack context will be used.