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,11 +1,12 @@
|
||||||
"use strict"
|
"use strict"
|
||||||
|
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
|
var zlib = require("zlib")
|
||||||
|
|
||||||
var bundle = require("./bundle")
|
var bundle = require("./bundle")
|
||||||
var minify = require("./minify")
|
var minify = require("./minify")
|
||||||
|
|
||||||
var aliases = {o: "output", m: "minify", w: "watch", a: "aggressive", s: "save"}
|
var aliases = {o: "output", m: "minify", w: "watch", s: "save"}
|
||||||
var params = {}
|
var params = {}
|
||||||
var args = process.argv.slice(2), command = null
|
var args = process.argv.slice(2), command = null
|
||||||
for (var i = 0; i < args.length; i++) {
|
for (var i = 0; i < args.length; i++) {
|
||||||
|
|
@ -24,26 +25,31 @@ function add(value) {
|
||||||
command = null
|
command = null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function format(n) {
|
||||||
|
return n.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")
|
||||||
|
}
|
||||||
|
|
||||||
bundle(params.input, params.output, {watch: params.watch})
|
bundle(params.input, params.output, {watch: params.watch})
|
||||||
if (params.minify) {
|
if (params.minify) {
|
||||||
minify(params.output, params.output, {watch: params.watch, advanced: params.aggressive}, function(stats) {
|
// mFiles = { original: String(mithril.js), compressed: String(mithril.min.js) }
|
||||||
function format(n) {
|
var mFiles = minify(params.output, {watch: params.watch})
|
||||||
return n.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")
|
var originalSize = mFiles.original.length
|
||||||
}
|
var compressedSize = mFiles.compressed.length
|
||||||
|
var originalGzipSize = zlib.gzipSync(mFiles.original).byteLength
|
||||||
|
var compressedGzipSize = zlib.gzipSync(mFiles.compressed).byteLength
|
||||||
|
|
||||||
console.log("Original size: " + format(stats.originalGzipSize) + " bytes gzipped (" + format(stats.originalSize) + " bytes uncompressed)")
|
console.log("Original size: " + format(originalGzipSize) + " bytes gzipped (" + format(originalSize) + " bytes uncompressed)")
|
||||||
console.log("Compiled size: " + format(stats.compressedGzipSize) + " bytes gzipped (" + format(stats.compressedSize) + " bytes uncompressed)")
|
console.log("Compiled size: " + format(compressedGzipSize) + " bytes gzipped (" + format(compressedSize) + " bytes uncompressed)")
|
||||||
|
|
||||||
if (params.save) {
|
if (params.save) {
|
||||||
var readme = fs.readFileSync("./README.md", "utf8")
|
var readme = fs.readFileSync("./README.md", "utf8")
|
||||||
var kb = stats.compressedGzipSize / 1000
|
var kb = compressedGzipSize / 1000
|
||||||
|
|
||||||
fs.writeFileSync("./README.md",
|
fs.writeFileSync("./README.md",
|
||||||
readme.replace(
|
readme.replace(
|
||||||
/(<!-- size -->)(.+?)(<!-- \/size -->)/,
|
/(<!-- size -->)(.+?)(<!-- \/size -->)/,
|
||||||
"$1" + (kb % 1 ? kb.toFixed(2) : kb) + " KB$3"
|
"$1" + (kb % 1 ? kb.toFixed(2) : kb) + " KB$3"
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
)
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,69 +1,26 @@
|
||||||
"use strict"
|
"use strict"
|
||||||
|
|
||||||
var http = require("https")
|
|
||||||
var querystring = require("querystring")
|
|
||||||
var fs = require("fs")
|
var fs = require("fs")
|
||||||
|
var UglifyES = require("uglify-es")
|
||||||
|
|
||||||
module.exports = function(input, output, options, done) {
|
module.exports = function(filePath, options) {
|
||||||
function minify(input, output) {
|
function minify(filePath) {
|
||||||
var code = fs.readFileSync(input, "utf8")
|
var original = fs.readFileSync(filePath, "utf8"),
|
||||||
|
uglified = UglifyES.minify(original),
|
||||||
|
compressed = uglified.code
|
||||||
|
|
||||||
|
if (uglified.error) throw new Error(uglified.error)
|
||||||
|
|
||||||
var data = {
|
fs.writeFileSync(filePath, compressed, "utf8")
|
||||||
output_format: "json",
|
return {original: original, compressed: compressed}
|
||||||
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...")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function run() {
|
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()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
49
mithril.min.js
vendored
49
mithril.min.js
vendored
File diff suppressed because one or more lines are too long
24
package-lock.json
generated
24
package-lock.json
generated
|
|
@ -1972,6 +1972,30 @@
|
||||||
"integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=",
|
"integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"uglify-es": {
|
||||||
|
"version": "3.3.9",
|
||||||
|
"resolved": "https://registry.npmjs.org/uglify-es/-/uglify-es-3.3.9.tgz",
|
||||||
|
"integrity": "sha512-r+MU0rfv4L/0eeW3xZrd16t4NZfK8Ld4SWVglYBb7ez5uXFWHuVRs6xCTrf1yirs9a4j4Y27nn7SRfO6v67XsQ==",
|
||||||
|
"dev": true,
|
||||||
|
"requires": {
|
||||||
|
"commander": "~2.13.0",
|
||||||
|
"source-map": "~0.6.1"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"commander": {
|
||||||
|
"version": "2.13.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/commander/-/commander-2.13.0.tgz",
|
||||||
|
"integrity": "sha512-MVuS359B+YzaWqjCL/c+22gfryv+mCBPHAv3zyVI2GN8EY6IRP8VwtasXn8jyyhvvq84R4ImN1OKRtcbIasjYA==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
|
"source-map": {
|
||||||
|
"version": "0.6.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
|
||||||
|
"integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
|
||||||
|
"dev": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"uglify-js": {
|
"uglify-js": {
|
||||||
"version": "2.8.29",
|
"version": "2.8.29",
|
||||||
"resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-2.8.29.tgz",
|
"resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-2.8.29.tgz",
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,8 @@
|
||||||
"lint-staged": "^4.0.4",
|
"lint-staged": "^4.0.4",
|
||||||
"locater": "^1.3.0",
|
"locater": "^1.3.0",
|
||||||
"marked": "^0.3.19",
|
"marked": "^0.3.19",
|
||||||
"pinpoint": "^1.1.0"
|
"pinpoint": "^1.1.0",
|
||||||
|
"uglify-es": "^3.3.9"
|
||||||
},
|
},
|
||||||
"bin": {
|
"bin": {
|
||||||
"ospec": "./ospec/bin/ospec"
|
"ospec": "./ospec/bin/ospec"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue