File tree Expand file tree Collapse file tree 4 files changed +47
-28
lines changed Expand file tree Collapse file tree 4 files changed +47
-28
lines changed Original file line number Diff line number Diff line change
1
+ Django Compressor Closure API Compile Filter
2
+ ============================================
3
+
4
+ ### Doc
5
+
6
+ - [ What is the Closure Compiler] ( https://developers.google.com/closure/compiler/ )
7
+ - [ Closure Compiler Service API Reference] ( https://developers.google.com/closure/compiler/docs/api-ref )
8
+ - [ Closure Compiler Online] ( https://closure-compiler.appspot.com/ )
9
+
10
+ ### Similar projects
11
+
12
+ - [ Google closure compiler CLI API Docker Image] ( https://github.com/femtopixel/docker-google-closure-compiler-api )
13
+ - [ closure] ( https://github.com/miracle2k/python-closure )
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python3
2
+ import os
3
+
4
+ import sys
5
+
6
+ BASE_DIR = os .path .dirname (os .path .dirname (os .path .abspath (__file__ )))
7
+ sys .path .append (BASE_DIR )
8
+
9
+ from compiler import minify
10
+
11
+
12
+ def main (argv ):
13
+ """Read each argument as file if exits. Or else read stdin. And write to stdout"""
14
+ js_code = ''
15
+
16
+ if argv :
17
+ for file_path in argv :
18
+ with open (file_path ) as file :
19
+ js_code += file .read ()
20
+ elif sys .stdin :
21
+ for line in sys .stdin :
22
+ js_code += line
23
+
24
+ print (minify (js_code ))
25
+
26
+
27
+ if __name__ == '__main__' :
28
+ main (sys .argv [1 :])
Original file line number Diff line number Diff line change 1
- #!/usr/bin/env python3
2
-
3
1
import requests
4
- # src: https://developers.google.com/closure/compiler/?csw=1
5
- # online: https://closure-compiler.appspot.com/home
6
- # compilation_level: WHITESPACE_ONLY, SIMPLE_OPTIMIZATIONS, ADVANCED_OPTIMIZATIONS
7
- # API reference: https://developers.google.com/closure/compiler/docs/api-ref
8
- import sys
9
2
10
3
URL = 'https://closure-compiler.appspot.com/compile'
11
4
COMPILATION_LEVEL = 'SIMPLE_OPTIMIZATIONS'
@@ -17,6 +10,9 @@ class ParseError(Exception):
17
10
def __init__ (self , errors ):
18
11
self .errors = errors
19
12
13
+ def __str__ (self ):
14
+ return str (self .errors )
15
+
20
16
21
17
def minify (text ):
22
18
data = dict (
@@ -28,27 +24,9 @@ def minify(text):
28
24
response = requests .post (URL , data ).json ()
29
25
errors = response .get ('errors' )
30
26
if errors :
31
- raise ParseError (errors = errors )
27
+ raise ParseError (errors )
32
28
33
29
data ['output_info' ] = 'compiled_code'
34
30
response = requests .post (URL , data ).json ()
35
31
36
32
return response ['compiledCode' ]
37
-
38
-
39
- def main (argv ):
40
- js_code = ''
41
-
42
- if argv :
43
- for file_path in argv :
44
- with open (file_path ) as file :
45
- js_code += file .read ()
46
- elif sys .stdin :
47
- for line in sys .stdin :
48
- js_code += line
49
-
50
- print (minify (js_code ))
51
-
52
-
53
- if __name__ == '__main__' :
54
- main (sys .argv [1 :])
Original file line number Diff line number Diff line change 1
1
from compressor .filters import CallbackOutputFilter
2
2
3
3
4
- class GoogleClosureCompileFilter (CallbackOutputFilter ):
4
+ class ClosureAPICompileFilter (CallbackOutputFilter ):
5
5
dependencies = ['requests' ]
6
- callback = 'google_closure_online_compile_filter. compiler.minify'
6
+ callback = 'compiler.minify'
You can’t perform that action at this time.
0 commit comments