Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/python | |
| 2 # Copyright 2016 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 ''' Javascript minifier using the closure compiler | |
| 6 | |
| 7 This minifier strips spaces and comments out of Javascript using the closure | |
| 8 compiler. It takes the original Javascript on standard input, and outputs | |
| 9 the minified output on standard output. | |
| 10 | |
| 11 Any errors or other messages from the compiler are output on standard error. | |
| 12 ''' | |
| 13 | |
| 14 import argparse | |
| 15 import sys | |
| 16 import tempfile | |
| 17 from compile2 import Checker | |
|
Dan Beam
2016/07/28 16:27:05
nit: \n\n between top-level
aberent
2016/08/02 11:43:29
Done.
| |
| 18 | |
| 19 def Minify(source): | |
| 20 parser = argparse.ArgumentParser() | |
| 21 parser.add_argument("-c", "--closure_args", nargs=argparse.ZERO_OR_MORE, | |
| 22 help="Arguments passed directly to the Closure compiler") | |
| 23 args = parser.parse_args() | |
| 24 closure_args = args.closure_args + ['compilation_level=WHITESPACE_ONLY'] | |
| 25 with tempfile.NamedTemporaryFile(suffix='.js') as t1, \ | |
| 26 tempfile.NamedTemporaryFile(suffix='.js') as t2: | |
|
Dan Beam
2016/07/28 16:27:05
how do these get deleted? should you be specifyin
aberent
2016/08/02 11:43:29
No, it is the default, as you might expect, for te
| |
| 27 t1.write(source) | |
| 28 t1.seek(0) | |
| 29 checker = Checker() | |
| 30 (compile_error, compile_stderr) = checker.check( | |
| 31 [t1.name], out_file=t2.name, closure_args=closure_args) | |
| 32 if compile_error: | |
| 33 print compile_stderr | |
| 34 t2.seek(0) | |
| 35 result = t2.read() | |
| 36 return result | |
| 37 | |
| 38 | |
| 39 if __name__ == '__main__': | |
| 40 orig_stdout = sys.stdout | |
| 41 result = '' | |
| 42 try: | |
| 43 sys.stdout = sys.stderr | |
| 44 result = Minify(sys.stdin.read()) | |
| 45 finally: | |
| 46 sys.stdout = orig_stdout | |
| 47 print result | |
| OLD | NEW |