Skip to content

Commit e2c932b

Browse files
committed
Add filter, init as default, bump to v1.0.2
1 parent f2a7c4d commit e2c932b

4 files changed

Lines changed: 40 additions & 16 deletions

File tree

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,38 @@ $ npm install --save-dev rollup-analyzer
1010

1111
## api
1212

13-
### init(options)
13+
### rollupAnalyzer
14+
the default exported function is identical to init, and returns the same functions as export {init, formatted, analyze}. So, you can `require('rollup-analyzer')` or `require('rollup-analyzer')({limit: 5})` and use the same functions.
15+
16+
### rollupAnalyzer.init(options)
1417
set options to use in analysis (this step is optional)
1518
- **options** *(Object)*
1619
- **limit** - *optional*
1720
- type: Number
1821
- default: `null`
1922
- description: Limit number of files to output analysis of, sorted by DESC size
23+
- **filter** - *optional*
24+
- type: Array | String
25+
- default: `null`
26+
- description: Filter to only show imports matching the specified name(s)
2027
- **root** - *optional*
2128
- type: String
2229
- default: `process.cwd()`
2330
- description: Application directory, used to display file paths relatively
2431

25-
### formatted(bundle)
32+
### rollupAnalyzer.formatted(bundle)
2633
returns Promise which resolves with well formatted analysis string (for CLI printing)
2734
- **bundle** *(Rollup Bundle)* - *required*
2835

29-
### analyze(bundle)
36+
### rollupAnalyzer.analyze(bundle)
3037
returns Promise which resolves with array of objects describing each imported file
3138
- **bundle** *(Rollup Bundle)* - *required*
3239

3340
## usage
3441

3542
```js
36-
const rollupAnalyzer = require('rollup-analyzer')
43+
const rollupAnalyzer = require('rollup-analyzer')({limit: 5})
3744
rollup.rollup({/*...*/}).then((bundle) => {
38-
// init is an optional step that can be used to set options
39-
rollupAnalyzer.init({limit: 5})
4045
// print console optimized analysis string
4146
rollupAnalyzer.formatted(bundle).then(console.log).catch(console.error)
4247
})

index-es6.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const tab = ' '
66
const borderX = `${Array(30).join('-')}\n`
77
let proc = process
88
let root = proc && proc.cwd ? proc.cwd() : null
9-
let limit
9+
let limit, filter
1010

1111
// Helpers
1212
const formatBytes = (bytes) => {
@@ -19,15 +19,24 @@ const formatBytes = (bytes) => {
1919
}
2020

2121
// Exports
22-
export {init, analyze, formatted}
22+
export default analyzer
2323

2424
// Main
2525
function init (opts) {
2626
opts = opts || {}
2727
limit = opts.limit
28+
filter = opts.filter
2829
root = opts.root || root
2930
}
3031

32+
function analyzer (opts) {
33+
init(opts)
34+
return {init, formatted, analyze}
35+
}
36+
analyzer.init = init
37+
analyzer.formatted = formatted
38+
analyzer.analyze = analyze
39+
3140
function formatted (bndl) { return analyze(bndl, true) }
3241

3342
function analyze (bundle, format) {
@@ -38,13 +47,15 @@ function analyze (bundle, format) {
3847
let id = m.id.replace(root, '')
3948
let size = Buffer.byteLength(m.code, 'utf8') || 0
4049
bundleSize += size
50+
if (Array.isArray(filter) && !filter.some((f) => id.match(f))) return null
51+
if (filter && !id.match(filter)) return null
4152
m.dependencies.forEach((d) => {
4253
d = d.replace(root, '')
4354
deps[d] = deps[d] || []
4455
deps[d].push(id)
4556
})
4657
return {id, size}
47-
})
58+
}).filter((m) => m)
4859
modules.sort((a, b) => b.size - a.size)
4960
if (limit) modules = modules.slice(0, limit)
5061
modules.forEach((m) => {

index.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
'use strict';
22

3-
Object.defineProperty(exports, '__esModule', { value: true });
4-
53
// Setup
64
const buf = ' ';
75
const tab = ' ';
86
const borderX = `${Array(30).join('-')}\n`;
97
let proc = process;
108
let root = proc && proc.cwd ? proc.cwd() : null;
119
let limit;
10+
let filter;
1211

1312
// Helpers
1413
const formatBytes = (bytes) => {
@@ -24,9 +23,18 @@ const formatBytes = (bytes) => {
2423
function init (opts) {
2524
opts = opts || {};
2625
limit = opts.limit;
26+
filter = opts.filter;
2727
root = opts.root || root;
2828
}
2929

30+
function analyzer$1 (opts) {
31+
init(opts);
32+
return {init, formatted, analyze}
33+
}
34+
analyzer$1.init = init;
35+
analyzer$1.formatted = formatted;
36+
analyzer$1.analyze = analyze;
37+
3038
function formatted (bndl) { return analyze(bndl, true) }
3139

3240
function analyze (bundle, format) {
@@ -37,13 +45,15 @@ function analyze (bundle, format) {
3745
let id = m.id.replace(root, '');
3846
let size = Buffer.byteLength(m.code, 'utf8') || 0;
3947
bundleSize += size;
48+
if (Array.isArray(filter) && !filter.some((f) => id.match(f))) return null
49+
if (filter && !id.match(filter)) return null
4050
m.dependencies.forEach((d) => {
4151
d = d.replace(root, '');
4252
deps[d] = deps[d] || [];
4353
deps[d].push(id);
4454
});
4555
return {id, size}
46-
});
56+
}).filter((m) => m);
4757
modules.sort((a, b) => b.size - a.size);
4858
if (limit) modules = modules.slice(0, limit);
4959
modules.forEach((m) => {
@@ -68,6 +78,4 @@ function analyze (bundle, format) {
6878
})
6979
}
7080

71-
exports.init = init;
72-
exports.analyze = analyze;
73-
exports.formatted = formatted;
81+
module.exports = analyzer$1;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rollup-analyzer",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Analyze file sizes of rollup bundled imports",
55
"main": "index.js",
66
"jsnext:main": "index-es6.js",

0 commit comments

Comments
 (0)