node build.js input.js -> GCC Advanced mode, output is 'input.js'
node build.js input.js GCC_A output.js -> GCC Advanced mode, output is 'output.js'
node build.js input.js GCC_S -> GCC Simple mode, output is 'input.js'
node build.js input.js GCC_W -> GCC Whitespace only mode, output is 'input.js'
node build.js input.js RG -> Remove global mode, output is 'input.js'
Created
November 19, 2012 09:32
-
-
Save termi/4109836 to your computer and use it in GitHub Desktop.
JS GCC compile or remove GCC globals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
if (process.argv.length < 3) { | |
console.error('No enough parameters'); | |
process.exit(); | |
} | |
const fs = require('fs') | |
, path = require('path') | |
; | |
var argv = process.argv | |
, file = argv[2] | |
, mode = (argv[3] || "GCC_A").toUpperCase() | |
, gccMode | |
, output = argv[4] || file | |
, i | |
, k | |
, str | |
; | |
file = fs.readFileSync(file) + ""; | |
switch(mode) { | |
case "RG": | |
file = file.replace(/\/\/\s*\[\[\[\|\|\|---=== GCC DEFINES START ===---\|\|\|\]\]\]([\s\S]*)\/\/\s*\[\[\[\|\|\|---=== GCC DEFINES END ===---\|\|\|\]\]\]/i, function(a, find) { | |
str = find; | |
return ""; | |
}); | |
if(str) { | |
file = file.replace(/"use strict"\s*;\s*\n/, '"use strict";\n' + str + "\n"); | |
fs.writeFileSync(output, file); | |
} | |
break; | |
case "GCC_W": | |
if(!gccMode)gccMode = "WHITESPACE_ONLY"; | |
case "GCC_S": | |
if(!gccMode)gccMode = "SIMPLE_OPTIMIZATIONS"; | |
case "GCC_A": | |
if(!gccMode)gccMode = "ADVANCED_OPTIMIZATIONS"; | |
i = require('http').request({ | |
host: 'closure-compiler.appspot.com' | |
, port: 80 | |
, method: 'POST' | |
, headers: { "Content-Type": "application/x-www-form-urlencoded" } | |
, path: '/compile' | |
}, function (res){ | |
var body = ''; | |
res.on('data', function (data){ body += data; }); | |
res.on('end', function () { | |
fs.writeFileSync(output, body); | |
}); | |
}); | |
i.write(require('querystring').stringify({ | |
js_code: file | |
//DEFAULT | |
, compilation_level: gccMode | |
//, formatting : 'pretty_print' | |
, output_format: 'text' | |
// , output_info: 'errors' | |
, output_info: 'compiled_code' | |
}) | |
); | |
i.end(); | |
break; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment