Skip to content

Instantly share code, notes, and snippets.

@CapacitorSet
Created July 31, 2017 18:01
Show Gist options
  • Save CapacitorSet/40ac6e1c2bebd64d5508029c8fdd9b4c to your computer and use it in GitHub Desktop.
Save CapacitorSet/40ac6e1c2bebd64d5508029c8fdd9b4c to your computer and use it in GitHub Desktop.
// A standalone script that replicates the preprocessing stage in box-js.
const unsafe = true;
const code = require("fs").readFileSync(process.argv[2], "utf8");
const result = require("uglify-es").minify(code, {
compress: {
passes: 3,
booleans: true,
cascade: true,
collapse_vars: true,
comparisons: true,
conditionals: true,
dead_code: true,
drop_console: false,
evaluate: true,
if_return: true,
inline: true,
join_vars: false, // readability
keep_fargs: unsafe, // code may rely on Function.length
keep_fnames: unsafe, // code may rely on Function.prototype.name
keep_infinity: true, // readability
loops: true,
negate_iife: false, // readability
properties: true,
pure_getters: false, // many variables are proxies, which don't have pure getters
/* If unsafe preprocessing is enabled, tell uglify-es that Math.* functions
* have no side effects, and therefore can be removed if the result is
* unused. Related issue: mishoo/UglifyJS2#2227
*/
pure_funcs: unsafe ?
// https://stackoverflow.com/a/10756976
Object.getOwnPropertyNames(Math).map(key => `Math.${key}`) :
null,
reduce_vars: true,
/* Using sequences (a; b; c; -> a, b, c) provides some performance benefits
* (https://github.com/CapacitorSet/box-js/commit/5031ba7114b60f1046e53b542c0e4810aad68a76#commitcomment-23243778),
* but it makes code harder to read. Therefore, this behaviour is disabled.
*/
sequences: false,
toplevel: true,
typeofs: false, // typeof foo == "undefined" -> foo === void 0: the former is more readable
unsafe,
unused: true,
},
output: {
beautify: true,
comments: true,
},
});
console.log(result.code);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment