ClosureCompiler API でMinifyする
(ε・◇・)з o O ( SIMPLE_OPTIMIZATIONS の基本的な使い方だよ。
// ClosureCompiler.js: JavaScript source code minifier // // Closure Compiler Service API Reference: // https://developers.google.com/closure/compiler/docs/gettingstarted_api?hl=ja // https://developers.google.com/closure/compiler/docs/api-ref // // Closure Compiler Service // http://closure-compiler.appspot.com/home // (function(global) { // --- header ---------------------------------------------- function ClosureCompiler() { } ClosureCompiler.name = "ClosureCompiler"; ClosureCompiler.minify = minify; // minify(js:String, fn:Function):void // --- library scope vars ---------------------------------- // --- implement ------------------------------------------- function minify(js, // @arg String: js expression fn) { // @arg Function: fn(err:Error, result:String) var http = require("http"); var option = { hostname: "closure-compiler.appspot.com", path: "/compile", port: 80, method: "POST" }; var req = http.request(option, function(res) { //console.log("STATUS: " + res.statusCode); //console.log("HEADERS: " + JSON.stringify(res.headers)); if (res.statusCode !== 200) { fn(new TypeError(res.statusCode), ""); return; } var data = ""; res.on("data", function(chunk) { data += chunk; }); res.on("end", function() { var json = eval("(" + data + ")"); var minified = json.compiledCode; if (json.errors) { console.log("ERR: " + JSON.stringify(json.errors, "", 2)); } if (json.warnings) { console.log("WARN: " + JSON.stringify(json.warnings, "", 2)); } fn(null, minified); }); }); req.setHeader("Content-Type", "application/x-www-form-urlencoded"); req.on("error", function(event) { console.log("problem with request: " + event.message); fn(new TypeError(event.message), ""); }); var requestParameters = [ "output_format=json", "output_info=compiled_code", "output_info=warnings", "output_info=errors", "output_info=statistics", "compilation_level=SIMPLE_OPTIMIZATIONS", "warning_level=default", "js_code=" + encodeURIComponent(js) ]; req.end(requestParameters.join("&"), "utf8"); } // --- build ----------------------------------------------- // --- export ---------------------------------------------- if (typeof module !== "undefined") { module.exports = { ClosureCompiler: ClosureCompiler }; } else { global.ClosureCompiler = ClosureCompiler; } })(this.self || global);