Skip to content

Commit 2ba5ac7

Browse files
committed
init
0 parents  commit 2ba5ac7

File tree

10 files changed

+242
-0
lines changed

10 files changed

+242
-0
lines changed

.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[*]
2+
indent_size = 2

.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Logs
2+
logs
3+
*.log
4+
5+
# Runtime data
6+
pids
7+
*.pid
8+
*.seed
9+
10+
# Directory for instrumented libs generated by jscoverage/JSCover
11+
lib-cov
12+
13+
# Coverage directory used by tools like istanbul
14+
coverage
15+
16+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17+
.grunt
18+
19+
# node-waf configuration
20+
.lock-wscript
21+
22+
# Compiled binary addons (http://nodejs.org/api/addons.html)
23+
build/Release
24+
25+
# Dependency directory
26+
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
27+
node_modules
28+
bundle.js
29+
stats.json

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- "stable"
4+
- "8"
5+
- "6"

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# browserify-webpack-stats change log
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
This project adheres to [Semantic Versioning](http://semver.org/).

LICENSE.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# [Apache License 2.0](https://spdx.org/licenses/Apache-2.0)
2+
3+
Copyright 2018 Renée Kooi <[email protected]>
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
> http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# browserify-webpack-stats
2+
3+
output a webpack-style stats.json with browserify, for use with webpack based bundle analyzer tools
4+
5+
> WIP!
6+
7+
[![npm][npm-image]][npm-url]
8+
[![travis][travis-image]][travis-url]
9+
[![standard][standard-image]][standard-url]
10+
11+
[npm-image]: https://img.shields.io/npm/v/browserify-webpack-stats.svg?style=flat-square
12+
[npm-url]: https://www.npmjs.com/package/browserify-webpack-stats
13+
[travis-image]: https://img.shields.io/travis/goto-bus-stop/browserify-webpack-stats.svg?style=flat-square
14+
[travis-url]: https://travis-ci.org/goto-bus-stop/browserify-webpack-stats
15+
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
16+
[standard-url]: http://npm.im/standard
17+
18+
## Install
19+
20+
```
21+
npm install browserify-webpack-stats
22+
```
23+
24+
## Usage
25+
26+
```bash
27+
browserify app.js -p browserify-webpack-stats -o bundle.js
28+
webpack-bundle-analyzer stats.json .
29+
```
30+
31+
## License
32+
33+
[Apache-2.0](LICENSE.md)

index.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
var path = require('path')
2+
var crypto = require('crypto')
3+
var through = require('through2')
4+
5+
module.exports = function webpackStats (b) {
6+
var stats = null
7+
var startTime
8+
var chunkId
9+
function init () {
10+
stats = {
11+
errors: [],
12+
warnings: [],
13+
version: '1.0.0',
14+
publicPath: '',
15+
assets: [],
16+
entrypoints: {},
17+
chunks: [],
18+
modules: []
19+
}
20+
startTime = Date.now()
21+
chunkId = 0
22+
}
23+
24+
b.on('reset', addHooks)
25+
b.on('split.pipeline', addChunk)
26+
b.on('factor.pipeline', addChunk)
27+
addHooks()
28+
29+
function addHooks () {
30+
init()
31+
32+
addChunk('bundle.js', b.pipeline)
33+
b.pipeline.get('wrap').on('end', function () {
34+
require('fs').writeFile('stats.json', JSON.stringify(stats, null, 2), function () {})
35+
})
36+
}
37+
function addChunk (name, pipeline) {
38+
var totalSize = 0
39+
var chunk = {
40+
id: chunkId++,
41+
rendered: true,
42+
initial: true,
43+
entry: true,
44+
extraAsync: false,
45+
size: 0,
46+
names: [ name.replace(/\.[a-z]*$/, '') ],
47+
files: [],
48+
hash: '',
49+
modules: [],
50+
}
51+
var hash = crypto.createHash('sha512')
52+
pipeline.get('pack').unshift(through.obj(onmodule, onmodulesend))
53+
pipeline.get('wrap').push(through(ondata, onchunkend))
54+
55+
function onmodule (row, enc, cb) {
56+
var relative = path.relative(b._options.basedir || process.cwd(), row.file)
57+
var match = /^(\.\.\/)+(.*?)\/browserify\/(lib|node_modules)\//.exec(relative)
58+
if (match) {
59+
relative = '(browserify)/' + match[3] + '/' + relative.slice(match[0].length)
60+
}
61+
if (!relative.includes('(browserify') && !relative.includes('node_modules/')) {
62+
chunk.files.push(relative)
63+
}
64+
if (!/^\.\.?\//.test(relative) && !relative.startsWith('(browserify)') && !path.isAbsolute(relative)) {
65+
relative = './' + relative
66+
}
67+
var module = {
68+
id: row.id,
69+
identifier: row.file,
70+
name: relative,
71+
chunks: [chunk.id],
72+
chunkNames: chunk.names,
73+
source: row.source,
74+
size: Math.max(1, row.source.length)
75+
}
76+
chunk.modules.push(module)
77+
stats.modules.push(module)
78+
cb(null, row)
79+
}
80+
81+
function onmodulesend (done) {
82+
chunk.size = chunk.modules.reduce(function (s, r) { return s + r.size }, 0)
83+
done()
84+
}
85+
86+
function ondata (chunk, enc, cb) {
87+
hash.update(chunk)
88+
totalSize += chunk.length
89+
cb(null, chunk)
90+
}
91+
function onchunkend (done) {
92+
chunk.hash = hash.digest('hex')
93+
stats.chunks.push(chunk)
94+
stats.assets.push({
95+
name: name,
96+
size: totalSize,
97+
chunks: [chunk.id],
98+
chunkNames: chunk.names,
99+
emitted: true
100+
})
101+
done()
102+
}
103+
}
104+
}

package.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "browserify-webpack-stats",
3+
"description": "output a webpack-style stats.json with browserify",
4+
"version": "0.0.0",
5+
"author": "Renée Kooi <[email protected]>",
6+
"bugs": {
7+
"url": "https://github.com/goto-bus-stop/browserify-webpack-stats/issues"
8+
},
9+
"devDependencies": {
10+
"standard": "^11.0.0",
11+
"webpack-bundle-analyzer": "^2.11.1"
12+
},
13+
"homepage": "https://github.com/goto-bus-stop/browserify-webpack-stats",
14+
"keywords": [
15+
"analyzer",
16+
"asset",
17+
"assets",
18+
"browserify",
19+
"bundle",
20+
"bundles",
21+
"chunk",
22+
"chunks",
23+
"modules",
24+
"pipeline",
25+
"profile",
26+
"stats",
27+
"webpack"
28+
],
29+
"license": "Apache-2.0",
30+
"main": "index.js",
31+
"repository": {
32+
"type": "git",
33+
"url": "https://github.com/goto-bus-stop/browserify-webpack-stats.git"
34+
},
35+
"scripts": {
36+
"test": "standard && npm run analyze",
37+
"analyze": "browserify index.js -p [ ./index.js stats.json ] -o bundle.js && webpack-bundle-analyzer stats.json"
38+
},
39+
"dependencies": {
40+
"through2": "^2.0.3"
41+
}
42+
}

test/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var test = require('tape')
2+
3+
test('Example Test', function (t) {
4+
t.plan(1)
5+
t.error('No tests defined.')
6+
})

0 commit comments

Comments
 (0)