-
Notifications
You must be signed in to change notification settings - Fork 1
/
vash.packer.js
70 lines (56 loc) · 1.99 KB
/
vash.packer.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
/*
* vash.packer.js - Javascript node compressor
* http://github.com/G33kLabs/vash.packer.js
*/
/*global define: false*/
var htmlPacker = require('html-minifier').minify,
jsParser = require("uglify-js").parser,
jsPacker = require("uglify-js").uglify,
cssPacker = require('uglifycss') ;
(function (exports) {
if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
module.exports = exports;
} else {
window.packer = exports;
}
}(function () {
var exports = {};
/* Pack CSS content */
exports.packCSS = function(content) {
return cssPacker.processString(content) ;
} ;
/* Pack HTML content */
exports.packHTML = function(content) {
return htmlPacker(content, { removeComments: true, collapseWhitespace: false, removeEmptyAttributes: true }) ;
} ;
/* Pack JS content */
exports.packJS = function(content, itemPath) {
/* Try to parse content */
try {
var ast = jsParser.parse(content); /* parse code and get the initial AST */
ast = jsPacker.ast_mangle(ast); /* get a new AST with mangled names */
ast = jsPacker.ast_squeeze(ast); /* get an AST with compression optimizations */
}
/* Report error */
catch(e) {
/* Set a compile error flag */
this.compileError = true ;
/* Output error */
console.log('-------------------------') ;
if ( itemPath ) {
console.log(" /* Path : "+itemPath+" */ ");
}
console.log(content.split("\n").slice(e.line-10, e.line-1).join("\n")) ;
console.log(" /******** "+e.line+" >> "+e.message+" ********/") ;
console.log((content.split("\n").slice(e.line-1, e.line).join("\n"))) ;
console.log(" /***********************************************************/") ;
console.log(content.split("\n").slice(e.line, e.line+10).join("\n")) ;
/* Say something (designed for OSX) */
try {
require('child_process').exec('say -v Alex -r 200 "Houston ? Yeah a problem in code..."') ;
} catch(e) {}
}
return jsPacker.gen_code(ast); // compressed code here
} ;
return exports;
}()));