-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (48 loc) · 1.77 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
const chalk = require('chalk')
const PLUGIN_NAME = 'loaderCountWebpackPlugin'
class LoderCountWebpackPlugin {
constructor() {
this.cache = Object.create(null)
}
// Define the `apply` method
apply(compiler) {
// Specify the event hook to attach to
compiler.hooks.compilation.tap(
'PLUGIN_NAME',
(compilation) => {
compilation.hooks.buildModule.tap('PLUGIN_NAME', (module) => {
const { loaders, resource } = module
if (loaders && loaders.length) {
for (const loader of loaders) {
const match = loader.loader.match(/node\_modules\/([a-zA-Z\-\_]+)\//)
const loaderName = match && match[1] ? match[1] : loader.loader
const sourceIsInNodeModules = /node_modules/.test(resource)
if (!this.cache[loaderName]) {
this.cache[loaderName] = {
count: 0,
inNodeModules: []
}
}
this.cache[loaderName].count++
sourceIsInNodeModules && this.cache[loaderName].inNodeModules.push(resource)
}
}
})
}
)
compiler.hooks.done.tap(PLUGIN_NAME, () => {
Object.keys(this.cache).forEach(key => {
const { count } = this.cache[key]
console.log(chalk.green(`loader: ${key} has processed ${count} ${count > 1 ? 'files' : 'file'}`))
const fileLength = this.cache[key].inNodeModules.length
if (fileLength) {
console.log(chalk.yellow(`And ${fileLength} ${fileLength > 1 ? 'files are' : 'file is' } in node_modules`))
this.cache[key].inNodeModules.forEach(file => {
console.log(chalk.magenta(`==> ${file}`))
})
}
})
})
}
}
module.exports = LoderCountWebpackPlugin