Leaflet/build/build.js

79 lines
1.7 KiB
JavaScript
Raw Normal View History

2011-12-12 18:44:16 +08:00
var fs = require('fs'),
uglifyjs = require('uglify-js'),
deps = require('./deps.js').deps;
exports.getFiles = function (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++) {
memo[srcs[j]] = true;
}
}
for (var i in deps) {
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 = [];
for (var src in memo) {
files.push('src/' + src);
}
return files;
2011-12-13 06:41:56 +08:00
};
2011-12-12 18:44:16 +08:00
exports.uglify = function (code) {
var pro = uglifyjs.uglify;
var ast = uglifyjs.parser.parse(code);
2012-06-02 15:55:59 +08:00
ast = pro.ast_mangle(ast, {mangle: true});
ast = pro.ast_squeeze(ast);
2011-12-12 18:44:16 +08:00
ast = pro.ast_squeeze_more(ast);
2012-01-27 04:45:12 +08:00
return pro.gen_code(ast) + ';';
2011-12-13 06:41:56 +08:00
};
2011-12-12 18:44:16 +08:00
exports.combineFiles = function (files) {
var content = '(function (window, undefined) {\n\n';
2011-12-12 18:44:16 +08:00
for (var i = 0, len = files.length; i < len; i++) {
2012-06-02 15:55:59 +08:00
content += fs.readFileSync(files[i], 'utf8') + '\n\n';
2011-12-12 18:44:16 +08:00
}
return content + '\n\n}(this));';
2011-12-13 06:41:56 +08:00
};
2011-12-12 18:44:16 +08:00
exports.save = function (savePath, compressed) {
2011-12-13 06:41:56 +08:00
return fs.writeFileSync(savePath, compressed, 'utf8');
};
exports.load = function (loadPath) {
try {
return fs.readFileSync(loadPath, 'utf8');
} catch (e) {
return null;
}
};
exports.getSizeDelta = function (newContent, oldContent) {
if (!oldContent) {
return 'new';
}
var delta = newContent.replace(/\r\n?/g, '\n').length - oldContent.replace(/\r\n?/g, '\n').length;
2011-12-13 06:41:56 +08:00
return (delta >= 0 ? '+' : '') + delta;
};