build: Added "uglify-js" as optional compressor
This commit is contained in:
+16
-1
@@ -32,6 +32,13 @@ def build(config_file = None, output_file = None, options = None):
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
print "No minimize"
|
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
|
use_compressor = None
|
||||||
if options.compressor and options.compressor in have_compressor:
|
if options.compressor and options.compressor in have_compressor:
|
||||||
use_compressor = options.compressor
|
use_compressor = options.compressor
|
||||||
@@ -52,7 +59,7 @@ def build(config_file = None, output_file = None, options = None):
|
|||||||
|
|
||||||
print "Merging libraries."
|
print "Merging libraries."
|
||||||
try:
|
try:
|
||||||
if use_compressor == "closure":
|
if use_compressor == "closure" or use_compressor == 'uglify-js':
|
||||||
sourceFiles = mergejs.getNames(sourceDirectory, configFilename)
|
sourceFiles = mergejs.getNames(sourceDirectory, configFilename)
|
||||||
else:
|
else:
|
||||||
merged = mergejs.run(sourceDirectory, None, configFilename)
|
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."
|
print "\nAbnormal termination due to compilation errors."
|
||||||
sys.exit("ERROR: Closure Compilation failed! See compilation errors.")
|
sys.exit("ERROR: Closure Compilation failed! See compilation errors.")
|
||||||
print "Closure Compilation has completed successfully."
|
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
|
else: # fallback
|
||||||
minimized = merged
|
minimized = merged
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
Reference in New Issue
Block a user