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'));
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 () {

View File

@ -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) {