Skip to content

Commit 4a5f535

Browse files
committed
Add ability to filter with callback, 2.0.2
1 parent 9795af7 commit 4a5f535

7 files changed

Lines changed: 64 additions & 13 deletions

File tree

README.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,43 @@ $ npm install --save-dev rollup-plugin-analyzer
3030
### from rollup config
3131
```js
3232
import { plugin as analyze } from 'rollup-analyzer-plugin'
33-
const opts = {limit: 5, filter: [], root: __dirname}
33+
3434
export default {
3535
entry: 'module.js',
3636
dest: 'index.js',
3737
format: 'cjs',
38-
plugins: [analyze(opts)]
38+
plugins: [analyze()]
3939
}
4040
```
4141

4242
### from build script
4343
```js
4444
import { rollup } from 'rollup'
4545
import { plugin as analyze } from 'rollup-analyzer-plugin'
46-
const opts = {limit: 5, filter: [], root: __dirname}
4746

4847
rollup({
4948
entry: 'main.js',
50-
plugins: [analyze(opts)]
49+
plugins: [analyze()]
50+
}).then(...)
51+
```
52+
53+
### CI usage example
54+
```js
55+
import { rollup } from 'rollup'
56+
import { plugin as analyze } from 'rollup-analyzer-plugin'
57+
const limitBytes = 1e6
58+
59+
const onAnalysis = ({bundleSize}) => {
60+
if (bundleSize < limitBytes) return
61+
console.log(`Bundle size exceeds ${limitBytes} bytes: ${bundleSize} bytes`)
62+
return process.exit(1)
63+
}
64+
import { rollup } from 'rollup'
65+
import { plugin as analyze } from 'rollup-analyzer-plugin'
66+
67+
rollup({
68+
entry: 'main.js',
69+
plugins: [analyze({onAnalysis})]
5170
}).then(...)
5271
```
5372

@@ -97,9 +116,10 @@ dependents: 1
97116
- default: `null`
98117
- description: Limit number of files to output analysis of, sorted by DESC size
99118
- **filter** - *optional*
100-
- type: Array | String
119+
- type: Array | String | Function
101120
- default: `null`
102121
- description: Filter to only show imports matching the specified name(s)
122+
- notes: Function receives `module` object specified below, should return boolean
103123
- **root** - *optional*
104124
- type: String
105125
- default: `process.cwd()`

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const formatBytes = (bytes) => {
1414
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
1515
};
1616
const shakenPct = (n, o) => Math.max((100 - ((n / o) * 100)).toFixed(2), 0);
17+
const match = (str, check) => str.indexOf(check) !== -1;
1718

