diff --git a/build/build.py b/build/build.py index da539a29ab..fd0f6e9bb1 100755 --- a/build/build.py +++ b/build/build.py @@ -32,6 +32,13 @@ def build(config_file = None, output_file = None, options = None): except ImportError: print "No minimize" + try: + import uglify_js + uglify_js.check_available() + have_compressor.append("uglify-js") + except Exception, E: + print "No uglify-js (%s)" % E + use_compressor = None if options.compressor and options.compressor in have_compressor: use_compressor = options.compressor @@ -52,7 +59,7 @@ def build(config_file = None, output_file = None, options = None): print "Merging libraries." try: - if use_compressor == "closure": + if use_compressor == "closure" or use_compressor == 'uglify-js': sourceFiles = mergejs.getNames(sourceDirectory, configFilename) else: merged = mergejs.run(sourceDirectory, None, configFilename) @@ -107,6 +114,14 @@ def build(config_file = None, output_file = None, options = None): print "\nAbnormal termination due to compilation errors." sys.exit("ERROR: Closure Compilation failed! See compilation errors.") print "Closure Compilation has completed successfully." + elif use_compressor == "uglify-js": + minimized = uglify_js.compile(sourceFiles) + if minimized is None: + print "\nAbnormal termination due to compilation errors." + sys.exit("ERROR: Uglify JS compilation failed! See compilation errors.") + + print "Uglify JS compilation has completed successfully." + else: # fallback minimized = merged diff --git a/tools/uglify_js.py b/tools/uglify_js.py new file mode 100644 index 0000000000..50ef0984c5 --- /dev/null +++ b/tools/uglify_js.py @@ -0,0 +1,35 @@ +"""Utility to use the Uglify JS Compiler CLI from Python.""" + +import logging +import subprocess + + +def check_available(): + """ Returns whether the uglify-js tool is available. """ + subprocess.check_output(['which', 'uglifyjs']) + + +def compile(source_paths, flags=None): + """ + Prepares command-line call to uglify-js compiler. + + Args: + source_paths: Source paths to build, in order. + flags: A list of additional flags to pass on to uglify-js. + + Returns: + The compiled source, as a string, or None if compilation failed. + """ + + args = ['uglifyjs'] + args.extend(source_paths) + args.extend(['-c', '-m']) + if flags: + args += flags + + logging.info('Compiling with the following command: %s', ' '.join(args)) + + try: + return subprocess.check_output(args) + except subprocess.CalledProcessError: + return