forked from latentflip/webpack-unused
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·105 lines (84 loc) · 2.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env node
'use strict';
const Path = require('path');
const Promisify = require('es6-promisify');
const Glob2 = Promisify(require('glob'));
const Glob = require('glob');
// webpack root? ideally start from package.json location I think
const cwd = process.cwd();
const argv = require('yargs')
.usage('Usage: webpack --stats | $0 [options]')
.example('webpack --stats | $0 -s src', 'Check for unused files in the `src/` directory')
.alias('s', 'src')
.describe('s', 'Directory of source code')
.default('s', '.')
.help('h')
.alias('h', 'help')
.argv;
// specify which directory to look in for source files
const srcDir = Path.resolve(argv.src);
const isWebpackLocal = (path) => {
return (path.indexOf('./') === 0 && path.indexOf('./~/') === -1);
};
const selectLocalModules = (webpack) => {
let modules = webpack.modules;
if (!modules && webpack.children && webpack.children.length) {
modules = [];
webpack.children.forEach((child) => {
child.chunks.forEach((chunk) => {
modules = modules.concat(chunk.modules);
});
});
}
return modules.filter((module) => isWebpackLocal(module.name))
.map((module) => Path.join(cwd, module.name));
};
const findAllLocalFiles = (cwd) => {
return Glob2('!(node_modules)/**/*.*', { cwd: srcDir })
.then((files) => files.map((f) => Path.join(srcDir, f)));
};
const parseStdin = () => {
return new Promise((resolve, reject) => {
let data = '';
process.stdin.setEncoding('utf8');
process.stdin.on('readable', () => {
const chunk = process.stdin.read();
if (chunk === null && data === '') {
console.error('The output of webpack --json must be piped to webpack-unused');
process.exit(1);
}
if (chunk !== null) {
data += chunk.toString();
}
});
process.stdin.on('end', () => {
try {
resolve(JSON.parse(data));
} catch (e) {
console.error('Warning: output does not parse as json');
console.error('Attempting to trim to json');
const from = data.indexOf('{');
const to = data.lastIndexOf('}');
try {
if (from === -1 || to === -1) {
throw new Error('NOT_JSON');
}
resolve(JSON.parse(data.slice(from, to + 1)));
} catch (e) {
console.error('Output does not appear to be json at all');
process.exit(1);
}
}
});
});
};
Promise.all([
parseStdin().then(selectLocalModules),
findAllLocalFiles(cwd)
]).then((args) => {
const webpackFiles = args[0];
const localFiles = args[1];
const unused = localFiles.filter((file) => webpackFiles.indexOf(file) === -1)
.map((file) => `./${Path.relative(cwd, file)}`);
console.log(unused.join('\n'));
});