-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (77 loc) · 2.84 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
const path = require("path");
const fs = require("fs");
const rm = require('rimraf').sync
const micromatch = require('micromatch')
let join = path.join;
/**
* @param startPath 起始目录文件夹路径
* @returns {Array}
*/
function findSync(startPath) {
let result = [];
function finder(path) {
let files = fs.readdirSync(path);
files.forEach((val) => {
let fPath = join(path, val);
let stats = fs.statSync(fPath);
if (stats.isDirectory()) finder(fPath);
if (stats.isFile()) result.push(fPath);
});
}
finder(startPath);
return result;
}
const isPathStringRE = /^[./\w]+$/
class ExcludeUnusedFilesPlugin {
constructor(options = {}){
this.options = options
}
apply(compiler) {
let includeFiles,excludeFiles
Array.isArray(this.options.include)
? includeFiles = this.options.include
: includeFiles = [ this.options.include || path.resolve("src") ]
Array.isArray(this.options.exclude)
? excludeFiles = this.options.exclude
: excludeFiles = [ this.options.exclude ]
let fileNames = includeFiles.reduce((c,v)=>{
if(!v) return c
return c.concat(findSync(v))
},[])
if(!fileNames.length) {
console.error(`Can't resolve include files with Option ${this.options.include}`)
}
fileNames = fileNames.map(e => path.relative(path.resolve(), e)) // exclude只对根目录下的路径做匹配
excludeFiles.forEach(pattern => {
if(!pattern) return
Object.prototype.toString.call(pattern) === "[object String]"
? isPathStringRE.test(pattern)
? fileNames = fileNames.filter(e => !new RegExp(pattern).test(e))
: fileNames = micromatch.not([...fileNames], pattern)
: Object.prototype.toString.call(pattern) === "[object RegExp]"
? fileNames = fileNames.filter(e => !pattern.test(e))
: console.error(`Your exclude option ${pattern} dosen't work, exclude options only accept path-string, regexp-expression and micromath-string.`)
})
if(!fileNames.length) {
console.error(`Your exclude options has excluded all include files.`)
}
fileNames = new Set([...fileNames.map(e => path.resolve(e))]) // 匹配完成后还原为绝对路径
compiler.hooks.compilation.tap("ExcludeUnusedFilesPlugin", (compilation) => {
compilation.hooks.buildModule.tap(
"ExcludeUnusedFilesPlugin",
(normalModule) => {
let absPath = normalModule.resource.split("?")[0];
fileNames.has(absPath) && fileNames.delete(absPath);
}
);
});
compiler.hooks.done.tap("ExcludeUnusedFilesPlugin", () => {
if(this.options.needRM){
fileNames.forEach(p => rm(p))
}else{
fs.writeFileSync(path.resolve('.','unused-files-report.json'),JSON.stringify([...fileNames], null, 2))
}
});
}
}
module.exports = ExcludeUnusedFilesPlugin;