Leaflet/build/build.js

210 lines
4.7 KiB
JavaScript
Raw Normal View History

2011-12-12 18:44:16 +08:00
var fs = require('fs'),
2012-12-18 01:15:19 +08:00
UglifyJS = require('uglify-js'),
zlib = require('zlib'),
2015-08-10 21:04:21 +08:00
MagicString = require('magic-string'),
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
2015-08-10 21:04:21 +08:00
function bundleFiles(files, copy) {
var bundle = new MagicString.Bundle();
2012-12-18 01:15:19 +08:00
for (var i = 0, len = files.length; i < len; i++) {
2015-08-10 21:04:21 +08:00
bundle.addSource({
filename: files[i],
content: new MagicString( fs.readFileSync(files[i], 'utf8') + '\n\n' )
});
2012-12-18 01:15:19 +08:00
}
2015-08-10 21:04:21 +08:00
bundle.prepend(
copy + '(function (window, document, undefined) {'
).append('}(window, document));');
return bundle;
2012-12-18 01:15:19 +08:00
}
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);
2015-08-10 21:04:21 +08:00
console.log('Bundling 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 01:15:19 +08:00
2015-08-10 21:04:21 +08:00
filenamePart = 'leaflet' + (buildName ? '-' + buildName : ''),
pathPart = 'dist/' + filenamePart,
2012-12-18 01:15:19 +08:00
srcPath = pathPart + '-src.js',
2015-08-10 21:04:21 +08:00
mapPath = pathPart + '-src.map',
srcFilename = filenamePart + '-src.js',
mapFilename = filenamePart + '-src.map',
bundle = bundleFiles(files, copy),
newSrc = bundle.toString() + '\n//# sourceMappingURL=' + mapFilename,
2012-12-18 01:15:19 +08:00
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);
2015-08-10 21:04:21 +08:00
fs.writeFileSync(mapPath, bundle.generateMap({
file: srcFilename,
includeContent: true,
hires: false
}));
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
2015-09-04 23:46:55 +08:00
var server = new karma.Server(testConfig, function(exitCode) {
if (!exitCode) {
console.log('\tTests ran successfully.\n');
complete();
} else {
process.exit(exitCode);
}
});
2015-09-04 23:46:55 +08:00
server.start();
2013-03-02 05:49:20 +08:00
};