Abandon Closure Compiler in favor of UglifyES (#2285)
Moved to UglifyES, ditched async cruft, clarified responsibilities between cli & minify. Makes for faster, more reliable, synchronous, non-Google-reliant minification.
This commit is contained in:
parent
9d2586df9c
commit
843d7ad454
5 changed files with 67 additions and 126 deletions
|
|
@ -1,69 +1,26 @@
|
|||
"use strict"
|
||||
|
||||
var http = require("https")
|
||||
var querystring = require("querystring")
|
||||
var fs = require("fs")
|
||||
var UglifyES = require("uglify-es")
|
||||
|
||||
module.exports = function(input, output, options, done) {
|
||||
function minify(input, output) {
|
||||
var code = fs.readFileSync(input, "utf8")
|
||||
module.exports = function(filePath, options) {
|
||||
function minify(filePath) {
|
||||
var original = fs.readFileSync(filePath, "utf8"),
|
||||
uglified = UglifyES.minify(original),
|
||||
compressed = uglified.code
|
||||
|
||||
if (uglified.error) throw new Error(uglified.error)
|
||||
|
||||
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",
|
||||
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() {
|
||||
try {
|
||||
var results = JSON.parse(response)
|
||||
} catch(e) {
|
||||
console.error(response);
|
||||
|
||||
throw e;
|
||||
}
|
||||
|
||||
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...")
|
||||
fs.writeFileSync(filePath, compressed, "utf8")
|
||||
return {original: original, compressed: compressed}
|
||||
}
|
||||
|
||||
function run() {
|
||||
minify(input, output)
|
||||
console.log("minifying...")
|
||||
return minify(filePath)
|
||||
}
|
||||
run()
|
||||
|
||||
if (options && options.watch) fs.watchFile(input, run)
|
||||
if (options && options.watch) fs.watchFile(filePath, run)
|
||||
|
||||
return run()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue