| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 ''' Javascript minifier using the closure compiler | 5 ''' Javascript minifier using the closure compiler |
| 6 | 6 |
| 7 This minifier strips spaces and comments out of Javascript using the closure | 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 | 8 compiler. It takes the original Javascript on standard input, and outputs |
| 9 the minified output on standard output. | 9 the minified output on standard output. |
| 10 | 10 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 def Minify(source): | 21 def Minify(source): |
| 22 parser = argparse.ArgumentParser() | 22 parser = argparse.ArgumentParser() |
| 23 parser.add_argument("-c", "--closure_args", nargs=argparse.ZERO_OR_MORE, | 23 parser.add_argument("-c", "--closure_args", nargs=argparse.ZERO_OR_MORE, |
| 24 help="Arguments passed directly to the Closure compiler") | 24 help="Arguments passed directly to the Closure compiler") |
| 25 args = parser.parse_args() | 25 args = parser.parse_args() |
| 26 with tempfile.NamedTemporaryFile(suffix='.js') as t1, \ | 26 with tempfile.NamedTemporaryFile(suffix='.js') as t1, \ |
| 27 tempfile.NamedTemporaryFile(suffix='.js') as t2: | 27 tempfile.NamedTemporaryFile(suffix='.js') as t2: |
| 28 t1.write(source) | 28 t1.write(source) |
| 29 t1.seek(0) | 29 t1.seek(0) |
| 30 checker = Checker() | 30 checker = Checker() |
| 31 (compile_error, compile_stderr) = checker.check( | 31 (_, compile_error, compile_stderr) = checker.check( |
| 32 [t1.name], | 32 [t1.name], |
| 33 out_file=t2.name, | 33 out_file=t2.name, |
| 34 closure_args=args.closure_args) | 34 closure_args=args.closure_args) |
| 35 if compile_error: | 35 if compile_error: |
| 36 print compile_stderr | 36 print compile_stderr |
| 37 t2.seek(0) | 37 t2.seek(0) |
| 38 result = t2.read() | 38 result = t2.read() |
| 39 return result | 39 return result |
| 40 | 40 |
| 41 | 41 |
| 42 if __name__ == '__main__': | 42 if __name__ == '__main__': |
| 43 orig_stdout = sys.stdout | 43 orig_stdout = sys.stdout |
| 44 result = '' | 44 result = '' |
| 45 try: | 45 try: |
| 46 sys.stdout = sys.stderr | 46 sys.stdout = sys.stderr |
| 47 result = Minify(sys.stdin.read()) | 47 result = Minify(sys.stdin.read()) |
| 48 finally: | 48 finally: |
| 49 sys.stdout = orig_stdout | 49 sys.stdout = orig_stdout |
| 50 print result | 50 print result |
| OLD | NEW |