-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminify.js
More file actions
executable file
·64 lines (52 loc) · 1.46 KB
/
minify.js
File metadata and controls
executable file
·64 lines (52 loc) · 1.46 KB
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
"use strict"
var http = require("http")
var querystring = require("querystring")
var fs = require("fs")
module.exports = function (input, output, options, done) {
function minify(input, output) {
var code = fs.readFileSync(input, "utf8")
var data = {
output_format: "json",
output_info: ["compiled_code", "warnings", "errors", "statistics"],
compilation_level: options.advanced ? "ADVANCED_OPTIMIZATIONS" : "SIMPLE_OPTIMIZATIONS",
warning_level: "default",
output_file_name: "default.js",
js_code: code,
}
var body = querystring.stringify(data)
var response = ""
var req = http.request({
method: "POST",
protocol: "http:",
hostname: "closure-compiler.appspot.com",
path: "/compile",
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
"Content-Length": body.length
}
}, function (res) {
res.on("data", function (chunk) {
response += chunk.toString()
})
res.on("end", function () {
var results = JSON.parse(response)
if (results.errors) {
for (var i = 0; i < results.errors.length; i++) console.log(results.errors[i])
}
else {
fs.writeFileSync(output, results.compiledCode, "utf8")
console.log("done")
if (typeof done === "function") done(results.statistics)
}
})
})
req.write(body)
req.end()
console.log("minifying...")
}
function run() {
minify(input, output)
}
run()
if (options && options.watch) fs.watchFile(input, run)
}