Permalink
Browse files

Updating build scripts.

  • Loading branch information...
jacobtoye committed Mar 12, 2013
1 parent eafc08b commit be6358ee221e0c4ea818f8a5ffe986a6c44b9e06
Showing with 157 additions and 89 deletions.
  1. +13 −57 Jakefile.js
  2. +122 −31 build/build.js
  3. +9 −1 build/deps.js
  4. +5 −0 src/Leaflet.label.js
  5. +8 −0 src/copyright.js
View
@@ -1,65 +1,21 @@
var build = require('./build/build.js'),
lint = require('./build/hint.js');
/*
Leaflet.label building and linting scripts.
var COPYRIGHT = '/*\n Copyright (c) 2012, Smartrak, Jacob Toye\n' +
' Leaflet.label is an open-source JavaScript library for adding labels to markers and paths on leaflet powered maps.\n' +
' https://github.com/jacobtoye/Leaflet.label\n*/\n';
To use, install Node, then run the following commands in the project root:
desc('Check source for errors with JSHint');
task('lint', function () {
var files = build.getFiles();
npm install -g jake
npm install uglify-js
npm install jshint
console.log('Checking for JS errors...');
To check the code and build Leaflet.label from source, run "jake"
*/
var errorsFound = lint.jshint(files);
var build = require('./build/build.js');
if (errorsFound > 0) {
console.log(errorsFound + ' error(s) found.\n');
fail();
} else {
console.log('\tCheck passed');
}
});
desc('Check Leaflet.label source for errors with JSHint');
task('lint', build.lint);
desc('Combine and compress source files');
task('build', ['lint'], function () {
var pathPart = 'dist/leaflet.label',
srcPath = pathPart + '-src.js',
path = pathPart + '.js';
var files = build.getFiles();
console.log('Concatenating ' + files.length + ' files...');
var content = build.combineFiles(files);
var oldSrc = build.load(srcPath),
newSrc = COPYRIGHT + content,
srcDelta = build.getSizeDelta(newSrc, oldSrc);
console.log('\tUncompressed size: ' + newSrc.length + ' bytes (' + srcDelta + ')');
if (newSrc === oldSrc) {
console.log('\tNo changes');
} else {
build.save(srcPath, newSrc);
console.log('\tSaved to ' + srcPath);
}
console.log('Compressing...');
var oldCompressed = build.load(path),
newCompressed = COPYRIGHT + build.uglify(content),
delta = build.getSizeDelta(newCompressed, oldCompressed);
console.log('\tCompressed size: ' + newCompressed.length + ' bytes (' + delta + ')');
if (newCompressed === oldCompressed) {
console.log('\tNo changes');
} else {
build.save(path, newCompressed);
console.log('\tSaved to ' + path);
}
});
desc('Combine and compress Leaflet.label source files');
task('build', ['lint'], build.build);
task('default', ['build']);
View
@@ -1,9 +1,40 @@
var fs = require('fs'),
uglifyjs = require('uglify-js'),
deps = require('./deps.js').deps;
jshint = require('jshint'),
UglifyJS = require('uglify-js'),
exports.getFiles = function () {
var memo = {};
deps = require('./deps.js').deps,
hintrc = require('./hintrc.js').config;
function lintFiles(files) {
var errorsFound = 0,
i, j, len, len2, src, errors, e;
for (i = 0, len = files.length; i < len; i++) {
jshint.JSHINT(fs.readFileSync(files[i], 'utf8'), hintrc);
errors = jshint.JSHINT.errors;
for (j = 0, len2 = errors.length; j < len2; j++) {
e = errors[j];
console.log(files[i] + '\tline ' + e.line + '\tcol ' + e.character + '\t ' + e.reason);
}
errorsFound += len2;
}
return errorsFound;
}
function getFiles(compsBase32) {
var memo = {},
comps;
if (compsBase32) {
comps = parseInt(compsBase32, 32).toString(2).split('');
console.log('Managing dependencies...');
}
function addFiles(srcs) {
for (var j = 0, len = srcs.length; j < len; j++) {
@@ -12,7 +43,16 @@ exports.getFiles = function () {
}
for (var i in deps) {
addFiles(deps[i].src);
if (comps) {
if (parseInt(comps.pop(), 2) === 1) {
console.log('\t* ' + i);
addFiles(deps[i].src);
} else {
console.log('\t ' + i);
}
} else {
addFiles(deps[i].src);
}
}
var files = [];
@@ -22,43 +62,94 @@ exports.getFiles = function () {
}
return files;
};
}
exports.uglify = function (code) {
var pro = uglifyjs.uglify;
exports.lint = function () {
var ast = uglifyjs.parser.parse(code);
ast = pro.ast_mangle(ast, {mangle: true});
ast = pro.ast_squeeze(ast);
ast = pro.ast_squeeze_more(ast);
var files = getFiles();
return pro.gen_code(ast) + ';';
};
console.log('Checking for JS errors...');
exports.combineFiles = function (files) {
var content = '(function (window, undefined) {\n\n';
for (var i = 0, len = files.length; i < len; i++) {
content += fs.readFileSync(files[i], 'utf8') + '\n\n';
var errorsFound = lintFiles(files);
if (errorsFound > 0) {
console.log(errorsFound + ' error(s) found.\n');
fail();
} else {
console.log('\tCheck passed');
}
return content + '\n\n}(this));';
};
exports.save = function (savePath, compressed) {
return fs.writeFileSync(savePath, compressed, 'utf8');
};
exports.load = function (loadPath) {
function getSizeDelta(newContent, oldContent) {
if (!oldContent) {
return 'new';
}
var newLen = newContent.replace(/\r\n?/g, '\n').length,
oldLen = oldContent.replace(/\r\n?/g, '\n').length,
delta = newLen - oldLen;
return (delta >= 0 ? '+' : '') + delta;
}
function loadSilently(path) {
try {
return fs.readFileSync(loadPath, 'utf8');
return fs.readFileSync(path, 'utf8');
} catch (e) {
return null;
}
};
}
exports.getSizeDelta = function (newContent, oldContent) {
if (!oldContent) {
return 'new';
function combineFiles(files) {
var content = '';
for (var i = 0, len = files.length; i < len; i++) {
content += fs.readFileSync(files[i], 'utf8') + '\n\n';
}
var delta = newContent.length - oldContent.length;
return (delta >= 0 ? '+' : '') + delta;
};
return content;
}
exports.build = function (compsBase32, buildName) {
var files = getFiles(compsBase32);
console.log('Concatenating ' + files.length + ' files...');
var copy = fs.readFileSync('src/copyright.js', 'utf8'),
intro = '(function (window, document, undefined) {\n',
outro = '}(this, document));',
newSrc = copy + intro + combineFiles(files) + outro,
pathPart = 'dist/leaflet.label' + (buildName ? '-' + buildName : ''),
srcPath = pathPart + '-src.js',
oldSrc = loadSilently(srcPath),
srcDelta = getSizeDelta(newSrc, oldSrc);
console.log('\tUncompressed size: ' + newSrc.length + ' bytes (' + srcDelta + ')');
if (newSrc === oldSrc) {
console.log('\tNo changes');
} else {
fs.writeFileSync(srcPath, newSrc);
console.log('\tSaved to ' + srcPath);
}
console.log('Compressing...');
var path = pathPart + '.js',
oldCompressed = loadSilently(path),
newCompressed = copy + UglifyJS.minify(newSrc, {
warnings: true,
fromString: true
}).code,
delta = getSizeDelta(newCompressed, oldCompressed);
console.log('\tCompressed size: ' + newCompressed.length + ' bytes (' + delta + ')');
if (newCompressed === oldCompressed) {
console.log('\tNo changes');
} else {
fs.writeFileSync(path, newCompressed);
console.log('\tSaved to ' + path);
}
};
View
@@ -1,4 +1,11 @@
var deps = {
Core: {
src: [
'Leaflet.label.js'
],
desc: 'The core of the plugin. Currently only includes the version.'
},
Label: {
src: [
'Label.js',
@@ -7,7 +14,8 @@ var deps = {
'Map.Label.js',
'FeatureGroup.Label.js'
],
desc: 'Leaflet.label plugin files.'
desc: 'Leaflet.label plugin files.',
deps: ['Core']
}
};
View
@@ -0,0 +1,5 @@
/*
* Leaflet.label assumes that you have already included the Leaflet library.
*/
L.drawVersion = '0.1.2-dev';
View
@@ -0,0 +1,8 @@
/*
Leaflet.label, a plugin that adds labels to markers and vectors for Leaflet powered maps.
(c) 2012-2013, Jacob Toye, Smartrak
https://github.com/Leaflet/Leaflet.label
http://leafletjs.com
https://github.com/jacobtoye
*/

0 comments on commit be6358e

Please sign in to comment.