Update javascript minifying script

- 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.
This commit is contained in:
Calum Lind 2018-10-16 11:30:06 +01:00
commit 80178f7310

View file

@ -22,45 +22,45 @@ import fnmatch
import os import os
import subprocess import subprocess
import sys 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: try:
__import__(module_name) subprocess.check_call(
except ImportError: [
return False closure_cmd,
else: '--warning_level',
'QUIET',
'--language_in=ECMASCRIPT5',
'--js',
file_in,
'--js_output_file',
file_out,
]
)
return True return True
except subprocess.CalledProcessError:
return False
# Imports sorted by resulting file size. # Closure outputs smallest files but it is a java-based command, so have slimit
if module_exists('closure'): # as a python-only fallback.
#
def minify_closure(file_in, file_out): # deluge-all.js: Closure 127K, Slimit: 143K, JSMin: 162K
try: #
subprocess.check_call( if not closure_cmd:
[ try:
'closure', from slimit import minify as minify
'-W', except ImportError:
'QUIET', print('Warning: No minifying command found.')
'--js', minify = None
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
def source_files_list(source_dir): def source_files_list(source_dir):
@ -95,9 +95,9 @@ def concat_src_files(file_list, fileout_path):
def minify_file(file_debug, file_minified): def minify_file(file_debug, file_minified):
try: if closure_cmd:
return minify_closure(file_debug, file_minified) return minify_closure(file_debug, file_minified)
except NameError: elif minify:
with open(file_minified, 'w') as file_out: with open(file_minified, 'w') as file_out:
with open(file_debug, 'r') as file_in: with open(file_debug, 'r') as file_in:
file_out.write(minify(file_in.read())) file_out.write(minify(file_in.read()))
@ -118,7 +118,7 @@ def minify_js_dir(source_dir):
concat_src_files(source_files, file_debug_js) concat_src_files(source_files, file_debug_js)
print('Minifying %s' % source_dir) print('Minifying %s' % source_dir)
if not minify_file(file_debug_js, file_minified_js): 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__': if __name__ == '__main__':