Skip to content

Commit 01646de

Browse files
committed
Added logging of the bundle-size delta compared to the last run
1 parent a5fbf42 commit 01646de

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

src/react-native-bundle-visualizer.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,16 @@ const bundleOutput =
3939
const bundleOutputSourceMap = bundleOutput + '.map';
4040
const bundleOutputExplorerHTML = tmpDir + '/output/explorer.html';
4141

42-
// Make sure output dir exists
42+
// Make sure the temp dir exists
4343
if (!fs.existsSync(baseDir)) fs.mkdirSync(baseDir);
44-
if (fs.existsSync(tmpDir)) rimraf.sync(tmpDir);
45-
fs.mkdirSync(tmpDir);
44+
if (!fs.existsSync(tmpDir)) fs.mkdirSync(tmpDir);
45+
46+
// Try to obtain the previous file size
47+
let prevBundleSize;
48+
if (fs.existsSync(bundleOutput)) {
49+
const stats = fs.statSync(bundleOutput);
50+
prevBundleSize = stats.size;
51+
}
4652

4753
// Bundle
4854
console.log(chalk.green.bold('Generating bundle...'));
@@ -65,14 +71,35 @@ bundlePromise.stdout.pipe(process.stdout);
6571
bundlePromise
6672
.then(
6773
() => {
74+
// Log bundle-size
6875
const stats = fs.statSync(bundleOutput);
76+
77+
// Log increase or decrease since last run
78+
let deltaSuffix = '';
79+
if (prevBundleSize) {
80+
const delta = stats.size - prevBundleSize;
81+
if (delta > 0) {
82+
deltaSuffix = chalk.yellow(
83+
' (+++ has increased with ' + delta + ' bytes since last run)'
84+
);
85+
} else if (delta < 0) {
86+
deltaSuffix = chalk.green.bold(
87+
' (--- has decreased with ' + (0 - delta) + ' bytes since last run)'
88+
);
89+
} else {
90+
deltaSuffix = chalk.green(' (unchanged since last run)');
91+
}
92+
}
6993
console.log(
7094
chalk.green.bold(
7195
'Bundle is ' +
72-
Math.round((stats.size / (1024 * 1024)) * 10) / 10 +
96+
Math.round((stats.size / (1024 * 1024)) * 100) / 100 +
7397
' MB in size'
74-
)
98+
) + deltaSuffix
7599
);
100+
101+
// Make sure the explorer output dir is removed
102+
if (fs.existsSync(tmpDir + '/output')) rimraf.sync(tmpDir + '/output');
76103
return explore(
77104
{
78105
code: bundleOutput,

0 commit comments

Comments
 (0)