-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjs.py
74 lines (51 loc) · 2.13 KB
/
js.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
import re
from subprocess import call
from glsl import compress_glsl
from os import path
def inline_modules(source, directory, imported = []):
""" Inline modules
:param source: The source Javascript string
:param directory: The root directory
:param imported: All files inlined so far
:return: The source with all modules recursively inlined
"""
def include(match):
head, tail = path.split(match[1])
if tail in imported:
return ""
imported.append(tail)
contents = ""
with open(path.join(directory, head, tail), 'r', encoding="utf8") as file:
contents = inline_modules(file.read(), path.join(directory, head), imported)
file.close()
return contents
return re.sub('import {.*} from "(.*)";', lambda match: include(match), source.replace("export ", ""))
def compress_js(directory, source, css_variables, advanced_cc):
""" Compress Javascript content
:param directory: The root directory
:param source: The source Javascript string
:param css_variables: A CSS variables object to shorten css variable names
:param advanced_cc: True if advanced optimizations should be used
:return: The compressed source
"""
with open('tmp-in', 'w', encoding="utf8") as file:
file.write(inline_modules(source, directory))
file.close()
call('npx google-closure-compiler\
--compilation_level=' + ('ADVANCED_OPTIMIZATIONS' if advanced_cc else 'SIMPLE_OPTIMIZATIONS') + '\
--language_out=ECMASCRIPT5\
--warning_level=QUIET\
--js=tmp-in\
--js_output_file=tmp-out', shell=True)
os.remove('tmp-in')
with open('tmp-out', 'r', encoding="utf8") as file:
contents = file.read().replace('\n', '')
file.close()
os.remove('tmp-out')
contents = re.sub('(?!"")#version 100[^"]*(?=")', lambda match: compress_glsl(match[0]), contents)
def found(match):
if match[1] in css_variables:
return match[0].replace(match[1], css_variables[match[1]])
return match[0]
return re.sub(r'"(--[a-zA-Z-]*)"', found, contents)