1819
const analyze = (bundle, opts = {}, format = false) => {
1920
let { root, limit, filter, hideDeps } = opts;
@@ -32,8 +33,8 @@ const analyze = (bundle, opts = {}, format = false) => {
3233
bundleSize += size;
3334
bundleOrigSize += origSize;
3435

35-
if (Array.isArray(filter) && !filter.some((f) => id.match(f))) return null
36-
if (typeof filter === 'string' && !id.match(filter)) return null
36+
if (Array.isArray(filter) && !filter.some((f) => match(id, f))) return null
37+
if (typeof filter === 'string' && !match(id, filter)) return null
3738

3839
m.dependencies.forEach((d) => {
3940
d = d.replace(root, '');
@@ -51,6 +52,7 @@ const analyze = (bundle, opts = {}, format = false) => {
5152
m.percent = Math.min(((m.size / bundleSize) * 100).toFixed(2), 100);
5253
m.reduction = shakenPct(m.size, m.origSize);
5354
});
55+
if (typeof filter === 'function') modules = modules.filter(filter);
5456

5557
let bundleReduction = shakenPct(bundleSize, bundleOrigSize);
5658

module.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const formatBytes = (bytes) => {
1212
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
1313
}
1414
const shakenPct = (n, o) => Math.max((100 - ((n / o) * 100)).toFixed(2), 0)
15+
const match = (str, check) => str.indexOf(check) !== -1
1516

1617
export const analyze = (bundle, opts = {}, format = false) => {
1718
let { root, limit, filter, hideDeps } = opts
@@ -30,8 +31,8 @@ export const analyze = (bundle, opts = {}, format = false) => {
3031
bundleSize += size
3132
bundleOrigSize += origSize
3233

33-
if (Array.isArray(filter) && !filter.some((f) => id.match(f))) return null
34-
if (typeof filter === 'string' && !id.match(filter)) return null
34+
if (Array.isArray(filter) && !filter.some((f) => match(id, f))) return null
35+
if (typeof filter === 'string' && !match(id, filter)) return null
3536

3637
m.dependencies.forEach((d) => {
3738
d = d.replace(root, '')
@@ -49,6 +50,7 @@ export const analyze = (bundle, opts = {}, format = false) => {
4950
m.percent = Math.min(((m.size / bundleSize) * 100).toFixed(2), 100)
5051
m.reduction = shakenPct(m.size, m.origSize)
5152
})
53+
if (typeof filter === 'function') modules = modules.filter(filter)
5254

5355
let bundleReduction = shakenPct(bundleSize, bundleOrigSize)
5456

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.1",
3+
"version": "2.0.2",
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: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
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+
}
11+
412
export default {
5-
plugins: [plugin()],
13+
plugins: [plugin({onAnalysis})],
614
input: 'module.js',
715
output: {
816
file: 'index.js',

test/test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ rollers.forEach(({rollup, version, opts, noTreeshake}) => {
8989
assert.is((await analyze(bundle, {filter: 'import-b'})).modules.length, 1)
9090
})
9191

92+
test(`${version}: filter with callback works`, async (assert) => {
93+
let bundle = await rollup(opts)
94+
let noMatch = (m) => m.id.indexOf('jimmy') !== -1
95+
let hasMatch = (m) => m.id.indexOf('import-b') !== -1
96+
assert.is((await analyze(bundle, {filter: noMatch})).modules.length, 0)
97+
assert.is((await analyze(bundle, {filter: hasMatch})).modules.length, 1)
98+
})
99+
92100
test(`${version}: root works as expected`, async (assert) => {
93101
let bundle = await rollup(opts)
94102
assert.not(
@@ -114,7 +122,6 @@ rollers.forEach(({rollup, version, opts, noTreeshake}) => {
114122
let rollOpts = Object.assign({plugins: [plugin({writeTo})]}, opts)
115123
let bundle = await rollup(rollOpts)
116124
await bundle.generate({format: 'cjs'})
117-
if (version === 'latest') console.log(results)
118125
assert.is(results.substr(0, expectHeader.length), expectHeader)
119126
})
120127

@@ -130,5 +137,17 @@ rollers.forEach(({rollup, version, opts, noTreeshake}) => {
130137
let imported = results.find((r) => r.id.indexOf('import-a') !== -1)
131138
assert.is(imported.size, 27)
132139
})
140+
141+
test(`${version}: treeshaken bundle filters with callback`, async (assert) => {
142+
let results
143+
let filter = (m) => m.size > 2000
144+
let onAnalysis = (r) => { results = r.modules }
145+
let plugins = [plugin({onAnalysis, filter})]
146+
let rollOpts = Object.assign({}, opts, {plugins})
147+
let bundle = await rollup(rollOpts)
148+
let output = {file: join(fixtures, 'output.js'), format: 'cjs'}
149+
await bundle.write(output)
150+
assert.is(results.length, 1)
151+
})
133152
}
134153
})

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2246,7 +2246,7 @@ rimraf@^2.6.1:
22462246
"@types/estree" "0.0.39"
22472247
"@types/node" "*"
22482248

2249-
rollup@^0.60.1:
2249+
rollup@latest:
22502250
version "0.60.1"
22512251
resolved "https://registry.yarnpkg.com/rollup/-/rollup-0.60.1.tgz#07cb66153f1541d5f7e82b8393b405c31647dae9"
22522252
dependencies:

0 commit comments

Comments
 (0)