Skip to content

Instantly share code, notes, and snippets.

@daffl
Last active March 17, 2021 20:09
Show Gist options
  • Save daffl/5571372 to your computer and use it in GitHub Desktop.
Save daffl/5571372 to your computer and use it in GitHub Desktop.
Use UglifyJS 2 (uglify-js package) to compress a string of JavaScript source code
var UglifyJS = require('uglify-js');
module.exports = function(code) {
var toplevel = UglifyJS.parse(code);
toplevel.figure_out_scope();
var compressor = UglifyJS.Compressor({
warnings: false
});
var compressed = toplevel.transform(compressor);
compressed.figure_out_scope();
compressed.compute_char_frequency();
compressed.mangle_names();
return compressed.print_to_string();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment