-
-
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.
Add sourcmaps support for uglifyjs and cleancss
- Loading branch information
Showing
6 changed files
with
163 additions
and
23 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
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, indent="\t") | ||
|
||
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,12 +1,45 @@ | ||
from __future__ import unicode_literals | ||
|
||
import codecs | ||
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, path_depth | ||
|
||
|
||
class UglifyJSCompressor(SubProcessCompressor): | ||
|
||
def compress_js(self, js): | ||
command = (settings.UGLIFYJS_BINARY, settings.UGLIFYJS_ARGUMENTS) | ||
command = [settings.UGLIFYJS_BINARY, settings.UGLIFYJS_ARGUMENTS] | ||
if self.verbose: | ||
command += ' --verbose' | ||
command.append(' --verbose') | ||
return self.execute_command(command, js) | ||
|
||
def compress_js_with_source_map(self, paths): | ||
source_map_file = tempfile.NamedTemporaryFile() | ||
|
||
args = [settings.UGLIFYJS_BINARY] | ||
args += [staticfiles_storage.path(p) for p in paths] | ||
args += ["--source-map", source_map_file.name] | ||
args += ["--source-map-root", staticfiles_storage.base_url] | ||
args += ["--prefix", "%s" % path_depth(staticfiles_storage.base_location)] | ||
|
||
args += settings.UGLIFYJS_ARGUMENTS | ||
|
||
if self.verbose: | ||
args.append('--verbose') | ||
|
||
js = self.execute_command(args) | ||
|
||
with codecs.open(source_map_file.name, encoding='utf-8') as f: | ||
source_map = f.read() | ||
|
||
source_map_file.close() | ||
|
||
# Strip out existing source map comment (it will be re-added with packaging) | ||
js = source_map_re.sub('', js) | ||
|
||
return js, 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
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