2011-12-12 18:44:16 +08:00
|
|
|
var fs = require('fs'),
|
2012-12-18 01:15:19 +08:00
|
|
|
UglifyJS = require('uglify-js'),
|
2013-11-28 20:56:32 +08:00
|
|
|
zlib = require('zlib'),
|
2016-04-19 16:22:52 +08:00
|
|
|
SourceNode = require( 'source-map' ).SourceNode;
|
2012-12-18 01:15:19 +08:00
|
|
|
|
2013-11-08 23:55:58 +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) {
|
2013-11-15 22:35:25 +08:00
|
|
|
console.log(' * ' + i);
|
2011-12-12 18:44:16 +08:00
|
|
|
addFiles(deps[i].src);
|
|
|
|
} else {
|
2013-11-15 22:35:25 +08:00
|
|
|
console.log(' ' + i);
|
2011-12-12 18:44:16 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
addFiles(deps[i].src);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-15 22:35:25 +08:00
|
|
|
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) {
|
2016-04-19 16:22:52 +08:00
|
|
|
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++) {
|
2016-04-19 16:22:52 +08:00
|
|
|
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)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-19 16:22:52 +08:00
|
|
|
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
|
|
|
|
2016-04-19 16:22:52 +08:00
|
|
|
node.add(new SourceNode(null, null, null, '}(window, document));'));
|
2015-08-10 21:04:21 +08:00
|
|
|
|
2016-04-19 16:22:52 +08:00
|
|
|
var bundle = node.toStringWithSourceMap();
|
|
|
|
return {
|
|
|
|
src: bundle.code,
|
|
|
|
srcmap: bundle.map.toString()
|
|
|
|
};
|
2012-12-18 01:15:19 +08:00
|
|
|
}
|
|
|
|
|
2013-11-28 20:56:32 +08:00
|
|
|
function bytesToKB(bytes) {
|
|
|
|
return (bytes / 1024).toFixed(2) + ' KB';
|
|
|
|
};
|
|
|
|
|
2013-12-27 00:46:00 +08:00
|
|
|
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
|
|
|
|
2013-12-27 00:46:00 +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),
|
2016-04-19 16:22:52 +08:00
|
|
|
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
|
|
|
|
2013-11-28 20:56:32 +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);
|
2016-04-19 16:22:52 +08:00
|
|
|
fs.writeFileSync(mapPath, bundle.srcmap);
|
2013-11-28 20:56:32 +08:00
|
|
|
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
|
|
|
|
2013-11-28 20:56:32 +08:00
|
|
|
console.log('\tCompressed: ' + bytesToKB(newCompressed.length) + delta);
|
2012-12-18 01:15:19 +08:00
|
|
|
|
2013-11-28 20:56:32 +08:00
|
|
|
var newGzipped,
|
|
|
|
gzippedDelta = '';
|
|
|
|
|
|
|
|
function done() {
|
2013-11-28 21:20:25 +08:00
|
|
|
if (newCompressed !== oldCompressed) {
|
2013-11-28 20:56:32 +08:00
|
|
|
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
|
|
|
}
|
2013-11-28 20:56:32 +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)) {
|
2013-11-28 20:56:32 +08:00
|
|
|
zlib.gzip(oldCompressed, function (err, oldGzipped) {
|
|
|
|
if (err) { return; }
|
2013-11-28 21:20:25 +08:00
|
|
|
gzippedDelta = getSizeDelta(gzipped, oldGzipped);
|
2013-11-28 20:56:32 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
2012-12-13 01:08:57 +08:00
|
|
|
};
|
2013-01-30 22:06:19 +08:00
|
|
|
|
2014-01-03 06:37:07 +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
|
|
|
|
2016-07-08 17:05:12 +08:00
|
|
|
testConfig.browsers = ['PhantomJSCustom'];
|
2013-02-02 15:29:20 +08:00
|
|
|
|
2013-11-08 23:55:58 +08:00
|
|
|
function isArgv(optName) {
|
|
|
|
return process.argv.indexOf(optName) !== -1;
|
|
|
|
}
|
|
|
|
|
2013-03-02 07:44:11 +08:00
|
|
|
if (isArgv('--chrome')) {
|
|
|
|
testConfig.browsers.push('Chrome');
|
|
|
|
}
|
2013-04-20 18:44:31 +08:00
|
|
|
if (isArgv('--safari')) {
|
|
|
|
testConfig.browsers.push('Safari');
|
|
|
|
}
|
2013-03-02 07:44:11 +08:00
|
|
|
if (isArgv('--ff')) {
|
|
|
|
testConfig.browsers.push('Firefox');
|
|
|
|
}
|
2013-04-20 18:44:31 +08:00
|
|
|
if (isArgv('--ie')) {
|
|
|
|
testConfig.browsers.push('IE');
|
|
|
|
}
|
2013-03-02 07:44:11 +08:00
|
|
|
|
|
|
|
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'];
|
|
|
|
}
|
|
|
|
|
2013-11-08 23:55:58 +08:00
|
|
|
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) {
|
2013-11-08 23:55:58 +08:00
|
|
|
if (!exitCode) {
|
|
|
|
console.log('\tTests ran successfully.\n');
|
2014-01-03 06:37:07 +08:00
|
|
|
complete();
|
|
|
|
} else {
|
|
|
|
process.exit(exitCode);
|
2013-11-08 23:55:58 +08:00
|
|
|
}
|
|
|
|
});
|
2015-09-04 23:46:55 +08:00
|
|
|
server.start();
|
2013-03-02 05:49:20 +08:00
|
|
|
};
|