Skip to content

Commit 0bdc851

Browse files
committed
Let onAnalysis and formatted coexist, addresses #2
1 parent 77ed765 commit 0bdc851

6 files changed

Lines changed: 224 additions & 103 deletions

File tree

CHANGELOG.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
### Changelog
2+
3+
All notable changes to this project will be documented in this file. Dates are displayed in UTC.
4+
5+
Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
6+
7+
#### [Unreleased](https://github.com/doesdev/rollup-plugin-analyzer/compare/2.0.4...HEAD)
8+
9+
- Add CONTRIBUTING.md [`77ed765`](https://github.com/doesdev/rollup-plugin-analyzer/commit/77ed7650e1140cf8bba89722b304ca00a0c7c804)
10+
11+
#### [2.0.4](https://github.com/doesdev/rollup-plugin-analyzer/compare/2.0.3...2.0.4)
12+
13+
> 3 July 2018
14+
15+
- Update multi-out tests, failing, skipped [`8813ec7`](https://github.com/doesdev/rollup-plugin-analyzer/commit/8813ec7786825b1256c9af9331f55941e1ad718e)
16+
- Handle bundles with multiple outputs, 2.0.4 [`12512c9`](https://github.com/doesdev/rollup-plugin-analyzer/commit/12512c9b2a7d2f2c61467b295add5286a79abae2)
17+
- Test with experimentalCodeSplitting enabled as well [`fc54b1b`](https://github.com/doesdev/rollup-plugin-analyzer/commit/fc54b1be5ed8b360ce30cdf511f5671a8f974923)
18+
- Fix module name in examples [`dafa44e`](https://github.com/doesdev/rollup-plugin-analyzer/commit/dafa44edb863d6e5d13953a11aa79e920d989158)
19+
20+
#### [2.0.3](https://github.com/doesdev/rollup-plugin-analyzer/compare/2.0.2...2.0.3)
21+
22+
> 9 June 2018
23+
24+
- Add showExports option, expose exports in analysis [`2be9c1c`](https://github.com/doesdev/rollup-plugin-analyzer/commit/2be9c1c9f82d7a01d3e638971b2127c363b9cfc9)
25+
- Some cleanup and formatting tweaks [`e40789e`](https://github.com/doesdev/rollup-plugin-analyzer/commit/e40789eb1047d0ed04d30b5254a9373034453325)
26+
- Add module count to formatted output [`e880a9d`](https://github.com/doesdev/rollup-plugin-analyzer/commit/e880a9d2fca46777199017abb83c133878c20c5b)
27+
- Fix redundant imports in CI example [`a5328de`](https://github.com/doesdev/rollup-plugin-analyzer/commit/a5328de5c0fdc170c347f8e502b84fbb27886219)
28+
- Include module count in example output [`86b0d55`](https://github.com/doesdev/rollup-plugin-analyzer/commit/86b0d55744e3502c58a53afde8c6e8f080965fe2)
29+
30+
#### [2.0.2](https://github.com/doesdev/rollup-plugin-analyzer/compare/2.0.1...2.0.2)
31+
32+
> 9 June 2018
33+
34+
- Add ability to filter with callback, 2.0.2 [`4a5f535`](https://github.com/doesdev/rollup-plugin-analyzer/commit/4a5f535b1cc5b2040747511be4d79aa63f7e9635)
35+
36+
#### [2.0.1](https://github.com/doesdev/rollup-plugin-analyzer/compare/2.0.0...2.0.1)
37+
38+
> 8 June 2018
39+
40+
- Use updated example output [`9795af7`](https://github.com/doesdev/rollup-plugin-analyzer/commit/9795af7a95d03b38aa43d4cef599724ea43cb3e1)
41+
42+
#### [2.0.0](https://github.com/doesdev/rollup-plugin-analyzer/compare/2.0.0-beta.1...2.0.0)
43+
44+
> 8 June 2018
45+
46+
- 2.0.0 [`#5`](https://github.com/doesdev/rollup-plugin-analyzer/pull/5)
47+
- Beef up test fixtures [`8e5d30f`](https://github.com/doesdev/rollup-plugin-analyzer/commit/8e5d30f6c341e0adc70299ba3c880da48514ed7c)
48+
- Epic commit, 97% moar awesomer [`ee6a7dd`](https://github.com/doesdev/rollup-plugin-analyzer/commit/ee6a7dd10c35876443c57bf8f68e3affa51a6802)
49+
- Update from rollup-analyzer to rollup-plugin-analyzer [`675dcdc`](https://github.com/doesdev/rollup-plugin-analyzer/commit/675dcdc79cc009f4800971b9683473842deafe72)
50+
- Rework analysis object API [`6f98a0b`](https://github.com/doesdev/rollup-plugin-analyzer/commit/6f98a0bbf69be452f2a38a92c4cd66a54efa7e15)
51+
- Add overall code reduction metrics [`2023900`](https://github.com/doesdev/rollup-plugin-analyzer/commit/20239001e974ef6d981caf53b32051eb6b0ded3b)
52+
- Formatting tweaks [`de2252a`](https://github.com/doesdev/rollup-plugin-analyzer/commit/de2252af1a49d73243883719cf49ad0d714344ca)
53+
- Ensure we don't fallback if renderedLength === 0 [`7dc5b4e`](https://github.com/doesdev/rollup-plugin-analyzer/commit/7dc5b4e7da1539ad588b05f8329b2541756f30bc)
54+
- Clamp percentages [`9dbbc79`](https://github.com/doesdev/rollup-plugin-analyzer/commit/9dbbc792a72fa047db017af181043f43d3c07e5d)
55+
- Set root default again [`e0262a1`](https://github.com/doesdev/rollup-plugin-analyzer/commit/e0262a13c083f6a16d7693d78c5d0dc2f43cffb9)
56+
57+
#### [2.0.0-beta.1](https://github.com/doesdev/rollup-plugin-analyzer/compare/1.1.0...2.0.0-beta.1)
58+
59+
> 7 June 2018
60+
61+
- Switch to yarn with multiple rollup versions to test [`3cd30cc`](https://github.com/doesdev/rollup-plugin-analyzer/commit/3cd30cc40118401f988234d3f76e4c3f3023f84f)
62+
- Update deps, add failing test for tree shaking [`9d26c38`](https://github.com/doesdev/rollup-plugin-analyzer/commit/9d26c388c347d130700004198032004375a0f6d8)
63+
- Tests against many rollup versions >= 0.40.x [`b52c793`](https://github.com/doesdev/rollup-plugin-analyzer/commit/b52c793844e2217027caac01f63b273937bd8fd3)
64+
- Start working on 2.0.0 [`46c03cb`](https://github.com/doesdev/rollup-plugin-analyzer/commit/46c03cb4f9b6577abf6745f11b29b41ccbfe55af)
65+
- Reflect treeshaking, only in plugin currently [`1ddf5f7`](https://github.com/doesdev/rollup-plugin-analyzer/commit/1ddf5f72a0c65088cb9484b00b0b4163633f1b3d)
66+
- Rework test file structure [`2767bfc`](https://github.com/doesdev/rollup-plugin-analyzer/commit/2767bfcf905bd9cf409b4e2ac1cedf40ea9b363e)
67+
- Import from two files in test [`73e524b`](https://github.com/doesdev/rollup-plugin-analyzer/commit/73e524b00033221395bf9167ffe58a2d6ba3ad7e)
68+
- Start updating API in readme [`6c78820`](https://github.com/doesdev/rollup-plugin-analyzer/commit/6c788207e9ed7eb981e1a72dac3eab655edd1ce9)
69+
- Integrate plugin, add related (failing) test [`9b9c2ca`](https://github.com/doesdev/rollup-plugin-analyzer/commit/9b9c2ca173702f2b7dbb3d4924cbd0fabb5e5b60)
70+
- Bundle with each test [`4f9f437`](https://github.com/doesdev/rollup-plugin-analyzer/commit/4f9f4370d615c0375ff97ce7376bbcb25f906e1d)
71+
- Test that it works after bundle generated [`eed1240`](https://github.com/doesdev/rollup-plugin-analyzer/commit/eed1240ea2f6f824cff6ba6abc97e264f524b236)
72+
- Update locked rollup to 0.60.0 [`96b1cde`](https://github.com/doesdev/rollup-plugin-analyzer/commit/96b1cdebcc47c5913d306fb13ff43ee0a627dbc3)
73+
- Fix node 6 compatibility [`286b8b3`](https://github.com/doesdev/rollup-plugin-analyzer/commit/286b8b32061950d5745d7003dac02f6e76b49d4f)
74+
- Ensure formatted string is as expected in test [`746db48`](https://github.com/doesdev/rollup-plugin-analyzer/commit/746db48771bd8b434b833dfa9c7df7314ce48d32)
75+
- Add travis config [`160859e`](https://github.com/doesdev/rollup-plugin-analyzer/commit/160859e9d1589a63189db11436befdf526ad4175)
76+
- Tweak test title [`0acd1e3`](https://github.com/doesdev/rollup-plugin-analyzer/commit/0acd1e3da6c33cf6e4332ae29fec7c807b53af33)
77+
- Fix travis urls [`46b5bc5`](https://github.com/doesdev/rollup-plugin-analyzer/commit/46b5bc59b3963ef20f305c3a0605632bc2f858b9)
78+
- Add travis badge [`cfda8b7`](https://github.com/doesdev/rollup-plugin-analyzer/commit/cfda8b71e17d29c22988c72315178e65b294e94b)
79+
80+
#### [1.1.0](https://github.com/doesdev/rollup-plugin-analyzer/compare/v1.0.3...1.1.0)
81+
82+
> 21 October 2017
83+
84+
- Add tests, fix filter bugs, 1.1.0 [`db98ac3`](https://github.com/doesdev/rollup-plugin-analyzer/commit/db98ac34e751e939caed7602b3a344a756be448d)
85+
86+
#### v1.0.3
87+
88+
> 19 August 2017
89+
90+
- Add info about plugin, config, v1.0.3 [`2a34c2d`](https://github.com/doesdev/rollup-plugin-analyzer/commit/2a34c2d4b22d7ba639e1146a1588f9e0c6b65dcc)
91+
- Add deps badge [`596c470`](https://github.com/doesdev/rollup-plugin-analyzer/commit/596c470c74bc035360f925b75314f5bae0da7aa8)
92+
- Add filter, init as default, bump to v1.0.2 [`e2c932b`](https://github.com/doesdev/rollup-plugin-analyzer/commit/e2c932b64ee641000294d7d00ba1e6dcffe2eedc)
93+
- Add percent, v1.0.1 [`f2a7c4d`](https://github.com/doesdev/rollup-plugin-analyzer/commit/f2a7c4dc8e661b8a75d190e01cca13af80a76cbd)
94+
- Add index to commit from build script [`6c3bf72`](https://github.com/doesdev/rollup-plugin-analyzer/commit/6c3bf72a66262f8571dd698528a1569545279c08)
95+
- Make root path an init option [`6804deb`](https://github.com/doesdev/rollup-plugin-analyzer/commit/6804debb4f5c0ed2f6013be237066511a50ec8a4)
96+
- Initial commit [`a6b3b9b`](https://github.com/doesdev/rollup-plugin-analyzer/commit/a6b3b9bd8aefd4b2e430fa19177bb809ab937a56)

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const onAnalysis = ({bundleSize}) => {
6464

6565
rollup({
6666
entry: 'main.js',
67-
plugins: [analyze({onAnalysis})]
67+
plugins: [analyze({onAnalysis, skipFormatted: true})]
6868
}).then(...)
6969
```
7070

@@ -131,6 +131,10 @@ dependents: 1
131131
- type: Boolean
132132
- default: `false`
133133
- description: Show used and unused exports
134+
- **skipFormatted** - *optional*
135+
- type: Boolean
136+
- default: `false`
137+
- description: Don't output formatted string
134138
- **writeTo** - *optional*
135139
- type: Function
136140
- default: `null`
@@ -141,12 +145,12 @@ dependents: 1
141145
- type: Function
142146
- default: `null`
143147
- description: Callback to be invoked with analysis object
144-
- notes: if this is set the formatted string will not be output
145148
- callback invoked with:
146149
- **analysisObject** *(Object)*
147150
- **bundleSize** *(Number)* - rendered bundle size in bytes
148151
- **bundleOrigSize** *(Number)* - original bundle size in bytes
149152
- **bundleReduction** *(Number)* - percentage of rendered bundle size reduction
153+
- **moduleCount** *(Number)* - Count of all included modules
150154
- **modules** *(Array)* - array of `module` analysis objects
151155
- **module** *(Object)*
152156
- **id** *(String)* - path of module / rollup module id

index.js

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,50 @@ const formatBytes = (bytes) => {
1616
const shakenPct = (n, o) => Math.max((100 - ((n / o) * 100)).toFixed(2), 0);
1717
const match = (str, check) => str.indexOf(check) !== -1;
1818

19-
const analyze = (bundle, opts = {}, format = false) => {
20-
let { root, limit, filter, hideDeps, showExports } = opts;
19+
const reporter = (analysis, opts) => {
20+
let formatted = `` +
21+
`${borderX}` +
22+
`Rollup File Analysis\n` +
23+
`${borderX}` +
24+
`bundle size: ${formatBytes(analysis.bundleSize)}\n` +
25+
`original size: ${formatBytes(analysis.bundleOrigSize)}\n` +
26+
`code reduction: ${analysis.bundleReduction} %\n` +
27+
`module count: ${analysis.moduleCount}\n` +
28+
`${borderX}`;
29+
30+
analysis.modules.forEach((m) => {
31+
formatted += `` +
32+
`file: ${buf}${m.id}\n` +
33+
`bundle space: ${buf}${m.percent} %\n` +
34+
`rendered size: ${buf}${formatBytes(m.size)}\n` +
35+
`original size: ${buf}${formatBytes(m.origSize || 'unknown')}\n` +
36+
`code reduction: ${buf}${m.reduction} %\n` +
37+
`dependents: ${buf}${m.dependents.length}\n`;
38+
39+
let { hideDeps, root, showExports } = opts || {};
40+
if (!hideDeps) {
41+
m.dependents.forEach((d) => {
42+
formatted += `${tab}-${buf}${d.replace(root, '')}\n`;
43+
});
44+
}
45+
if (showExports && m.usedExports && m.unusedExports) {
46+
formatted += `used exports: ${buf}${m.usedExports.length}\n`;
47+
m.usedExports.forEach((e) => {
48+
formatted += `${tab}-${buf}${e}\n`;
49+
});
50+
formatted += `unused exports: ${buf}${m.unusedExports.length}\n`;
51+
m.unusedExports.forEach((e) => {
52+
formatted += `${tab}-${buf}${e}\n`;
53+
});
54+
}
55+
formatted += `${borderX}`;
56+
});
57+
58+
return formatted
59+
};
60+
61+
const analyze = (bundle, opts = {}) => {
62+
let { root, limit, filter } = opts;
2163
root = root || (process && process.cwd ? process.cwd() : null);
2264
let deps = {};
2365
let bundleSize = 0;
@@ -64,65 +106,32 @@ const analyze = (bundle, opts = {}, format = false) => {
64106

65107
let bundleReduction = shakenPct(bundleSize, bundleOrigSize);
66108

67-
if (!format) {
68-
return resolve({bundleSize, bundleOrigSize, bundleReduction, modules})
69-
}
70-
71-
let formatted = `` +
72-
`${borderX}` +
73-
`Rollup File Analysis\n` +
74-
`${borderX}` +
75-
`bundle size: ${formatBytes(bundleSize)}\n` +
76-
`original size: ${formatBytes(bundleOrigSize)}\n` +
77-
`code reduction: ${bundleReduction} %\n` +
78-
`module count: ${moduleCount}\n` +
79-
`${borderX}`;
80-
81-
modules.forEach((m) => {
82-
formatted += `` +
83-
`file: ${buf}${m.id}\n` +
84-
`bundle space: ${buf}${m.percent} %\n` +
85-
`rendered size: ${buf}${formatBytes(m.size)}\n` +
86-
`original size: ${buf}${formatBytes(m.origSize || 'unknown')}\n` +
87-
`code reduction: ${buf}${m.reduction} %\n` +
88-
`dependents: ${buf}${m.dependents.length}\n`;
89-
90-
if (!hideDeps) {
91-
m.dependents.forEach((d) => {
92-
formatted += `${tab}-${buf}${d.replace(root, '')}\n`;
93-
});
94-
}
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-
}
105-
formatted += `${borderX}`;
106-
});
107-
108-
return resolve(formatted)
109+
return resolve({
110+
bundleSize, bundleOrigSize, bundleReduction, modules, moduleCount
111+
})
109112
})
110113
};
111114

112-
const formatted = (bndl, opts) => analyze(bndl, opts, true);
115+
const formatted = (bndl, opts) => {
116+
return analyze(bndl, opts).then((analysis) => reporter(analysis, opts))
117+
};
113118

114119
const plugin = (opts = {}) => {
115-
let cb = opts.writeTo || (opts.stdout ? console.log : console.error);
116-
if (opts.onAnalysis) cb = opts.onAnalysis;
120+
let writeTo = opts.writeTo || (opts.stdout ? console.log : console.error);
117121
let depMap = {};
118122

123+
let onAnalysis = (analysis) => {
124+
if (typeof opts.onAnalysis === 'function') opts.onAnalysis(analysis);
125+
if (!opts.skipFormatted) writeTo(reporter(analysis, opts));
126+
};
127+
119128
let runAnalysis = (out, bundle, isWrite) => new Promise((resolve, reject) => {
120129
resolve();
121130
if (out.bundle) bundle = out.bundle;
122131
let modules = bundle.modules;
123132

124133
if (Array.isArray(modules)) {
125-
return analyze({modules}, opts, !opts.onAnalysis).then(cb)
134+
return analyze({modules}, opts).then(onAnalysis)
126135
}
127136

128137
modules = Object.keys(modules).map((k) => {
@@ -131,7 +140,7 @@ const plugin = (opts = {}) => {
131140
module.unusedExports = module.removedExports;
132141
return module
133142
});
134-
return analyze({modules}, opts, !opts.onAnalysis).then(cb)
143+
return analyze({modules}, opts).then(onAnalysis)
135144
});
136145

137146
return {
@@ -148,6 +157,7 @@ const plugin = (opts = {}) => {
148157
}
149158
};
150159

160+
exports.reporter = reporter;
151161
exports.analyze = analyze;
152162
exports.formatted = formatted;
153163
exports.plugin = plugin;

0 commit comments

Comments
 (0)