Skip to content

Commit cce6e9b

Browse files
committed
Merge branch 'v2'
# Conflicts: # README.md
2 parents 5e503c2 + b397d3a commit cce6e9b

11 files changed

+675
-103
lines changed

README.md

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,51 @@
11
# react-native-bundle-visualizer
22

3-
**Version 2 is in the works which uses output from the Metro bundler and has a new User Interface. [Check it out here](https://github.com/IjzerenHein/react-native-bundle-visualizer/tree/v2)**
4-
53
See what's inside of your react-native bundle 📦
64

7-
![bundle-visualizer-animation](./react-native-bundle-visualizer.gif)
5+
![bundle-visualizer-animation](./react-native-bundle-visualizer2.gif)
86

9-
Uses the awesome [webpack-visualizer](https://github.com/chrisbateman/webpack-visualizer) and [haul](https://github.com/callstack-io/haul) bundler.
7+
Uses the awesome [source-map-explorer](https://github.com/danvk/source-map-explorer) to visualize the output of the [Metro bundler](https://github.com/facebook/metro).
108

119
## Purpose
1210

1311
Sometimes, importing a single javascript library can drastically increase your bundle size. This package helps you to identify such a library, so you can keep the bundle size low and loading times fast.
1412

1513
## Usage
1614

17-
*Make sure you have node 8 or higher installed.*
15+
`npx react-native-bundle-visualizer`
16+
17+
### Or install as a dev-dependency
1818

19-
Depending on your react-native version, install the appropriate package as a dev dependency:
19+
`yarn add --dev react-native-bundle-visualizer`
2020

21-
| RN version | Install react-native-bundle-visualizer |
22-
| ----------- | ----------------------------------------------------- |
23-
| >= 0.57 | `yarn add --dev react-native-bundle-visualizer` |
24-
| 0.49 - 0.56 | `yarn add --dev react-native-bundle-visualizer@1.3.5` |
25-
| <= 0.48 | `yarn add --dev react-native-bundle-visualizer@1.1.0` |
26-
2721
And run it:
2822

29-
yarn run react-native-bundle-visualizer
23+
yarn run react-native-bundle-visualizer
3024

31-
*or when using npm:*
25+
_or when using npm:_
3226

33-
npm install --save-dev react-native-bundle-visualizer
34-
./node_modules/.bin/react-native-bundle-visualizer
27+
npm install --save-dev react-native-bundle-visualizer
28+
./node_modules/.bin/react-native-bundle-visualizer
3529

36-
### Gitignore output files
30+
## Command line arguments
3731

38-
Additionally, add the haul generated files & folders to your `.gitignore` file:
32+
All command-line arguments are optional. By default a production build will be created for the `ios` platform.
3933

40-
```
41-
# Haul bundler
42-
/assets
43-
stats.html
44-
index.ios.bundle
45-
index.ios.bundle.map
46-
haul-debug.log
47-
```
34+
| Option | Description | Example |
35+
| --------------- | ------------------------------------------------------------- | --------------------------------- |
36+
| `platform` | Platform to build (default is **ios**) | `--platform android` |
37+
| `dev` | Dev or production build (default is **false**) | `--dev false` |
38+
| `entry-file` | Entry-file (when omitted tries to auto-resolve it) | `--entry-file ./index.android.js` |
39+
| `bundle-output` | Output bundle-file (default is **tmp**) | `--bundle-output ./myapp.bundle` |
40+
| `verbose` | Dumps additional output to the console (default is **false**) | `--verbose` |
41+
| `reset-cache` | Removes cached react-native files (default is **false**) | `--reset-cache` |
4842

49-
## Disclaimer
43+
## Usage with older react-native versions and the Haul bundler
5044

51-
The sizes reported are an indication rather than the exact byte size in your bundle. This is because the Haul packager returns different bundles compared to the react-native Metro bundler. Also, due to limitations in webpack's stats, the "actual" (minified) numbers reported here are approximate, but they should be pretty close.
45+
As of `react-native-bundle-visualizer` version 2.x, the direct output of the [Metro bundler](https://github.com/facebook/metro) is visualized using the [source-map-explorer](https://github.com/danvk/source-map-explorer).
46+
47+
Prior to version 2, the Haul bundler was used which used Webpack. If you are having problems visualizing the output for older react-native versions, or you want to explicitly use the Haul bundler, [please check out the V1 documentation](https://github.com/IjzerenHein/react-native-bundle-visualizer/tree/v1).
5248

5349
## License
5450

5551
[MIT](./LICENSE.txt)
56-

package.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
{
22
"name": "react-native-bundle-visualizer",
3-
"version": "1.4.2",
3+
"version": "2.0.3",
44
"description": "See what's inside your react-native bundle",
55
"author": "IjzerenHein <hrutjes@gmail.com>",
66
"keywords": [
77
"react-native-bundle-visualizer",
8+
"react native bundle size",
89
"react-native",
910
"react native",
1011
"bundle",
1112
"visualizer",
1213
"size",
1314
"bundle-size"
1415
],
15-
"bin": "./src/react-native-bundle-visualizer.sh",
16+
"bin": "./src/react-native-bundle-visualizer.js",
1617
"repository": "https://github.com/IjzerenHein/react-native-bundle-visualizer",
1718
"bugs": "https://github.com/IjzerenHein/react-native-bundle-visualizer/issues",
1819
"license": "MIT",
1920
"dependencies": {
20-
"haul": "^1.0.0-rc.15",
21-
"react-dom": "^16.8.4",
22-
"react-hot-loader": "^4.8.0",
23-
"webpack-visualizer-plugin": "^0.1.11"
21+
"chalk": "^2.4.2",
22+
"execa": "^2.0.3",
23+
"minimist": "^1.2.0",
24+
"open": "^6.4.0",
25+
"rimraf": "^2.6.3",
26+
"source-map-explorer": "^2.0.1"
2427
}
2528
}

react-native-bundle-visualizer.gif

-1.05 MB
Binary file not shown.
3.15 MB
Loading

src/get-entry-point.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/haul.config.extend.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/haul.config.js

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#!/usr/bin/env node
2+
const chalk = require('chalk');
3+
const fs = require('fs');
4+
const os = require('os');
5+
const argv = require('minimist')(process.argv.slice(2));
6+
const execa = require('execa');
7+
const rimraf = require('rimraf');
8+
const open = require('open');
9+
const { explore } = require('source-map-explorer');
10+
const pkgJSON = JSON.parse(fs.readFileSync('./package.json'));
11+
12+
function getAppName() {
13+
if (pkgJSON.name) return pkgJSON.name;
14+
try {
15+
const appJSON = JSON.parse(fs.readFileSync('./app.json'));
16+
return appJSON.name || appJSON.expo.name || 'UnknownApp';
17+
} catch (err) {
18+
return 'UnknownApp';
19+
}
20+
}
21+
22+
function getEntryPoint() {
23+
let entry = pkgJSON.main || 'index.js';
24+
if (entry[0] !== '.' && entry[0] !== '/' && entry[0] !== '\\') {
25+
entry = './' + entry;
26+
}
27+
return entry;
28+
}
29+
30+
// Get (default) arguments
31+
const baseDir = os.tmpdir() + '/react-native-bundle-visualizer';
32+
const tmpDir = baseDir + '/' + getAppName();
33+
const entryFile = argv['entry-file'] || getEntryPoint();
34+
const platform = argv.platform || 'ios';
35+
const dev = argv.dev || false;
36+
const verbose = argv.verbose || false;
37+
const resetCache = argv['reset-cache'] || false;
38+
const bundleOutput =
39+
argv['bundle-output'] || tmpDir + '/' + platform + '.bundle';
40+
const bundleOutputSourceMap = bundleOutput + '.map';
41+
const bundleOutputExplorerHTML = tmpDir + '/output/explorer.html';
42+
43+
// Make sure the temp dir exists
44+
if (!fs.existsSync(baseDir)) fs.mkdirSync(baseDir);
45+
if (!fs.existsSync(tmpDir)) fs.mkdirSync(tmpDir);
46+
47+
// Try to obtain the previous file size
48+
let prevBundleSize;
49+
if (fs.existsSync(bundleOutput)) {
50+
const stats = fs.statSync(bundleOutput);
51+
prevBundleSize = stats.size;
52+
}
53+
54+
// Bundle
55+
console.log(chalk.green.bold('Generating bundle...'));
56+
const commands = [
57+
'bundle',
58+
'--platform',
59+
platform,
60+
'--dev',
61+
dev,
62+
'--entry-file',
63+
entryFile,
64+
'--bundle-output',
65+
bundleOutput,
66+
'--sourcemap-output',
67+
bundleOutputSourceMap
68+
];
69+
if (resetCache) {
70+
commands.push('--reset-cache');
71+
commands.push(resetCache);
72+
}
73+
const bundlePromise = execa('./node_modules/.bin/react-native', commands);
74+
bundlePromise.stdout.pipe(process.stdout);
75+
76+
// Upon bundle completion, run `source-map-explorer`
77+
bundlePromise
78+
.then(
79+
() => {
80+
// Log bundle-size
81+
const stats = fs.statSync(bundleOutput);
82+
83+
// Log increase or decrease since last run
84+
let deltaSuffix = '';
85+
if (prevBundleSize) {
86+
const delta = stats.size - prevBundleSize;
87+
if (delta > 0) {
88+
deltaSuffix = chalk.yellow(
89+
' (+++ has increased with ' + delta + ' bytes since last run)'
90+
);
91+
} else if (delta < 0) {
92+
deltaSuffix = chalk.green.bold(
93+
' (--- has decreased with ' + (0 - delta) + ' bytes since last run)'
94+
);
95+
} else {
96+
deltaSuffix = chalk.green(' (unchanged since last run)');
97+
}
98+
}
99+
console.log(
100+
chalk.green.bold(
101+
'Bundle is ' +
102+
Math.round((stats.size / (1024 * 1024)) * 100) / 100 +
103+
' MB in size'
104+
) + deltaSuffix
105+
);
106+
107+
// Make sure the explorer output dir is removed
108+
if (fs.existsSync(tmpDir + '/output')) rimraf.sync(tmpDir + '/output');
109+
return explore(
110+
{
111+
code: bundleOutput,
112+
map: bundleOutputSourceMap
113+
},
114+
{
115+
output: {
116+
format: 'html',
117+
filename: bundleOutputExplorerHTML
118+
}
119+
}
120+
);
121+
}
122+
123+
// Log info and open html file
124+
)
125+
.then(result => {
126+
if (verbose) {
127+
result.bundles.forEach(bundle => {
128+
Object.keys(bundle.files).forEach(file => {
129+
console.log(
130+
chalk.green(file + ', size: ' + bundle.files[file] + ' bytes')
131+
);
132+
});
133+
});
134+
}
135+
136+
// Log any errors
137+
if (result.errors) {
138+
result.errors.forEach(error => {
139+
if (error.isWarning) {
140+
console.log(chalk.yellow.bold(error.message));
141+
} else {
142+
console.log(chalk.red.bold(error.message));
143+
}
144+
});
145+
}
146+
147+
// Open html file
148+
return open(bundleOutputExplorerHTML);
149+
});

src/react-native-bundle-visualizer.sh

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/webpack.haul.extend.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)