forked from ai/size-limit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load-plugins.js
36 lines (30 loc) · 812 Bytes
/
load-plugins.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
function toArray(obj) {
return typeof obj === 'object' ? Object.keys(obj) : []
}
class Plugins {
constructor(list) {
this.list = list
this.isEmpty = list.length === 0
}
has(type) {
return this.list.some(i => i.name === `@size-limit/${type}`)
}
}
module.exports = function loadPlugins(pkg) {
if (!pkg || !pkg.packageJson) return new Plugins([])
let list = toArray(pkg.packageJson.dependencies)
.concat(toArray(pkg.packageJson.devDependencies))
.concat(toArray(pkg.packageJson.optionalDependencies))
.filter(i => i.startsWith('@size-limit/'))
.reduce(
(all, i) =>
all.concat(
require(require.resolve(i, {
paths: [process.cwd()]
}))
),
[]
)
return new Plugins(list)
}
module.exports.Plugins = Plugins