Created
January 5, 2011 17:02
-
-
Save minimal/766594 to your computer and use it in GitHub Desktop.
minify js with closure compiler
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/env python | |
# JS Minify | |
# Python script that can process all JavaScript files in a directory | |
# through the Closure compiler | |
# original: http://taylanpince.com/blog/posts/automated-javascript-minification-with-fabric/ | |
import os | |
from optparse import OptionParser | |
def main(): | |
# Setup the option parser | |
parser = OptionParser( | |
usage="Usage: %prog [options] --dir=SOURCE_DIR", | |
version="%prog 0.1" | |
) | |
parser.set_defaults( | |
compressor="/home/chris/share/compiler-latest/compiler.jar", | |
) | |
parser.add_option("--dir", dest="dir", | |
help="Directory to look for JavaScript files") | |
parser.add_option("--compressor", dest="compressor", | |
help="Path to the Closure compiler jar file") | |
(options, args) = parser.parse_args() | |
if not options.dir: | |
parser.error("You have to specify a directory.") | |
# Loop through all files with .js extension | |
for file in [x for x in os.listdir(options.dir) | |
if os.path.splitext(x)[1].lower() == ".js"]: | |
print "Compressing %s" % file | |
# Setup the input/output paths | |
input = os.path.join(options.dir, file) | |
output = os.path.join(options.dir, os.path.splitext(file)[0] + ".min.js") | |
# Pass the file through the Compressor, save the output as .min.js | |
os.system("java -jar %(compressor)s " | |
"--js %(input)s --js_output_file %(output)s" % { | |
"compressor": options.compressor, | |
"input": input, | |
"output": output, | |
}) | |
# Replace the original file with the minified version | |
os.rename(output, input) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment