Simplify Jakefile

This commit is contained in:
mourner 2011-12-12 02:32:53 +02:00
parent 3776e73c00
commit 12b46ae1c4

View File

@ -1,19 +1,11 @@
var deps = require('./build/deps.js').deps, var fs = require('fs'),
fs = require('fs'),
uglify = require('uglify-js'), uglify = require('uglify-js'),
jshint = require('jshint'); jshint = require('jshint'),
deps = require('./build/deps.js').deps;
var copyright = "/*\n Copyright (c) 2010-2011, CloudMade, Vladimir Agafonkin\n" +
" Leaflet is a modern open source JavaScript library for interactive maps.\n" +
" http://leaflet.cloudmade.com\n*/\n";
var savePath = 'dist/leaflet.js';
function getFiles() { function getFiles() {
var memo = {}, var memo = {},
files = []; i, srcs, j, len;
var i, j, src, srcs, len;
for (i in deps) { for (i in deps) {
srcs = deps[i].src; srcs = deps[i].src;
@ -22,6 +14,9 @@ function getFiles() {
} }
} }
var files = [],
src;
for (src in memo) { for (src in memo) {
files.push('src/' + src); files.push('src/' + src);
} }
@ -29,13 +24,10 @@ function getFiles() {
return files; return files;
} }
var files = getFiles();
function myUglify(code){ function myUglify(code){
var jsp = uglify.parser, var pro = uglify.uglify;
pro = uglify.uglify;
var ast = jsp.parse(code); var ast = uglify.parser.parse(code);
ast = pro.ast_mangle(ast); ast = pro.ast_mangle(ast);
ast = pro.ast_squeeze(ast, {keep_comps: false}); ast = pro.ast_squeeze(ast, {keep_comps: false});
ast = pro.ast_squeeze_more(ast); ast = pro.ast_squeeze_more(ast);
@ -43,48 +35,38 @@ function myUglify(code){
return pro.gen_code(ast); return pro.gen_code(ast);
}; };
function compress(content) { function combineFiles(files) {
var content = '';
for (var i = 0, len = files.length; i < len; i++) {
content += fs.readFileSync(files[i], 'utf8') + '\n\n';
}
return content;
}
var COPYRIGHT = "/*\n Copyright (c) 2010-2011, CloudMade, Vladimir Agafonkin\n" +
" Leaflet is a modern open source JavaScript library for interactive maps.\n" +
" http://leaflet.cloudmade.com\n*/\n";
var SAVE_PATH = 'dist/leaflet.js';
var files = getFiles();
task('build', function () {
console.log('Concatenating ' + files.length + ' files...');
var content = combineFiles(files);
console.log('Uncompressed size: ' + content.length); console.log('Uncompressed size: ' + content.length);
console.log('Compressing...'); console.log('Compressing...');
var compressed = copyright + myUglify(content); var compressed = COPYRIGHT + myUglify(content);
fail("Helo world");
console.log('Compressed size: ' + compressed.length); console.log('Compressed size: ' + compressed.length);
return compressed; fs.writeFileSync(SAVE_PATH, compressed, 'utf8');
}
function combineFiles(files, callback) { console.log('Saved to ' + SAVE_PATH);
var remaining = files.length,
fileContents = new Array(remaining);
console.log('Concatenating ' + remaining + ' files...');
var i, len;
for (i = 0, len = files.length; i < len; i++) {
(function (i) {
fs.readFile(files[i], 'utf8', function (err, contents) {
if (err) { throw err; }
fileContents[i] = contents;
remaining--;
if (remaining === 0) {
callback(fileContents.join('\n\n'));
}
});
}(i));
}
}
task('default', function () {
combineFiles(files, function (content) {
var compressed = compress(content);
fs.writeFile(savePath, compressed, 'utf8', function (err) {
if (err) { throw err; }
console.log('Saved to ' + savePath);
});
});
}); });
task('build', ['default']); task('default', ['build']);