-
-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
320 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,66 @@ | ||
from __future__ import unicode_literals | ||
|
||
import codecs | ||
import json | ||
import os | ||
|
||
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, relurl | ||
|
||
|
||
class CleanCSSCompressor(SubProcessCompressor): | ||
|
||
def compress_css(self, css): | ||
args = [settings.CLEANCSS_BINARY, settings.CLEANCSS_ARGUMENTS] | ||
return self.execute_command(args, css) | ||
|
||
def compress_css_with_source_map(self, paths, output_filename): | ||
output_path = staticfiles_storage.path(output_filename) | ||
output_dir = os.path.dirname(output_path) | ||
if not os.path.exists(output_dir): | ||
os.makedirs(output_dir) | ||
|
||
args = [settings.CLEANCSS_BINARY] | ||
args += ['--source-map'] | ||
if settings.CLEANCSS_ARGUMENTS: | ||
args += [settings.CLEANCSS_ARGUMENTS] | ||
else: | ||
# At present, without these arguments, cleancss does not | ||
# generate accurate source maps | ||
args += [ | ||
'--skip-advanced', '--skip-media-merging', | ||
'--skip-restructuring', '--skip-shorthand-compacting', | ||
'--keep-line-breaks'] | ||
args += ['--output', output_path] | ||
args += [staticfiles_storage.path(p) for p in paths] | ||
|
||
self.execute_command(args, cwd=output_dir) | ||
|
||
source_map_file = "%s.map" % output_path | ||
|
||
with codecs.open(output_path, encoding='utf-8') as f: | ||
css = f.read() | ||
with codecs.open(source_map_file, encoding='utf-8') as f: | ||
source_map = f.read() | ||
|
||
# Strip out existing source map comment (it will be re-added with packaging) | ||
css = source_map_re.sub('', css) | ||
|
||
output_url = "%s/%s" % ( | ||
staticfiles_storage.url(os.path.dirname(output_filename)), | ||
os.path.basename(output_path)) | ||
|
||
# Grab urls from staticfiles storage (in case filenames are hashed) | ||
source_map_data = json.loads(source_map) | ||
for i, source in enumerate(source_map_data['sources']): | ||
source_abs_path = os.path.join(output_dir, source) | ||
source_rel_path = os.path.relpath( | ||
source_abs_path, staticfiles_storage.base_location) | ||
source_url = staticfiles_storage.url(source_rel_path) | ||
source_map_data['sources'][i] = relurl(source_url, output_url) | ||
source_map = json.dumps(source_map_data) | ||
|
||
return css, source_map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,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 |
Oops, something went wrong.