-
-
Notifications
You must be signed in to change notification settings - Fork 371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add sourcemap support #490
Open
fdintino
wants to merge
3
commits into
jazzband:master
Choose a base branch
from
theatlantic:sourcemaps
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is now obsolete.