Skip to content

Commit fa3121c

Browse files
committed
Init project
0 parents  commit fa3121c

File tree

6 files changed

+235
-0
lines changed

6 files changed

+235
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.idea/

Pipfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[[source]]
2+
name = "pypi"
3+
url = "https://pypi.org/simple"
4+
verify_ssl = true
5+
6+
[dev-packages]
7+
8+
[packages]
9+
django-compressor = "*"
10+
requests = "*"
11+
12+
[requires]
13+
python_version = "3.8"

Pipfile.lock

Lines changed: 131 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python3
2+
3+
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+
10+
URL = 'https://closure-compiler.appspot.com/compile'
11+
COMPILATION_LEVEL = 'SIMPLE_OPTIMIZATIONS'
12+
"""WHITESPACE_ONLY, SIMPLE_OPTIMIZATIONS, ADVANCED_OPTIMIZATIONS"""
13+
OUTPUT_FORMAT = 'json'
14+
15+
16+
class ParseError(Exception):
17+
def __init__(self, errors):
18+
self.errors = errors
19+
20+
21+
def minify(text):
22+
data = dict(
23+
js_code=text,
24+
compilation_level=COMPILATION_LEVEL,
25+
output_format=OUTPUT_FORMAT,
26+
output_info='errors'
27+
)
28+
response = requests.post(URL, data).json()
29+
errors = response.get('errors')
30+
if errors:
31+
raise ParseError(errors=errors)
32+
33+
data['output_info'] = 'compiled_code'
34+
response = requests.post(URL, data).json()
35+
36+
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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from compressor.filters import CallbackOutputFilter
2+
3+
4+
class GoogleClosureCompileFilter(CallbackOutputFilter):
5+
dependencies = ['requests']
6+
callback = 'google_closure_online_compile_filter.compiler.minify'

tests.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import unittest
2+
3+
import compiler
4+
5+
ERROR_JS_CODE = '''
6+
fun hello() {
7+
conso.log()
8+
}
9+
'''
10+
11+
GOOD_JS_CODE = '''
12+
function hello() {
13+
console.log('hello')
14+
}
15+
'''
16+
17+
18+
class TestCompiler(unittest.TestCase):
19+
20+
def test_minify(self):
21+
with self.assertRaises(compiler.ParseError) as cm:
22+
compiler.minify(ERROR_JS_CODE)
23+
24+
errors = cm.exception.errors
25+
self.assertTrue(len(errors), 1)
26+
self.assertTrue(errors[0]['type'], 'JSC_PARSE_ERROR')
27+
28+
def test_minify_good(self):
29+
minify_code = compiler.minify(GOOD_JS_CODE)
30+
self.assertTrue(minify_code, 'function hello(){console.log()};')

0 commit comments

Comments
 (0)