forked from BantouTelecom/dialpad.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
61 lines (48 loc) · 1.89 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var jade = require('jade'),
uglify = require('uglify-js'),
_ = require('underscore'),
fs = require('fs'),
jadeRuntime,
templatefunc,
main;
var jadeRuntime;
try {
jadeRuntime = fs.readFileSync(__dirname + '/../jade/runtime.min.js', 'utf-8');
} catch (e) {
jadeRuntime = fs.readFileSync(__dirname + '/node_modules/jade/runtime.min.js', 'utf-8');
}
var emitter = fs.readFileSync(__dirname + '/src/wildemitter.js', 'utf-8');
// indents each line in a file by 4 spaces or whatever you pass into it
function indent(file, indentAmount) {
var split = file.split('\n'),
actualIndent = indentAmount || ' ',
i = 0,
l = split.length;
for (; i < l; i++) {
split[i] = actualIndent + split[i];
}
return split.join('\n');
}
function beautify(code) {
var ast = uglify.parser.parse(code);
return uglify.uglify.gen_code(ast, {beautify: true});
}
function minify(name) {
var code = fs.readFileSync(__dirname + '/' + name + '.js', 'utf-8'),
ast = uglify.parser.parse(code),
pro = uglify.uglify,
minified;
ast = pro.ast_mangle(ast); // get a new AST with mangled names
ast = pro.ast_squeeze(ast); // get an AST with compression optimizations
minified = pro.gen_code(ast); // build out the code
fs.writeFileSync(__dirname + '/' + name + '.min.js', minified);
}
function build(name) {
templatefunc = beautify(jade.compile(fs.readFileSync(__dirname + '/src/' + name + '.jade', 'utf-8'), {client: true, compileDebug: false, pretty: true}).toString());
main = fs.readFileSync(__dirname + '/src/' + name + '.js', 'utf-8').toString().replace("{{{templatefunc}}}", templatefunc);
main = main.replace("{{{jaderuntime}}}", jadeRuntime);
main = main.replace("{{{emitter}}}", indent(emitter, ' '));
fs.writeFileSync(__dirname + '/' + name + '.js', main);
minify(name);
}
build('dialpad');