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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use jsUglify and have source maps! #9666
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ else | |
fi | ||
|
||
if [ "${BOWER:-no}" = "yes" ]; then | ||
npm install -g uglify-js | ||
npm install -g bower | ||
bower install | ||
fi |
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,99 @@ | ||
import os | ||
import subprocess | ||
|
||
from compressor.exceptions import FilterError | ||
from compressor.filters import CompilerFilter | ||
from compressor.js import JsCompressor | ||
from compressor.utils.stringformat import FormattableString as fstr | ||
from django.conf import settings | ||
from django.utils.safestring import mark_safe | ||
|
||
|
||
# For use with node.js' uglifyjs minifier | ||
# Code taken from: https://roverdotcom.github.io/blog/2014/05/28/javascript-error-reporting-with-source-maps-in-django/ | ||
class UglifySourcemapFilter(CompilerFilter): | ||
command = ( | ||
"uglifyjs {infiles} -o {outfile} --source-map {mapfile}" | ||
" --source-map-url {mapurl} --source-map-root {maproot} -c -m") | ||
|
||
def input(self, **kwargs): | ||
return self.content | ||
|
||
def output(self, **kwargs): | ||
options = dict(self.options) | ||
options['outfile'] = kwargs['outfile'] | ||
|
||
infiles = [] | ||
for infile in kwargs['content_meta']: | ||
# type, full_filename, relative_filename | ||
infiles.append(infile[2]) | ||
|
||
options['infiles'] = ' '.join(f for f in infiles) | ||
|
||
options['mapfile'] = kwargs['outfile'].replace('.js', '.map.js') | ||
|
||
options['mapurl'] = '{}{}'.format( | ||
settings.STATIC_URL, options['mapfile'] | ||
) | ||
|
||
options['maproot'] = settings.STATIC_URL | ||
|
||
self.cwd = kwargs['root_location'] | ||
|
||
try: | ||
command = fstr(self.command).format(**options) | ||
|
||
proc = subprocess.Popen( | ||
command, shell=True, cwd=self.cwd, stdout=self.stdout, | ||
stdin=self.stdin, stderr=self.stderr) | ||
err = proc.communicate() | ||
except (IOError, OSError), e: | ||
raise FilterError('Unable to apply %s (%r): %s' % | ||
(self.__class__.__name__, self.command, e)) | ||
else: | ||
# If the process doesn't return a 0 success code, throw an error | ||
if proc.wait() != 0: | ||
if not err: | ||
err = ('Unable to apply %s (%s)' % | ||
(self.__class__.__name__, self.command)) | ||
raise FilterError(err) | ||
if self.verbose: | ||
self.logger.debug(err) | ||
|
||
|
||
class JsUglifySourcemapCompressor(JsCompressor): | ||
|
||
def output(self, mode='file', forced=False): | ||
content = self.filter_input(forced) | ||
if not content: | ||
return '' | ||
|
||
concatenated_content = '\n'.join( | ||
c.encode(self.charset) for c in content) | ||
|
||
if settings.COMPRESS_ENABLED or forced: | ||
js_compress_dir = os.path.join( | ||
settings.STATIC_ROOT, self.output_dir, self.output_prefix | ||
) | ||
if not os.path.exists(js_compress_dir): | ||
os.makedirs(js_compress_dir, 0775) | ||
filepath = self.get_filepath(concatenated_content, basename=None) | ||
|
||
# UglifySourcemapFilter writes the file directly, as it needs to | ||
# output the sourcemap as well | ||
UglifySourcemapFilter(content).output( | ||
outfile=filepath, | ||
content_meta=self.split_content, | ||
root_location=self.storage.base_location) | ||
|
||
return self.output_file(mode, filepath) | ||
else: | ||
return concatenated_content | ||
|
||
def output_file(self, mode, new_filepath): | ||
""" | ||
The output method that saves the content to a file and renders | ||
the appropriate template with the file's URL. | ||
""" | ||
url = mark_safe(self.storage.url(new_filepath)) | ||
return self.render_output(mode, {"url": url}) |
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
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.
E501 line too long (119 > 115 characters)