Skip to content

Commit 41dfaf3

Browse files
committed
build(analytics): track bundle size
This will send bundle sizes (before and after gzip) to Google Analytics so that we can track bundle size over time for every bundle we produce. Closes angular#5294
1 parent 9607686 commit 41dfaf3

5 files changed

Lines changed: 1065 additions & 9 deletions

File tree

gulpfile.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,37 @@ gulp.task('!bundle.copy', function() {
10451045
gulp.src('dist/js/bundle/**').pipe(gulp.dest('dist/js/dev/es5/bundle')));
10461046
});
10471047

1048+
gulp.task('!bundles.js.checksize', function() {
1049+
var gzip = require('gulp-gzip');
1050+
var path = require('path');
1051+
1052+
return merge2(gulp.src('dist/js/bundle/**').on('data', checkFileSizeFactory('uncompressed')),
1053+
gulp.src('dist/js/bundle/**')
1054+
.pipe(gzip({gzipOptions: {level: 1}})) // code.angular.js
1055+
.on('data', checkFileSizeFactory('gzip level=1')),
1056+
gulp.src('dist/js/bundle/**')
1057+
.pipe(gzip({gzipOptions: {level: 2}})) // github pages, most common
1058+
.on('data', checkFileSizeFactory('gzip level=2', true)),
1059+
gulp.src('dist/js/bundle/**')
1060+
.pipe(gzip({gzipOptions: {level: 6}})) // default gzip level
1061+
.on('data', checkFileSizeFactory('gzip level=6')),
1062+
gulp.src('dist/js/bundle/**')
1063+
.pipe(gzip({gzipOptions: {level: 9}})) // max gzip level
1064+
.on('data', checkFileSizeFactory('gzip level=9')));
1065+
1066+
function checkFileSizeFactory(compressionLevel, printToConsole) {
1067+
return function checkFileSize(file) {
1068+
if (file.isNull()) return;
1069+
var filePath =
1070+
path.relative(path.join('dist', 'js', 'bundle'), file.path).replace('\.gz', '');
1071+
analytics.bundleSize(filePath, file.contents.length, compressionLevel);
1072+
if (printToConsole) {
1073+
console.log(` ${filePath} => ${file.contents.length} bytes (${compressionLevel})`)
1074+
}
1075+
}
1076+
}
1077+
});
1078+
10481079
gulp.task('bundles.js',
10491080
[
10501081
'!bundle.js.prod.deps',
@@ -1054,7 +1085,7 @@ gulp.task('bundles.js',
10541085
'!bundle.js.sfx.dev.deps',
10551086
'!bundle.testing'
10561087
],
1057-
function(done) { runSequence('!bundle.copy', done); });
1088+
function(done) { runSequence('!bundle.copy', '!bundles.js.checksize', done); });
10581089

10591090
gulp.task('build.js',
10601091
['build.js.dev', 'build.js.prod', 'build.js.cjs', 'bundles.js', 'benchpress.bundle']);

0 commit comments

Comments
 (0)