Skip to content

Instantly share code, notes, and snippets.

@boopathi
Last active December 13, 2015 16:59
Show Gist options
  • Save boopathi/4944562 to your computer and use it in GitHub Desktop.
Save boopathi/4944562 to your computer and use it in GitHub Desktop.
A python based build tool that uses Google Closure Compiler and Coffee-script compiler to build our JavaScript code and CoffeeScript code. For more info, check http://boopathi.in/blog/writing-your-own-javascript-build-system/
#!/usr/bin/python2.7
import httplib, urllib
from os import chdir, path
from subprocess import Popen, PIPE
# Change the script's working directory to
# the script's own directory
chdir(path.dirname(path.abspath(__FILE__)))
# Specify the file locations
coffee_file = './path/to/script.coffee'
js_file = './path/to/script.js'
output_file = './path/to/output.min.js'
#Getting the JS code
with open(js_file) as javascript:
js_data = javascript.read()
# Coffee-script -> JS code
process = Popen(['/usr/bin/coffee','-cp',coffee_file],
stdout=PIPE )
coffee_data = process.communicate()[0]
# Making the request
params = urllib.urlencode([
('code_url', 'http://code.jquery.com/jquery-1.8.0.min.js'), #jquery
('code_url', 'http://underscorejs.org/underscore.js'), #underscore
('code_url', 'http://backbonejs.org/backbone.js'), #backbone
('js_code', js_data),
('js_code', coffee_data),
('compilation_level', 'SIMPLE_OPTIMIZATIONS'),
('output_format', 'text'),
('output_info', 'compiled_code'),
])
headers = {
"Content-Type": "application/x-www-form-urlencoded",
}
conn = httplib.HTTPConnection('closure-compiler.appspot.com')
conn.request('POST','/compile', params, headers)
response = conn.getresponse().read()
conn.close()
# Write the output (minified JS CODE) to output_file
f = open(output_file)
f.write(response)
f.close()
print "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment