Skip to content

Commit 5fb7ac3

Browse files
committed
Move executable compiler to another file
1 parent fa3121c commit 5fb7ac3

File tree

4 files changed

+47
-28
lines changed

4 files changed

+47
-28
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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)

bin/compiler

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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:])

compiler.py

100755100644
Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
#!/usr/bin/env python3
2-
31
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
92

103
URL = 'https://closure-compiler.appspot.com/compile'
114
COMPILATION_LEVEL = 'SIMPLE_OPTIMIZATIONS'
@@ -17,6 +10,9 @@ class ParseError(Exception):
1710
def __init__(self, errors):
1811
self.errors = errors
1912

13+
def __str__(self):
14+
return str(self.errors)
15+
2016

2117
def minify(text):
2218
data = dict(
@@ -28,27 +24,9 @@ def minify(text):
2824
response = requests.post(URL, data).json()
2925
errors = response.get('errors')
3026
if errors:
31-
raise ParseError(errors=errors)
27+
raise ParseError(errors)
3228

3329
data['output_info'] = 'compiled_code'
3430
response = requests.post(URL, data).json()
3531

3632
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:])

filters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from compressor.filters import CallbackOutputFilter
22

33

4-
class GoogleClosureCompileFilter(CallbackOutputFilter):
4+
class ClosureAPICompileFilter(CallbackOutputFilter):
55
dependencies = ['requests']
6-
callback = 'google_closure_online_compile_filter.compiler.minify'
6+
callback = 'compiler.minify'

0 commit comments

Comments
 (0)