/Minimize all JavaScript files in a folder (recursive) using Google Closure Compiler Web Service API
Created
March 14, 2010 11:13
-
-
Save tnhu/331920 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import httplib, urllib, sys | |
import os | |
import fnmatch | |
def googleClosureCompiler(rpath, wpath): | |
f = open(rpath, 'r') | |
s = f.readlines() | |
s = "".join(s); | |
f.close() | |
params = urllib.urlencode([ | |
('js_code', s), | |
('compilation_level', 'SIMPLE_OPTIMIZATIONS'), | |
('output_format', 'text'), | |
('output_info', 'compiled_code'), | |
]) | |
f.close(); | |
headers = { "Content-type": "application/x-www-form-urlencoded" } | |
conn = httplib.HTTPConnection('closure-compiler.appspot.com') | |
conn.request('POST', '/compile', params, headers) | |
response = conn.getresponse() | |
data = response.read() | |
conn.close | |
f = open(wpath, 'w') | |
f.write(data) | |
f.close() | |
for top, dirs, files in os.walk('YOUR-JAVASCRIPT-FOLDER'): | |
for nm in fnmatch.filter(files, '*.js'): | |
s = os.path.join(top, nm) | |
if s.find('/test/') == -1: # Ignore /test folder | |
googleClosureCompiler(s, s.replace('.js', '.min.js')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You better use kjscompiler - makes compilation of multiple JavaScript files with Google Closure Compiler application in right order.
https://github.com/knyga/kjscompiler