Leaflet/build/build.js

191 lines
4.2 KiB
JavaScript
Raw Normal View History

2011-12-12 18:44:16 +08:00
var fs = require('fs'),
2012-12-18 03:32:00 +08:00
jshint = require('jshint'),
2012-12-18 01:15:19 +08:00
UglifyJS = require('uglify-js'),
zlib = require('zlib'),
2012-12-18 01:15:19 +08:00
deps = require('./deps.js').deps;
2012-12-18 01:15:19 +08:00
function getFiles(compsBase32) {
2011-12-12 18:44:16 +08:00
var memo = {},
2012-12-18 03:32:00 +08:00
comps;
2011-12-12 18:44:16 +08:00
if (compsBase32) {
comps = parseInt(compsBase32, 32).toString(2).split('');
2013-03-02 05:49:20 +08:00
console.log('Managing dependencies...');
2011-12-12 18:44:16 +08:00
}
function addFiles(srcs) {
for (var j = 0, len = srcs.length; j < len; j++) {
memo[srcs[j]] = true;
}
}
for (var i in deps) {
if (comps) {
if (parseInt(comps.pop(), 2) === 1) {
console.log(' * ' + i);
2011-12-12 18:44:16 +08:00
addFiles(deps[i].src);
} else {
console.log(' ' + i);
2011-12-12 18:44:16 +08:00
}
} else {
addFiles(deps[i].src);
}
}
console.log('');
2011-12-12 18:44:16 +08:00
var files = [];
for (var src in memo) {
files.push('src/' + src);
}
return files;
2012-12-18 01:15:19 +08:00
}
2011-12-12 18:44:16 +08:00
2013-01-31 02:48:22 +08:00
exports.getFiles = getFiles;
2013-11-28 21:20:25 +08:00
function getSizeDelta(newContent, oldContent, fixCRLF) {
2012-12-18 01:15:19 +08:00
if (!oldContent) {
2013-11-28 21:20:25 +08:00
return ' (new)';
2012-12-18 01:15:19 +08:00
}
2013-11-28 21:20:25 +08:00
if (newContent === oldContent) {
return ' (unchanged)';
}
if (fixCRLF) {
newContent = newContent.replace(/\r\n?/g, '\n');
oldContent = oldContent.replace(/\r\n?/g, '\n');
}
var delta = newContent.length - oldContent.length;
2012-12-18 01:15:19 +08:00
2013-11-28 21:20:25 +08:00
return delta === 0 ? '' : ' (' + (delta > 0 ? '+' : '') + delta + ' bytes)';
2012-12-18 01:15:19 +08:00
}
function loadSilently(path) {
2011-12-13 06:41:56 +08:00
try {
2012-12-18 01:15:19 +08:00
return fs.readFileSync(path, 'utf8');
2011-12-13 06:41:56 +08:00
} catch (e) {
return null;
}
2012-12-18 01:15:19 +08:00
}
2011-12-13 06:41:56 +08:00
2012-12-18 01:15:19 +08:00
function combineFiles(files) {
var content = '';
for (var i = 0, len = files.length; i < len; i++) {
2012-12-18 03:32:00 +08:00
content += fs.readFileSync(files[i], 'utf8') + '\n\n';
2012-12-18 01:15:19 +08:00
}
return content;
}
function bytesToKB(bytes) {
return (bytes / 1024).toFixed(2) + ' KB';
};
exports.build = function (callback, version, compsBase32, buildName) {
2012-12-18 01:15:19 +08:00
var files = getFiles(compsBase32);
console.log('Concatenating and compressing ' + files.length + ' files...');
2012-12-18 01:15:19 +08:00
var copy = fs.readFileSync('src/copyright.js', 'utf8').replace('{VERSION}', version),
2012-12-18 03:32:00 +08:00
intro = '(function (window, document, undefined) {',
outro = '}(window, document));',
2012-12-18 01:15:19 +08:00
newSrc = copy + intro + combineFiles(files) + outro,
pathPart = 'dist/leaflet' + (buildName ? '-' + buildName : ''),
srcPath = pathPart + '-src.js',
oldSrc = loadSilently(srcPath),
2013-11-28 21:20:25 +08:00
srcDelta = getSizeDelta(newSrc, oldSrc, true);
2012-12-18 01:15:19 +08:00
console.log('\tUncompressed: ' + bytesToKB(newSrc.length) + srcDelta);
2012-12-18 01:15:19 +08:00
2013-11-28 21:20:25 +08:00
if (newSrc !== oldSrc) {
2012-12-18 01:15:19 +08:00
fs.writeFileSync(srcPath, newSrc);
console.log('\tSaved to ' + srcPath);
2012-12-18 01:15:19 +08:00
}
var path = pathPart + '.js',
oldCompressed = loadSilently(path),
newCompressed = copy + UglifyJS.minify(newSrc, {
2012-12-18 03:32:00 +08:00
warnings: true,
fromString: true
2012-12-18 01:15:19 +08:00
}).code,
delta = getSizeDelta(newCompressed, oldCompressed);
console.log('\tCompressed: ' + bytesToKB(newCompressed.length) + delta);
2012-12-18 01:15:19 +08:00
var newGzipped,
gzippedDelta = '';
function done() {
2013-11-28 21:20:25 +08:00
if (newCompressed !== oldCompressed) {
fs.writeFileSync(path, newCompressed);
console.log('\tSaved to ' + path);
}
console.log('\tGzipped: ' + bytesToKB(newGzipped.length) + gzippedDelta);
callback();
2011-12-13 06:41:56 +08:00
}
zlib.gzip(newCompressed, function (err, gzipped) {
if (err) { return; }
newGzipped = gzipped;
2013-11-28 21:20:25 +08:00
if (oldCompressed && (oldCompressed !== newCompressed)) {
zlib.gzip(oldCompressed, function (err, oldGzipped) {
if (err) { return; }
2013-11-28 21:20:25 +08:00
gzippedDelta = getSizeDelta(gzipped, oldGzipped);
done();
});
} else {
done();
}
});
};
2013-01-30 22:06:19 +08:00
exports.test = function(complete, fail) {
2013-04-04 05:10:30 +08:00
var karma = require('karma'),
testConfig = {configFile : __dirname + '/../spec/karma.conf.js'};
2013-02-04 19:42:27 +08:00
2013-01-30 22:06:19 +08:00
testConfig.browsers = ['PhantomJS'];
2013-02-02 15:29:20 +08:00
function isArgv(optName) {
return process.argv.indexOf(optName) !== -1;
}
if (isArgv('--chrome')) {
testConfig.browsers.push('Chrome');
}
if (isArgv('--safari')) {
testConfig.browsers.push('Safari');
}
if (isArgv('--ff')) {
testConfig.browsers.push('Firefox');
}
if (isArgv('--ie')) {
testConfig.browsers.push('IE');
}
if (isArgv('--cov')) {
2013-02-04 19:42:27 +08:00
testConfig.preprocessors = {
2013-12-02 22:21:41 +08:00
'src/**/*.js': 'coverage'
2013-02-04 19:42:27 +08:00
};
testConfig.coverageReporter = {
type : 'html',
dir : 'coverage/'
};
testConfig.reporters = ['coverage'];
}
console.log('Running tests...');
2013-01-30 22:06:19 +08:00
karma.server.start(testConfig, function(exitCode) {
if (!exitCode) {
console.log('\tTests ran successfully.\n');
complete();
} else {
process.exit(exitCode);
}
});
2013-03-02 05:49:20 +08:00
};