From 941b20671456c6db98414519fb30baee81ea4bfa Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Thu, 28 Nov 2013 14:56:32 +0200 Subject: [PATCH] improve size reporting when building and add gzipped size --- Jakefile.js | 4 +++- build/build.js | 54 +++++++++++++++++++++++++++++++++++++------------- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/Jakefile.js b/Jakefile.js index f7ed1453..77be075b 100644 --- a/Jakefile.js +++ b/Jakefile.js @@ -32,7 +32,9 @@ desc('Check Leaflet specs source for errors with JSHint'); task('lintspec', {async: true}, hint('Checking for specs JS errors...', 'spec/spec.hintrc.js spec/suites')); desc('Combine and compress Leaflet source files'); -task('build', build.build); +task('build', {async: true}, function () { + build.build(complete); +}); desc('Run PhantomJS tests'); task('test', ['lint', 'lintspec'], {async: true}, function () { diff --git a/build/build.js b/build/build.js index cbb714de..518c93fe 100644 --- a/build/build.js +++ b/build/build.js @@ -1,6 +1,7 @@ var fs = require('fs'), jshint = require('jshint'), UglifyJS = require('uglify-js'), + zlib = require('zlib'), deps = require('./deps.js').deps; @@ -53,7 +54,7 @@ function getSizeDelta(newContent, oldContent) { oldLen = oldContent.replace(/\r\n?/g, '\n').length, delta = newLen - oldLen; - return (delta >= 0 ? '+' : '') + delta; + return delta === 0 ? '' : '(' + (delta > 0 ? '+' : '') + delta + ' bytes)'; } function loadSilently(path) { @@ -72,11 +73,15 @@ function combineFiles(files) { return content; } -exports.build = function (compsBase32, buildName) { +function bytesToKB(bytes) { + return (bytes / 1024).toFixed(2) + ' KB'; +}; + +exports.build = function (callback, compsBase32, buildName) { var files = getFiles(compsBase32); - console.log('Concatenating ' + files.length + ' files...'); + console.log('Concatenating and compressing ' + files.length + ' files...'); var copy = fs.readFileSync('src/copyright.js', 'utf8'), intro = '(function (window, document, undefined) {', @@ -89,17 +94,15 @@ exports.build = function (compsBase32, buildName) { oldSrc = loadSilently(srcPath), srcDelta = getSizeDelta(newSrc, oldSrc); - console.log('\tUncompressed size: ' + newSrc.length + ' bytes (' + srcDelta + ')'); + console.log('\tUncompressed: ' + bytesToKB(newSrc.length) + srcDelta); if (newSrc === oldSrc) { - console.log('\tNo changes\n'); + // console.log('\tNo changes'); } else { fs.writeFileSync(srcPath, newSrc); - console.log('\tSaved to ' + srcPath + '\n'); + console.log('\tSaved to ' + srcPath); } - console.log('Compressing...'); - var path = pathPart + '.js', oldCompressed = loadSilently(path), newCompressed = copy + UglifyJS.minify(newSrc, { @@ -108,14 +111,37 @@ exports.build = function (compsBase32, buildName) { }).code, delta = getSizeDelta(newCompressed, oldCompressed); - console.log('\tCompressed size: ' + newCompressed.length + ' bytes (' + delta + ')'); + console.log('\tCompressed: ' + bytesToKB(newCompressed.length) + delta); - if (newCompressed === oldCompressed) { - console.log('\tNo changes\n'); - } else { - fs.writeFileSync(path, newCompressed); - console.log('\tSaved to ' + path + '\n'); + var newGzipped, + gzippedDelta = ''; + + function done() { + var noChanges = newCompressed === oldCompressed; + if (!noChanges) { + fs.writeFileSync(path, newCompressed); + console.log('\tSaved to ' + path); + } + console.log('\tGzipped: ' + bytesToKB(newGzipped.length) + gzippedDelta); + if (noChanges) { + console.log('\tNo changes\n'); + } + callback(); } + + zlib.gzip(newCompressed, function (err, gzipped) { + if (err) { return; } + newGzipped = gzipped; + if (oldCompressed !== newCompressed) { + zlib.gzip(oldCompressed, function (err, oldGzipped) { + if (err) { return; } + gzippedDelta = getSizeDelta(gzipped.toString(), oldGzipped.toString()); + done(); + }); + } else { + done(); + } + }); }; exports.test = function(callback) {