Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ module.exports = {
exclude: ['*.test.js'],
// Root directory (optional)
root: __dirname,
// Export file path (optional)
outputFilePath: path.resolve(__dirname, 'UNUSED_FILES.MD'),
}),
],
};
Expand All @@ -39,6 +41,7 @@ module.exports = {
- `root` : root directory that will be use to display relative paths instead of absolute ones (see below)
- `failOnUnused`: whether or not the build should fail if unused files are found (defaults to `false`)
- `useGitIgnore`: whether or not to respect `.gitignore` file (defaults to `true`)
- `outputFilePath`: Full path to the file where you want the results be printed to.

With root

Expand Down
23 changes: 20 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const { searchFiles } = require('./lib/utils');
Expand All @@ -8,6 +9,7 @@ function UnusedPlugin(options) {
this.root = options.root;
this.failOnUnused = options.failOnUnused || false;
this.useGitIgnore = options.useGitIgnore || true;
this.outputFilePath = options.outputFilePath;
}

UnusedPlugin.prototype.apply = function apply(compiler) {
Expand Down Expand Up @@ -55,6 +57,11 @@ function display(filesByDirectory) {
if (!allFiles.length) {
return [];
}

let outputString = `## Unused Files
${allFiles.length} unused source files found.
`;

process.stdout.write('\n');
process.stdout.write(chalk.green('\n*** Unused Plugin ***\n'));
process.stdout.write(
Expand All @@ -66,12 +73,22 @@ function display(filesByDirectory) {
const relative = this.root
? path.relative(this.root, directory)
: directory;

process.stdout.write(chalk.blue(`\n● ${relative}\n`));
files.forEach(file => process.stdout.write(
chalk.yellow(` • ${path.relative(directory, file)}\n`),
));
outputString += `\n### ${relative}\n`;

files.forEach((file) => {
process.stdout.write(
chalk.yellow(` • ${path.relative(directory, file)}\n`),
);
outputString += ` - ${path.relative(directory, file)}\n`;
});
});
process.stdout.write(chalk.green('\n*** Unused Plugin ***\n\n'));

if (this.outputFilePath) {
fs.writeFile(this.outputFilePath, outputString, 'utf8');
}

return allFiles;
}