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 |
| 18 from compile2 import Checker |
| 19 |
| 20 |
| 21 def Minify(source): |
| 22 parser = argparse.ArgumentParser() |
| 23 parser.add_argument( |
| 24 '-c', |
| 25 '--closure_args', |
| 26 nargs=argparse.ZERO_OR_MORE, |
| 27 help='Arguments passed directly to the Closure compiler') |
| 28 args = parser.parse_args() |
| 29 with tempfile.NamedTemporaryFile(suffix='.js') as t1, \ |
| 30 tempfile.NamedTemporaryFile(suffix='.js') as t2: |
| 31 t1.write(source) |
| 32 t1.seek(0) |
| 33 checker = Checker() |
| 34 (compile_error, compile_stderr) = checker.check( |
| 35 [t1.name], |
| 36 out_file=t2.name, |
| 37 closure_args=args.closure_args, |
| 38 enable_chrome_pass=False) |
| 39 if compile_error: |
| 40 print compile_stderr |
| 41 t2.seek(0) |
| 42 result = t2.read() |
| 43 return result |
| 44 |
| 45 |
| 46 if __name__ == '__main__': |
| 47 orig_stdout = sys.stdout |
| 48 result = '' |
| 49 try: |
| 50 sys.stdout = sys.stderr |
| 51 result = Minify(sys.stdin.read()) |
| 52 finally: |
| 53 sys.stdout = orig_stdout |
| 54 print result |
OLD | NEW |