-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminify.js
101 lines (83 loc) · 2.34 KB
/
minify.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var fs=require('fs');
var path=require('path');
// npm install -g uglify-js@1
console.log('NOTE: uglify-js VERSION 1 REQUIRED...');
console.log('npm install -g uglify-js@1');
var jsp = require("uglify-js").parser;
var pro = require("uglify-js").uglify;
// CONCAT --> fullsource
var sourcePath=path.resolve(__dirname,'src');
var files=['header','http','data','helpers','freebase'];
var bigAST;
// ------PARSE CONCAT ------
function parse_concat(try_parse_each)
{
var fullsource;
var fname;
console.log('Reading Files...');
for (var inx=0;inx<files.length;inx++) {
fname=files[inx]+'.js';
console.log(' + ',fname);
fname=path.resolve(sourcePath,fname);
if ( ! fs.existsSync(fname) ) {
console.log('ERR: Source file "' + fname + '" not found.');
return;
}
var source=fs.readFileSync(fname,'utf8');
if (try_parse_each) {
try {
// Parse
console.log('try parse...');
var ast=jsp.parse(source);
} catch (e) {
console.log(e); //parse error
return;
}
}
else {
fullsource+='\n';
fullsource+=source;
}
}
// parse all
if (!try_parse_each)
try {
bigAST=jsp.parse(fullsource);
} catch (e) {
parse_concat(true); //try parse each
return;
}
}
// ------ MANGLE and SQUEEZE ------
function mangle_squeeze()
{ if (bigAST)
try {
console.log('Mangle...');
bigAST = pro.ast_mangle(bigAST);// get a new AST with mangled names
console.log('Squeeze...');
bigAST = pro.ast_squeeze(bigAST); // get an AST with compression optimizations
} catch (e) {
console.log(e);
bigAST=null;
return;
}
}
// ------ GENERATE and SAVE ------
function generate_save()
{ if (bigAST)
{
var dest_fname = 'client_side/node-freebase.min.js';
console.log('Generating and saving...');
fs.writeFile(path.resolve(__dirname,dest_fname), pro.gen_code(bigAST)
, function(err) {
if (err) throw err;
console.log('OK!, minified js:',dest_fname);
});
}
}
// -------------
// --- MAIN ----
// -------------
parse_concat();
mangle_squeeze();
generate_save();