Made compress.py script use the official google-closure-compiler release.

The script previously used the PyPI package closure, which is slightly
out of date and not maintained by Google.

The JavaScript contribution docs and the compress.py script now runs the
google-closure-compiler package in the recommended way. Google's
documentation on usage and installation can be found at:

https://github.com/google/closure-compiler-npm/tree/master/packages/google-closure-compiler#usage

This also makes the usage simpler as the package now runs through npm's
npx utility, which will automatically install google-closure-compiler to
a per-user cache.
This commit is contained in:
Jon Dufresne 2020-04-22 07:19:27 -07:00 committed by GitHub
parent f27482f147
commit f48a1990fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 26 deletions

View File

@ -4,37 +4,19 @@ import subprocess
import sys import sys
from pathlib import Path from pathlib import Path
try:
import closure
except ImportError:
closure_compiler = None
else:
closure_compiler = closure.get_jar_filename()
js_path = Path(__file__).parents[1] / 'static' / 'admin' / 'js' js_path = Path(__file__).parents[1] / 'static' / 'admin' / 'js'
def main(): def main():
description = """With no file paths given this script will automatically description = """With no file paths given this script will automatically
compress all jQuery-based files of the admin app. Requires the Google Closure compress files of the admin app. Requires the Google Closure Compiler library
Compiler library and Java version 6 or later.""" and Java version 7 or later."""
parser = argparse.ArgumentParser(description=description) parser = argparse.ArgumentParser(description=description)
parser.add_argument('file', nargs='*') parser.add_argument('file', nargs='*')
parser.add_argument(
"-c", dest="compiler", default="~/bin/compiler.jar",
help="path to Closure Compiler jar file",
)
parser.add_argument("-v", "--verbose", action="store_true", dest="verbose") parser.add_argument("-v", "--verbose", action="store_true", dest="verbose")
parser.add_argument("-q", "--quiet", action="store_false", dest="verbose") parser.add_argument("-q", "--quiet", action="store_false", dest="verbose")
options = parser.parse_args() options = parser.parse_args()
compiler = Path(closure_compiler or options.compiler).expanduser()
if not compiler.exists():
sys.exit(
"Google Closure compiler jar file %s not found. Please use the -c "
"option to specify the path." % compiler
)
if not options.file: if not options.file:
if options.verbose: if options.verbose:
sys.stdout.write("No filenames given; defaulting to admin scripts\n") sys.stdout.write("No filenames given; defaulting to admin scripts\n")
@ -49,13 +31,15 @@ Compiler library and Java version 6 or later."""
to_compress = file_path.expanduser() to_compress = file_path.expanduser()
if to_compress.exists(): if to_compress.exists():
to_compress_min = to_compress.with_suffix('.min.js') to_compress_min = to_compress.with_suffix('.min.js')
cmd = [ cmd = ['npx']
'java', if not options.verbose:
'-jar', str(compiler), cmd.append('-q')
cmd.extend([
'google-closure-compiler',
'--rewrite_polyfills=false', '--rewrite_polyfills=false',
'--js', str(to_compress), '--js', str(to_compress),
'--js_output_file', str(to_compress_min), '--js_output_file', str(to_compress_min),
] ])
if options.verbose: if options.verbose:
sys.stdout.write("Running: %s\n" % ' '.join(cmd)) sys.stdout.write("Running: %s\n" % ' '.join(cmd))
subprocess.run(cmd) subprocess.run(cmd)

View File

@ -55,12 +55,11 @@ version. To run it:
.. console:: .. console::
$ python -m pip install closure
$ python django/contrib/admin/bin/compress.py $ python django/contrib/admin/bin/compress.py
Behind the scenes, ``compress.py`` is a front-end for Google's Behind the scenes, ``compress.py`` is a front-end for Google's
`Closure Compiler`_ which is written in Java. The Closure Compiler library is `Closure Compiler`_ which is written in Java. The Closure Compiler library is
not bundled with Django, but you can install it using pip as done above. The not bundled with Django, but will be installed automatically by ``npm``. The
Closure Compiler library requires `Java`_ 7 or higher. Closure Compiler library requires `Java`_ 7 or higher.
Please don't forget to run ``compress.py`` and include the ``diff`` of the Please don't forget to run ``compress.py`` and include the ``diff`` of the