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