Skip to content

Commit 2be9c1c

Browse files
committed
Add showExports option, expose exports in analysis
1 parent e40789e commit 2be9c1c

6 files changed

Lines changed: 56 additions & 16 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ dependents: 1
127127
- type: Boolean
128128
- default: `false`
129129
- description: Don't itemize dependents in the formatted output
130+
- **showExports** - *optional*
131+
- type: Boolean
132+
- default: `false`
133+
- description: Show used and unused exports
130134
- **writeTo** - *optional*
131135
- type: Function
132136
- default: `null`
@@ -151,6 +155,8 @@ dependents: 1
151155
- **dependents** *(Array)* - list of dependent module ids / paths
152156
- **percent** *(Number)* - percentage of module size relative to entire bundle
153157
- **reduction** *(Number)* - percentage of rendered size reduction
158+
- **usedExports** *(Array)* - list of used named exports
159+
- **unusedExports** *(Array)* - list of unused named exports
154160

155161

156162
## License

index.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const shakenPct = (n, o) => Math.max((100 - ((n / o) * 100)).toFixed(2), 0);
1717
const match = (str, check) => str.indexOf(check) !== -1;
1818

1919
const analyze = (bundle, opts = {}, format = false) => {
20-
let { root, limit, filter, hideDeps } = opts;
20+
let { root, limit, filter, hideDeps, showExports } = opts;
2121
root = root || (process && process.cwd ? process.cwd() : null);
2222
let deps = {};
2323
let bundleSize = 0;
@@ -27,7 +27,14 @@ const analyze = (bundle, opts = {}, format = false) => {
2727

2828
return new Promise((resolve, reject) => {
2929
let modules = bundleModules.map((m, i) => {
30-
let { id, originalLength: origSize, renderedLength, code } = m;
30+
let {
31+
id,
32+
originalLength: origSize,
33+
renderedLength,
34+
code,
35+
usedExports,
36+
unusedExports
37+
} = m;
3138
id = id.replace(root, '');
3239
let size = renderedLength;
3340
if (!size && size !== 0) size = code ? Buffer.byteLength(code, 'utf8') : 0;
@@ -43,7 +50,7 @@ const analyze = (bundle, opts = {}, format = false) => {
4350
deps[d].push(id);
4451
});
4552

46-
return {id, size, origSize}
53+
return {id, size, origSize, usedExports, unusedExports}
4754
}).filter((m) => m);
4855

4956
modules.sort((a, b) => b.size - a.size);
@@ -79,11 +86,22 @@ const analyze = (bundle, opts = {}, format = false) => {
7986
`original size: ${buf}${formatBytes(m.origSize || 'unknown')}\n` +
8087
`code reduction: ${buf}${m.reduction} %\n` +
8188
`dependents: ${buf}${m.dependents.length}\n`;
89+
8290
if (!hideDeps) {
8391
m.dependents.forEach((d) => {
8492
formatted += `${tab}-${buf}${d.replace(root, '')}\n`;
8593
});
8694
}
95+
if (showExports && m.usedExports && m.unusedExports) {
96+
formatted += `used exports: ${buf}${m.usedExports.length}\n`;
97+
m.usedExports.forEach((e) => {
98+
formatted += `${tab}-${buf}${e}\n`;
99+
});
100+
formatted += `unused exports: ${buf}${m.unusedExports.length}\n`;
101+
m.unusedExports.forEach((e) => {
102+
formatted += `${tab}-${buf}${e}\n`;
103+
});
104+
}
87105
formatted += `${borderX}`;
88106
});
89107

@@ -109,6 +127,8 @@ const plugin = (opts = {}) => {
109127
modules.forEach((m) => {
110128
let bm = bundle.modules[m.id];
111129
bm.id = bm.id || m.id;
130+
bm.usedExports = bm.renderedExports || m.renderedExports;
131+
bm.unusedExports = bm.removedExports || m.removedExports;
112132
bm.dependencies = m.dependencies || [];
113133
});
114134
modules = Object.keys(bundle.modules).map((k) => bundle.modules[k]);

module.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const shakenPct = (n, o) => Math.max((100 - ((n / o) * 100)).toFixed(2), 0)
1515
const match = (str, check) => str.indexOf(check) !== -1
1616

1717
export const analyze = (bundle, opts = {}, format = false) => {
18-
let { root, limit, filter, hideDeps } = opts
18+
let { root, limit, filter, hideDeps, showExports } = opts
1919
root = root || (process && process.cwd ? process.cwd() : null)
2020
let deps = {}
2121
let bundleSize = 0
@@ -25,7 +25,14 @@ export const analyze = (bundle, opts = {}, format = false) => {
2525

2626
return new Promise((resolve, reject) => {
2727
let modules = bundleModules.map((m, i) => {
28-
let { id, originalLength: origSize, renderedLength, code } = m
28+
let {
29+
id,
30+
originalLength: origSize,
31+
renderedLength,
32+
code,
33+
usedExports,
34+
unusedExports
35+
} = m
2936
id = id.replace(root, '')
3037
let size = renderedLength
3138
if (!size && size !== 0) size = code ? Buffer.byteLength(code, 'utf8') : 0
@@ -41,7 +48,7 @@ export const analyze = (bundle, opts = {}, format = false) => {
4148
deps[d].push(id)
4249
})
4350

44-
return {id, size, origSize}
51+
return {id, size, origSize, usedExports, unusedExports}
4552
}).filter((m) => m)
4653

4754
modules.sort((a, b) => b.size - a.size)
@@ -77,11 +84,22 @@ export const analyze = (bundle, opts = {}, format = false) => {
7784
`original size: ${buf}${formatBytes(m.origSize || 'unknown')}\n` +
7885
`code reduction: ${buf}${m.reduction} %\n` +
7986
`dependents: ${buf}${m.dependents.length}\n`
87+
8088
if (!hideDeps) {
8189
m.dependents.forEach((d) => {
8290
formatted += `${tab}-${buf}${d.replace(root, '')}\n`
8391
})
8492
}
93+
if (showExports && m.usedExports && m.unusedExports) {
94+
formatted += `used exports: ${buf}${m.usedExports.length}\n`
95+
m.usedExports.forEach((e) => {
96+
formatted += `${tab}-${buf}${e}\n`
97+
})
98+
formatted += `unused exports: ${buf}${m.unusedExports.length}\n`
99+
m.unusedExports.forEach((e) => {
100+
formatted += `${tab}-${buf}${e}\n`
101+
})
102+
}
85103
formatted += `${borderX}`
86104
})
87105

@@ -107,6 +125,8 @@ export const plugin = (opts = {}) => {
107125
modules.forEach((m) => {
108126
let bm = bundle.modules[m.id]
109127
bm.id = bm.id || m.id
128+
bm.usedExports = bm.renderedExports || m.renderedExports
129+
bm.unusedExports = bm.removedExports || m.removedExports
110130
bm.dependencies = m.dependencies || []
111131
})
112132
modules = Object.keys(bundle.modules).map((k) => bundle.modules[k])

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rollup-plugin-analyzer",
3-
"version": "2.0.2",
3+
"version": "2.0.3",
44
"description": "Mad metrics for your rollup bundles, know all the things",
55
"main": "index.js",
66
"module": "module.js",

rollup.config.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
'use strict'
22

33
import { plugin } from './module'
4-
const limitBytes = 1e6
5-
6-
const onAnalysis = ({bundleSize}) => {
7-
if (bundleSize < limitBytes) return
8-
console.log(`Bundle size exceeds ${limitBytes} bytes: ${bundleSize} bytes`)
9-
return process.exit(1)
10-
}
114

125
export default {
13-
plugins: [plugin({onAnalysis})],
6+
plugins: [plugin()],
147
input: 'module.js',
158
output: {
169
file: 'index.js',

test/test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ rollers.forEach(({rollup, version, opts, noTreeshake}) => {
119119
test(`${version}: plugin writes expected results`, async (assert) => {
120120
let results
121121
let writeTo = (r) => { results = r }
122-
let rollOpts = Object.assign({plugins: [plugin({writeTo, version})]}, opts)
122+
let showExports = true
123+
let rollOpts = Object.assign({plugins: [plugin({writeTo, showExports})]}, opts)
123124
let bundle = await rollup(rollOpts)
124125
await bundle.generate({format: 'cjs'})
125126
assert.is(results.substr(0, expectHeader.length), expectHeader)

0 commit comments

Comments
 (0)