Last active
November 13, 2015 11:13
-
-
Save twhid/9958123 to your computer and use it in GitHub Desktop.
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/local/bin/node | |
// BBEdit text filter to uglify Javascript. | |
// Uses the default compression options, *except* it turns off warnings because they cause the script to error out. | |
// | |
// Requires uglifyjs https://github.com/mishoo/UglifyJS2 | |
// In this example, I've installed uglifyjs globally via `npm install uglify-js -g` | |
// Your install directory may be different... | |
var UglifyJS = require('/usr/local/share/npm/lib/node_modules/uglify-js'); | |
var chunks = ""; | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf8'); | |
process.stdin.on('data', function(chunk) { | |
chunks += chunk; | |
}); | |
process.stdin.on('end', function() { | |
var options = { | |
fromString: true, | |
compress: { | |
// defaults here: http://lisperator.net/uglifyjs/compress | |
sequences : true, // join consecutive statemets with the “comma operator” | |
properties : true, // optimize property access: a["foo"] → a.foo | |
dead_code : true, // discard unreachable code | |
drop_debugger : true, // discard “debugger” statements | |
unsafe : false, // some unsafe optimizations (see below) | |
conditionals : true, // optimize if-s and conditional expressions | |
comparisons : true, // optimize comparisons | |
evaluate : true, // evaluate constant expressions | |
booleans : true, // optimize boolean expressions | |
loops : true, // optimize loops | |
unused : true, // drop unused variables/functions | |
hoist_funs : true, // hoist function declarations | |
hoist_vars : false, // hoist variable declarations | |
if_return : true, // optimize if-s followed by return/continue | |
join_vars : true, // join var declarations | |
cascade : true, // try to cascade `right` into `left` in sequences | |
side_effects : true, // drop side-effect-free statements | |
warnings : false, // warn about potentially dangerous optimizations/code -- turned this off or script errors out | |
global_defs : {} // global definitions | |
}, | |
mangle: true | |
}; | |
var out = UglifyJS.minify(chunks, options); | |
process.stdout.write(out.code); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment