Skip to content

Commit a6b3b9b

Browse files
committed
Initial commit
0 parents  commit a6b3b9b

7 files changed

Lines changed: 290 additions & 0 deletions

File tree

.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
*.pid.lock
11+
12+
# Directory for instrumented libs generated by jscoverage/JSCover
13+
lib-cov
14+
15+
# Coverage directory used by tools like istanbul
16+
coverage
17+
18+
# nyc test coverage
19+
.nyc_output
20+
21+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
22+
.grunt
23+
24+
# node-waf configuration
25+
.lock-wscript
26+
27+
# Compiled binary addons (http://nodejs.org/api/addons.html)
28+
build/Release
29+
30+
# Dependency directories
31+
node_modules
32+
jspm_packages
33+
34+
# Optional npm cache directory
35+
.npm
36+
37+
# Optional REPL history
38+
.node_repl_history

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Andrew Carpenter
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# rollup-analyzer [![NPM version](https://badge.fury.io/js/rollup-analyzer.svg)](https://npmjs.org/package/rollup-analyzer) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)
2+
3+
> Analyze file sizes of rollup bundled imports
4+
5+
## install
6+
7+
```sh
8+
$ npm install --save-dev rollup-analyzer
9+
```
10+
11+
## api
12+
13+
### init(options)
14+
set options to use in analysis (this step is optional)
15+
- **options** *(Object)*
16+
- **limit** - *optional*
17+
- type: Number
18+
- default: `null`
19+
- description: Limit number of files to output analysis of, sorted by DESC size
20+
21+
### formatted(bundle)
22+
returns Promise which resolves with well formatted analysis string (for CLI printing)
23+
- **bundle** *(Rollup Bundle)* - *required*
24+
25+
### analyze(bundle)
26+
returns Promise which resolves with array of objects describing each imported file
27+
- **bundle** *(Rollup Bundle)* - *required*
28+
29+
## usage
30+
31+
```js
32+
const rollupAnalyzer = require('rollup-analyzer')
33+
rollup.rollup({/*...*/}).then((bundle) => {
34+
// init is an optional step that can be used to set options
35+
rollupAnalyzer.init({limit: 5})
36+
// print console optimized analysis string
37+
rollupAnalyzer.formatted(bundle).then(console.log).catch(console.error)
38+
})
39+
40+
// Results in ...
41+
/*
42+
-----------------------------
43+
Rollup file analysis
44+
-----------------------------
45+
file: \node_modules\html5-history-api\history.js
46+
size: 38.502 KB
47+
dependents: 1
48+
- \app\modules\page.js
49+
-----------------------------
50+
file: \node_modules\pikaday\pikaday.js
51+
size: 34.683 KB
52+
dependents: 1
53+
- \app\helpers\transformer.js
54+
-----------------------------
55+
...
56+
*/
57+
```
58+
59+
## License
60+
61+
MIT © [Andrew Carpenter](https://github.com/doesdev)

build.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict'
2+
3+
// Setup
4+
const rollup = require('rollup').rollup
5+
const dest = 'index.js'
6+
7+
// Main
8+
rollup({entry: 'index-es6.js'}).then((b) => b.write({format: 'cjs', dest}))

index-es6.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict'
2+
3+
// Setup
4+
const buf = ' '
5+
const tab = ' '
6+
const borderX = `${Array(30).join('-')}\n`
7+
let limit
8+
9+
// Helpers
10+
const formatBytes = (bytes) => {
11+
if (bytes === 0) return '0 Byte'
12+
let k = 1000
13+
let dm = 3
14+
let sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
15+
let i = Math.floor(Math.log(bytes) / Math.log(k))
16+
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
17+
}
18+
19+
// Exports
20+
export {init, analyze, formatted}
21+
22+
// Main
23+
function init (opts) {
24+
opts = opts || {}
25+
limit = opts.limit
26+
}
27+
28+
function formatted (bndl) { return analyze(bndl, true) }
29+
30+
function analyze (bundle, format) {
31+
let deps = {}
32+
return new Promise((resolve, reject) => {
33+
let modules = bundle.modules.map((m, i) => {
34+
let id = m.id.replace(__dirname, '')
35+
m.dependencies.forEach((d) => {
36+
d = d.replace(__dirname, '')
37+
deps[d] = deps[d] || []
38+
deps[d].push(id)
39+
})
40+
return {id, size: Buffer.byteLength(m.code, 'utf8') || 0}
41+
})
42+
modules.sort((a, b) => b.size - a.size)
43+
if (limit) modules = modules.slice(0, limit)
44+
modules.forEach((m) => { m.dependents = deps[m.id] || [] })
45+
if (!format) return resolve(modules)
46+
let heading = `Rollup file analysis\n`
47+
let formatted = `${borderX}${heading}${borderX}`
48+
modules.forEach((m) => {
49+
formatted += `file:${buf}${m.id}\n`
50+
formatted += `size:${buf}${formatBytes(m.size)}\n`
51+
formatted += `dependents:${buf}${m.dependents.length}\n`
52+
m.dependents.forEach((d) => {
53+
formatted += `${tab}-${buf}${d.replace(__dirname, '')}\n`
54+
})
55+
formatted += `${borderX}`
56+
})
57+
return resolve(formatted)
58+
})
59+
}

index.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, '__esModule', { value: true });
4+
5+
// Setup
6+
const buf = ' ';
7+
const tab = ' ';
8+
const borderX = `${Array(30).join('-')}\n`;
9+
let limit;
10+
11+
// Helpers
12+
const formatBytes = (bytes) => {
13+
if (bytes === 0) return '0 Byte'
14+
let k = 1000;
15+
let dm = 3;
16+
let sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
17+
let i = Math.floor(Math.log(bytes) / Math.log(k));
18+
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
19+
};
20+
21+
// Main
22+
function init (opts) {
23+
opts = opts || {};
24+
limit = opts.limit;
25+
}
26+
27+
function formatted (bndl) { return analyze(bndl, true) }
28+
29+
function analyze (bundle, format) {
30+
let deps = {};
31+
return new Promise((resolve, reject) => {
32+
let modules = bundle.modules.map((m, i) => {
33+
let id = m.id.replace(__dirname, '');
34+
m.dependencies.forEach((d) => {
35+
d = d.replace(__dirname, '');
36+
deps[d] = deps[d] || [];
37+
deps[d].push(id);
38+
});
39+
return {id, size: Buffer.byteLength(m.code, 'utf8') || 0}
40+
});
41+
modules.sort((a, b) => b.size - a.size);
42+
if (limit) modules = modules.slice(0, limit);
43+
modules.forEach((m) => { m.dependents = deps[m.id] || []; });
44+
if (!format) return resolve(modules)
45+
let heading = `Rollup file analysis\n`;
46+
let formatted = `${borderX}${heading}${borderX}`;
47+
modules.forEach((m) => {
48+
formatted += `file:${buf}${m.id}\n`;
49+
formatted += `size:${buf}${formatBytes(m.size)}\n`;
50+
formatted += `dependents:${buf}${m.dependents.length}\n`;
51+
m.dependents.forEach((d) => {
52+
formatted += `${tab}-${buf}${d.replace(__dirname, '')}\n`;
53+
});
54+
formatted += `${borderX}`;
55+
});
56+
return resolve(formatted)
57+
})
58+
}
59+
60+
exports.init = init;
61+
exports.analyze = analyze;
62+
exports.formatted = formatted;

package.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "rollup-analyzer",
3+
"version": "1.0.0",
4+
"description": "Analyze file sizes of rollup bundled imports",
5+
"main": "index.js",
6+
"jsnext:main": "index-es6.js",
7+
"files": [
8+
"index.js",
9+
"index-es6.js"
10+
],
11+
"scripts": {
12+
"test": "echo \"Error: no test specified\" && exit 1"
13+
},
14+
"repository": {
15+
"type": "git",
16+
"url": "git+https://github.com/doesdev/rollup-analyzer.git"
17+
},
18+
"keywords": [
19+
"rollup",
20+
"import",
21+
"bundle",
22+
"file",
23+
"size",
24+
"analysis"
25+
],
26+
"author": "Andrew Carpenter (https://github.com/doesdev)",
27+
"license": "MIT",
28+
"bugs": {
29+
"url": "https://github.com/doesdev/rollup-analyzer/issues"
30+
},
31+
"homepage": "https://github.com/doesdev/rollup-analyzer#readme",
32+
"devDependencies": {
33+
"ghooks": "^1.3.2",
34+
"rollup": "^0.36.4"
35+
},
36+
"config": {
37+
"ghooks": {
38+
"pre-commit": "node build"
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)