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
This commit is contained in:
crschmidt
2011-02-24 23:19:53 +00:00
parent 8d2bbfe28f
commit d06c66edab
4 changed files with 67 additions and 8 deletions

View File

@@ -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 This script selects the 'closure' compression mechanism,
./build.py uses a config file called 'full.cfg', and writes the output
cd .. 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.

View File

@@ -12,6 +12,11 @@ def build(config_file = None, output_file = None, options = None):
have_compressor.append("jsmin") have_compressor.append("jsmin")
except ImportError: except ImportError:
print "No jsmin" print "No jsmin"
try:
import closure
have_compressor.append("closure")
except ImportError, E:
print "No closure (%s) % E"
try: try:
import closure_ws import closure_ws
have_compressor.append("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) minimized = minimize.minimize(merged)
elif use_compressor == "closure_ws": elif use_compressor == "closure_ws":
minimized = closure_ws.minimize(merged) minimized = closure_ws.minimize(merged)
elif use_compressor == "closure":
minimized = closure.minimize(merged)
else: # fallback else: # fallback
minimized = merged minimized = merged
print "Adding license file." print "Adding license file."

21
tools/closure.py Normal file
View File

@@ -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

View File

@@ -22,5 +22,7 @@ def minimize(code):
response = conn.getresponse() response = conn.getresponse()
data = response.read() data = response.read()
conn.close() 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 return data