improve size reporting when building and add gzipped size

This commit is contained in:
Vladimir Agafonkin 2013-11-28 14:56:32 +02:00
parent daf7345194
commit 941b206714
2 changed files with 43 additions and 15 deletions

View File

@ -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')); task('lintspec', {async: true}, hint('Checking for specs JS errors...', 'spec/spec.hintrc.js spec/suites'));
desc('Combine and compress Leaflet source files'); desc('Combine and compress Leaflet source files');
task('build', build.build); task('build', {async: true}, function () {
build.build(complete);
});
desc('Run PhantomJS tests'); desc('Run PhantomJS tests');
task('test', ['lint', 'lintspec'], {async: true}, function () { task('test', ['lint', 'lintspec'], {async: true}, function () {

View File

@ -1,6 +1,7 @@
var fs = require('fs'), var fs = require('fs'),
jshint = require('jshint'), jshint = require('jshint'),
UglifyJS = require('uglify-js'), UglifyJS = require('uglify-js'),
zlib = require('zlib'),
deps = require('./deps.js').deps; deps = require('./deps.js').deps;
@ -53,7 +54,7 @@ function getSizeDelta(newContent, oldContent) {
oldLen = oldContent.replace(/\r\n?/g, '\n').length, oldLen = oldContent.replace(/\r\n?/g, '\n').length,
delta = newLen - oldLen; delta = newLen - oldLen;
return (delta >= 0 ? '+' : '') + delta; return delta === 0 ? '' : '(' + (delta > 0 ? '+' : '') + delta + ' bytes)';
} }
function loadSilently(path) { function loadSilently(path) {
@ -72,11 +73,15 @@ function combineFiles(files) {
return content; 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); 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'), var copy = fs.readFileSync('src/copyright.js', 'utf8'),
intro = '(function (window, document, undefined) {', intro = '(function (window, document, undefined) {',
@ -89,17 +94,15 @@ exports.build = function (compsBase32, buildName) {
oldSrc = loadSilently(srcPath), oldSrc = loadSilently(srcPath),
srcDelta = getSizeDelta(newSrc, oldSrc); srcDelta = getSizeDelta(newSrc, oldSrc);
console.log('\tUncompressed size: ' + newSrc.length + ' bytes (' + srcDelta + ')'); console.log('\tUncompressed: ' + bytesToKB(newSrc.length) + srcDelta);
if (newSrc === oldSrc) { if (newSrc === oldSrc) {
console.log('\tNo changes\n'); // console.log('\tNo changes');
} else { } else {
fs.writeFileSync(srcPath, newSrc); fs.writeFileSync(srcPath, newSrc);
console.log('\tSaved to ' + srcPath + '\n'); console.log('\tSaved to ' + srcPath);
} }
console.log('Compressing...');
var path = pathPart + '.js', var path = pathPart + '.js',
oldCompressed = loadSilently(path), oldCompressed = loadSilently(path),
newCompressed = copy + UglifyJS.minify(newSrc, { newCompressed = copy + UglifyJS.minify(newSrc, {
@ -108,14 +111,37 @@ exports.build = function (compsBase32, buildName) {
}).code, }).code,
delta = getSizeDelta(newCompressed, oldCompressed); delta = getSizeDelta(newCompressed, oldCompressed);
console.log('\tCompressed size: ' + newCompressed.length + ' bytes (' + delta + ')'); console.log('\tCompressed: ' + bytesToKB(newCompressed.length) + delta);
if (newCompressed === oldCompressed) { var newGzipped,
console.log('\tNo changes\n'); gzippedDelta = '';
} else {
fs.writeFileSync(path, newCompressed); function done() {
console.log('\tSaved to ' + path + '\n'); 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) { exports.test = function(callback) {