-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodule.js
More file actions
147 lines (128 loc) · 4.66 KB
/
module.js
File metadata and controls
147 lines (128 loc) · 4.66 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'use strict'
const buf = ' '
const tab = ' '
const borderX = `${Array(30).join('-')}\n`
const formatBytes = (bytes) => {
if (bytes === 0) return '0 Byte'
let k = 1000
let dm = 3
let sizes = ['Bytes', 'KB', 'MB', 'GB']
let i = Math.floor(Math.log(bytes) / Math.log(k))
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
}
const shakenPct = (n, o) => Math.max((100 - ((n / o) * 100)).toFixed(2), 0)
const match = (str, check) => str.indexOf(check) !== -1
export const analyze = (bundle, opts = {}, format = false) => {
let { root, limit, filter, hideDeps, showExports } = opts
root = root || (process && process.cwd ? process.cwd() : null)
let deps = {}
let bundleSize = 0
let bundleOrigSize = 0
let bundleModules = bundle.modules || []
let moduleCount = bundleModules.length
return new Promise((resolve, reject) => {
let modules = bundleModules.map((m, i) => {
let {
id,
originalLength: origSize,
renderedLength,
code,
usedExports,
unusedExports
} = m
id = id.replace(root, '')
let size = renderedLength
if (!size && size !== 0) size = code ? Buffer.byteLength(code, 'utf8') : 0
bundleSize += size
bundleOrigSize += origSize
if (Array.isArray(filter) && !filter.some((f) => match(id, f))) return null
if (typeof filter === 'string' && !match(id, filter)) return null
m.dependencies.forEach((d) => {
d = d.replace(root, '')
deps[d] = deps[d] || []
deps[d].push(id)
})
return {id, size, origSize, usedExports, unusedExports}
}).filter((m) => m)
modules.sort((a, b) => b.size - a.size)
if (limit || limit === 0) modules = modules.slice(0, limit)
modules.forEach((m) => {
m.dependents = deps[m.id] || []
m.percent = Math.min(((m.size / bundleSize) * 100).toFixed(2), 100)
m.reduction = shakenPct(m.size, m.origSize)
})
if (typeof filter === 'function') modules = modules.filter(filter)
let bundleReduction = shakenPct(bundleSize, bundleOrigSize)
if (!format) {
return resolve({bundleSize, bundleOrigSize, bundleReduction, modules})
}
let formatted = `` +
`${borderX}` +
`Rollup File Analysis\n` +
`${borderX}` +
`bundle size: ${formatBytes(bundleSize)}\n` +
`original size: ${formatBytes(bundleOrigSize)}\n` +
`code reduction: ${bundleReduction} %\n` +
`module count: ${moduleCount}\n` +
`${borderX}`
modules.forEach((m) => {
formatted += `` +
`file: ${buf}${m.id}\n` +
`bundle space: ${buf}${m.percent} %\n` +
`rendered size: ${buf}${formatBytes(m.size)}\n` +
`original size: ${buf}${formatBytes(m.origSize || 'unknown')}\n` +
`code reduction: ${buf}${m.reduction} %\n` +
`dependents: ${buf}${m.dependents.length}\n`
if (!hideDeps) {
m.dependents.forEach((d) => {
formatted += `${tab}-${buf}${d.replace(root, '')}\n`
})
}
if (showExports && m.usedExports && m.unusedExports) {
formatted += `used exports: ${buf}${m.usedExports.length}\n`
m.usedExports.forEach((e) => {
formatted += `${tab}-${buf}${e}\n`
})
formatted += `unused exports: ${buf}${m.unusedExports.length}\n`
m.unusedExports.forEach((e) => {
formatted += `${tab}-${buf}${e}\n`
})
}
formatted += `${borderX}`
})
return resolve(formatted)
})
}
export const formatted = (bndl, opts) => analyze(bndl, opts, true)
export const plugin = (opts = {}) => {
let cb = opts.writeTo || (opts.stdout ? console.log : console.error)
if (opts.onAnalysis) cb = opts.onAnalysis
let depMap = {}
let runAnalysis = (out, bundle, isWrite) => new Promise((resolve, reject) => {
resolve()
if (out.bundle) bundle = out.bundle
let modules = bundle.modules
if (Array.isArray(modules)) {
return analyze({modules}, opts, !opts.onAnalysis).then(cb)
}
modules = Object.keys(modules).map((k) => {
let module = Object.assign(modules[k], depMap[k] || {})
module.usedExports = module.renderedExports
module.unusedExports = module.removedExports
return module
})
return analyze({modules}, opts, !opts.onAnalysis).then(cb)
})
return {
name: 'rollup-plugin-analyzer',
transformChunk: (_a, _b, chunk) => new Promise((resolve, reject) => {
resolve(null)
if (!chunk || !chunk.orderedModules) return
chunk.orderedModules.forEach(({id, dependencies}) => {
depMap[id] = {id, dependencies: dependencies.map((d) => d.id)}
})
}),
generateBundle: runAnalysis,
ongenerate: runAnalysis
}
}