Skip to content

Instantly share code, notes, and snippets.

@circa10a
Last active July 3, 2018 19:45
Show Gist options
  • Save circa10a/52c9896aaf857e683a048e1d5141f47f to your computer and use it in GitHub Desktop.
Save circa10a/52c9896aaf857e683a048e1d5141f47f to your computer and use it in GitHub Desktop.
One line to safely minimize js files using sed(there will be single spaces left to not break variable assignment)
# Requires appropriate semi-colons
FILE_PATH=$(pwd); for i in $(ls $FILE_PATH/*.js); do sed -i 's|//.*||g; /\/\*/,/*\//d; /^\s*$/d;' $i && sed -i ':a;N;$!ba;s/\n/ /g; s/ //g' $i; done
# Before
# const os = require('os');
# function num(num){
# return 5 + num
# }
# // Sample Comment
# /* More
# Comments
# */
# module.exports = {
# "randNum": num(5),
# "host": {
# "name": os.hostname(),
# "homeDir": os.homedir()
# }
# };
# After
# const os = require('os'); function num(num){ return 5 + num } module.exports = { "randNum": num(5), "host": { "name": os.hostname(), "homeDir": os.homedir() } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment