Skip to content

Instantly share code, notes, and snippets.

@twhid
Last active November 13, 2015 11:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twhid/9958123 to your computer and use it in GitHub Desktop.
Save twhid/9958123 to your computer and use it in GitHub Desktop.
#!/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