Leaflet/build/build.js

238 lines
5.5 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'),
SourceNode = require( 'source-map' ).SourceNode;
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
2016-04-18 16:10:27 +08:00
// Concatenate the files while building up a sourcemap for the concatenation,
// and replace the line defining L.version with the string prepared in the jakefile
function bundleFiles(files, copy, version) {
var node = new SourceNode(null, null, null, '');
node.add(new SourceNode(null, null, null, copy + '(function (window, document, undefined) {'));
2015-08-10 21:04:21 +08:00
2012-12-18 01:15:19 +08:00
for (var i = 0, len = files.length; i < len; i++) {
var contents = fs.readFileSync(files[i], 'utf8');
2016-04-18 16:10:27 +08:00
if (files[i] === 'src/Leaflet.js') {
contents = contents.replace(
new RegExp('version: \'.*\''),
'version: ' + JSON.stringify(version)
);
}
var lines = contents.split('\n');
var lineCount = lines.length;
var fileNode = new SourceNode(null, null, null, '');
fileNode.setSourceContent(files[i], contents);
for (var j=0; j<lineCount; j++) {
fileNode.add(new SourceNode(j+1, 0, files[i], lines[j] + '\n'));
}
node.add(fileNode);
node.add(new SourceNode(null, null, null, '\n\n'));
2012-12-18 01:15:19 +08:00
}
2015-08-10 21:04:21 +08:00
node.add(new SourceNode(null, null, null, '}(window, document));'));
2015-08-10 21:04:21 +08:00
var bundle = node.toStringWithSourceMap();
return {
src: bundle.code,
srcmap: bundle.map.toString()
};
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',
2016-04-18 16:10:27 +08:00
bundle = bundleFiles(files, copy, version),
newSrc = bundle.src + '\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);
fs.writeFileSync(mapPath, bundle.srcmap);
console.log('\tSaved to ' + srcPath);
2012-12-18 01:15:19 +08:00
}
var path = pathPart + '.js',
oldCompressed = loadSilently(path),
Trying out Leafdoc comments in L.Marker Added Leafdoc comments to Layer.js Leafdoc comments for Popup, Layer Leafdoc comments: L.Evented, inheritances, minor tilelayer Leafdoc comments: gridlayer & tilelayer options Leafdoc comments: tilelayer, marker drag Typos Leafdoc: switch to shorthand method params Leafdoc: Switch to shorthands in marker drag, WMS. Leafdoc: Vector layers Leafdoc: Layer group, feature group, geojson Leafdoc: LatLng, Point, Bounds, Icons. Leafdoc: Controls. Leafdoc: DOM & utils. Leafdoc: "jake docs" now builds the documentation Leafdoc: Commit actual templates instead of symlinks Leafdoc: Fix broken build, have jake print out uglifyjs errors Leafdoc: Several L.Map bits. Leafdoc: Map handlers Leafdoc: Map events, L.CRS, misc. fixes Leafdoc: Fixed ordering of classes by using new leafdoc features Leafdoc: Misc bits at the bottom of the docs 🍂doc: Map panes 🍂doc: CRSs, projections and their templates 🍂doc: miniclasses for map methods' options Leafdoc: Cleanup L.Class, mark uninheritable sections, use Leafdoc 0.3.0 🍂doc: miniclasses for event types, bump to Leafdoc 1.0.0 🍂doc: Make linter happy after branch rebase 🍂doc: Tweaked headers for inherited stuff. 🍂doc: Tweaking section headers (white, padding, triangles) Leafdoc: upgrade to 1.2, document SVG&Canvas, and misc bits 🍂doc: minor CSS tweaks, version in filename, typo. Add missing bits - supersedes #4105, #4065, #4031 🍂doc: moved sections around, minor typos & fixes Typo about LocationEvent
2015-08-13 02:51:04 +08:00
newCompressed;
try {
newCompressed = copy + UglifyJS.minify(newSrc, {
warnings: true,
fromString: true
}).code;
} catch(err) {
console.error('UglifyJS failed to minify the files');
console.error(err);
callback(err);
}
var delta = getSizeDelta(newCompressed, oldCompressed);
2012-12-18 01:15:19 +08:00
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
testConfig.browsers = ['PhantomJSCustom'];
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
};