-
-
Notifications
You must be signed in to change notification settings - Fork 370
/
Copy pathclosure.py
56 lines (41 loc) · 1.79 KB
/
closure.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
from __future__ import unicode_literals
import os
import tempfile
from django.contrib.staticfiles.storage import staticfiles_storage
from pipeline.conf import settings
from pipeline.compressors import SubProcessCompressor
from pipeline.utils import source_map_re
class ClosureCompressor(SubProcessCompressor):
def compress_js(self, js):
command = (settings.CLOSURE_BINARY, settings.CLOSURE_ARGUMENTS)
return self.execute_command(command, js)
def compress_js_with_source_map(self, paths):
args = [settings.CLOSURE_BINARY, settings.CLOSURE_ARGUMENTS]
location_maps = set([])
abs_paths = []
for path in paths:
abs_path = staticfiles_storage.path(path)
location_maps.add("%s|%s" % (
os.path.dirname(abs_path),
staticfiles_storage.url(os.path.dirname(path))))
abs_paths.append(abs_path)
with open(abs_path) as f:
content = f.read()
matches = source_map_re.search(content)
if matches:
input_source_map = filter(None, matches.groups())[0]
input_source_map_file = os.path.join(os.path.dirname(abs_path), input_source_map)
args += [
'--source_map_input',
"%s|%s" % (abs_path, input_source_map_file)]
for location_map in location_maps:
args += ['--source_map_location_mapping', location_map]
temp_file = tempfile.NamedTemporaryFile()
args += ["--create_source_map", temp_file.name]
for path in abs_paths:
args += ["--js", path]
js = self.execute_command(args, None)
with open(temp_file.name) as f:
source_map = f.read()
temp_file.close()
return js, source_map