Skip to content

Commit

Permalink
Update javascript minifying script
Browse files Browse the repository at this point in the history
- When both minifying modules are missing, creating a copy of the debug
  file is not actually desirable, a missing file is more obvious than a copy.
  WebUI can handle a missing 'normal' script and fallback to 'debug' script so
  modified script to skip and warn instead.
  • Loading branch information
cas-- committed Oct 16, 2018
1 parent ee354eb commit 80178f7
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions minify_web_js.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,45 @@
import os
import subprocess
import sys
from distutils.spawn import find_executable

closure_cmd = None
for cmd in ['closure-compiler', 'closure']:
if find_executable(cmd):
closure_cmd = cmd
break

def module_exists(module_name):

def minify_closure(file_in, file_out):
try:
__import__(module_name)
except ImportError:
return False
else:
subprocess.check_call(
[
closure_cmd,
'--warning_level',
'QUIET',
'--language_in=ECMASCRIPT5',
'--js',
file_in,
'--js_output_file',
file_out,
]
)
return True
except subprocess.CalledProcessError:
return False


# Imports sorted by resulting file size.
if module_exists('closure'):

def minify_closure(file_in, file_out):
try:
subprocess.check_call(
[
'closure',
'-W',
'QUIET',
'--js',
file_in,
'--js_output_file',
file_out,
]
)
return True
except subprocess.CalledProcessError:
return False


elif module_exists('slimit'):
from slimit import minify
else:
print('WARNING: Unable to minify js files. They will be copied as is.')

def minify(text):
return text
# Closure outputs smallest files but it is a java-based command, so have slimit
# as a python-only fallback.
#
# deluge-all.js: Closure 127K, Slimit: 143K, JSMin: 162K
#
if not closure_cmd:
try:
from slimit import minify as minify
except ImportError:
print('Warning: No minifying command found.')
minify = None


def source_files_list(source_dir):
Expand Down Expand Up @@ -95,9 +95,9 @@ def concat_src_files(file_list, fileout_path):


def minify_file(file_debug, file_minified):
try:
if closure_cmd:
return minify_closure(file_debug, file_minified)
except NameError:
elif minify:
with open(file_minified, 'w') as file_out:
with open(file_debug, 'r') as file_in:
file_out.write(minify(file_in.read()))
Expand All @@ -118,7 +118,7 @@ def minify_js_dir(source_dir):
concat_src_files(source_files, file_debug_js)
print('Minifying %s' % source_dir)
if not minify_file(file_debug_js, file_minified_js):
print('Error minifying %s' % source_dir)
print('Warning: Failed minifying files %s, debug only' % source_dir)


if __name__ == '__main__':
Expand Down

0 comments on commit 80178f7

Please sign in to comment.