unused-webpack-plugin
Advanced tools
Comparing version 2.0.0 to 2.1.0
59
index.js
@@ -13,29 +13,36 @@ const path = require('path'); | ||
UnusedPlugin.prototype.apply = function(compiler) { | ||
compiler.hooks.emit.tapAsync( | ||
'UnusedPlugin', | ||
function(compilation, callback) { | ||
// Files used by Webpack during compilation | ||
const usedModules = Array.from(compilation.fileDependencies) | ||
.filter(file => | ||
this.sourceDirectories.some(dir => file.indexOf(dir) !== -1) | ||
) | ||
.reduce((obj, item) => { | ||
obj[item] = true; | ||
return obj; | ||
}, {}); | ||
// Go through sourceDirectories to find all source files | ||
Promise.all( | ||
this.sourceDirectories.map(directory => | ||
recursive(directory, this.exclude) | ||
) | ||
const checkUnused = (compilation, callback) => { | ||
// Files used by Webpack during compilation | ||
const usedModules = Array.from(compilation.fileDependencies) | ||
.filter(file => | ||
this.sourceDirectories.some(dir => file.indexOf(dir) !== -1) | ||
) | ||
// Find unused source files | ||
.then(files => | ||
files.map(array => array.filter(file => !usedModules[file])) | ||
) | ||
.then(display.bind(this)) | ||
.then(continueOrFail.bind(this, this.failOnUnused, compilation)) | ||
.then(callback); | ||
}.bind(this) | ||
); | ||
.reduce((obj, item) => { | ||
obj[item] = true; | ||
return obj; | ||
}, {}); | ||
// Go through sourceDirectories to find all source files | ||
Promise.all( | ||
this.sourceDirectories.map(directory => | ||
recursive(directory, this.exclude) | ||
) | ||
) | ||
// Find unused source files | ||
.then(files => | ||
files.map(array => array.filter(file => !usedModules[file])) | ||
) | ||
.then(display.bind(this)) | ||
.then(continueOrFail.bind(this, this.failOnUnused, compilation)) | ||
.then(callback); | ||
} | ||
// webpack 4 | ||
if (compiler.hooks && compiler.hooks.emit) { | ||
compiler.hooks.emit.tapAsync( | ||
'UnusedPlugin', | ||
checkUnused | ||
); | ||
// webpack 3 | ||
} else { | ||
compiler.plugin('emit', checkUnused) | ||
} | ||
}; | ||
@@ -42,0 +49,0 @@ |
{ | ||
"name": "unused-webpack-plugin", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "A webpack plugin to find unused modules/source files", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
232219
109