Skip to content

Instantly share code, notes, and snippets.

@kirang89
Created September 13, 2013 14:41
Show Gist options
  • Save kirang89/6551624 to your computer and use it in GitHub Desktop.
Save kirang89/6551624 to your computer and use it in GitHub Desktop.
Simple javascript minifier using closure compiler
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
from requests.exceptions import ConnectionError, HTTPError, Timeout
import sys
def minifyjs(content, target):
URL = 'http://closure-compiler.appspot.com/compile'
data = {
"js_code": content,
"compilation_level": "WHITESPACE_ONLY",
"output_format": "text",
"output_info": "compiled_code"
}
headers = { "Content-type": "application/x-www-form-urlencoded" }
try:
response = requests.post(URL, data=data, headers=headers)
except (ConnectionError, HTTPError, Timeout) as e:
print e
response = ""
with open(target, 'w') as target_file:
target_file.write(response.content)
if __name__ == '__main__':
if sys.argv[1] == 'help':
print """Minify your javascript file using this script
Usage:
python jsminifier.py help
- Shows the help menu
python jsminifier.py [sourcefile] [targetfile]
- Minifies content in sourcefile and stores
it to targetfile
[sourcefile] - Path of the source file
[targetfile] - Path of the target file
The script creates a new file if targetfile
does not already exist.
"""
else:
source, target = sys.argv[1], sys.argv[2]
content = open(source).read()
minifyjs(content, target)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment