From d06c66edabec25d86a3e30b063b53f1be4dd75e0 Mon Sep 17 00:00:00 2001 From: crschmidt Date: Thu, 24 Feb 2011 23:19:53 +0000 Subject: [PATCH] Adding support for closure compiler and some documentation in the README about how to use it. git-svn-id: http://svn.openlayers.org/trunk/openlayers@11448 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- build/README.txt | 43 ++++++++++++++++++++++++++++++++++++------- build/build.py | 7 +++++++ tools/closure.py | 21 +++++++++++++++++++++ tools/closure_ws.py | 4 +++- 4 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 tools/closure.py diff --git a/build/README.txt b/build/README.txt index 6c482cf37a..ca8402c718 100644 --- a/build/README.txt +++ b/build/README.txt @@ -1,14 +1,43 @@ +The OpenLayers build tool supports several different +forms of compressing your javascript code, and a method +of describing build profiles to create customized +OpenLayers builds with only the components you need. -## HowTo: Build & deploy "Shrunk" Single File Library version of OpenLayers ## +When building a file, you can choose to build with several +different compression options to the Python-based build.py +script. The following is an example script: - * Build: +python build.py -c closure full OpenLayers-closure.js - cd build - ./build.py - cd .. +This script selects the 'closure' compression mechanism, +uses a config file called 'full.cfg', and writes the output +to OpenLayers-closure.js. - * Upload the result to the server: e.g. +The options available for compression are: - scp build/OpenLayers.js openlayers@openlayers.org:openlayers.org/htdocs/code/ + * closure + This requires you to have a closure-compiler.jar in your + tools directory. You can do this by fetching the compiler + from: + http://closure-compiler.googlecode.com/files/compiler-latest.zip + + Then unzipping that file, and placing compiler.jar into tools + and renaming it closure-compiler.jar. + + * closure_ws + This uses the closure compiler webservice. This will only work + for files source Javascript files which are under 1MB. (Note that + the default OpenLayers full build is not under 1MB.) + + * jsmin + jsmin is the default compiler, and uses the Python-based + jsmin script to compress the Javascript. + + * minimize + This is a simple whitespace removing Python script, designed + to fill in when other tools are unavailable. + + * none + None will leave the Javascript uncompressed. diff --git a/build/build.py b/build/build.py index 06fe082144..24a2f23846 100755 --- a/build/build.py +++ b/build/build.py @@ -12,6 +12,11 @@ def build(config_file = None, output_file = None, options = None): have_compressor.append("jsmin") except ImportError: print "No jsmin" + try: + import closure + have_compressor.append("closure") + except ImportError, E: + print "No closure (%s) % E" try: import closure_ws have_compressor.append("closure_ws") @@ -51,6 +56,8 @@ def build(config_file = None, output_file = None, options = None): minimized = minimize.minimize(merged) elif use_compressor == "closure_ws": minimized = closure_ws.minimize(merged) + elif use_compressor == "closure": + minimized = closure.minimize(merged) else: # fallback minimized = merged print "Adding license file." diff --git a/tools/closure.py b/tools/closure.py new file mode 100644 index 0000000000..e0bb0afa28 --- /dev/null +++ b/tools/closure.py @@ -0,0 +1,21 @@ +import sys +import os +import tempfile + +path = os.path.join(os.path.dirname(__file__), "closure-compiler.jar") +if not os.path.exists(path): + raise Exception("No closure-compiler.jar at %s; read README.txt!" % path) + +def minimize(code): + ntf = tempfile.NamedTemporaryFile() + ntf.write(code) + ntf.flush() + + ntf2 = tempfile.NamedTemporaryFile() + + os.system("java -jar %s --js %s --js_output_file %s" % (path, ntf.name, ntf2.name)) + ntf2.seek(0) + data = ntf2.read() + ntf.close() + ntf2.close() + return data diff --git a/tools/closure_ws.py b/tools/closure_ws.py index fb989d9088..3bf925a729 100644 --- a/tools/closure_ws.py +++ b/tools/closure_ws.py @@ -22,5 +22,7 @@ def minimize(code): response = conn.getresponse() data = response.read() conn.close() - print "%.3f seconds to compile", time.time() - t + if data.startswith("Error"): + raise Exception(data) + print "%.3f seconds to compile" % (time.time() - t) return data