[WebUI] Improve the minify script
This commit is contained in:
parent
24b71a400f
commit
d7029dcfc6
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
|
@ -9,7 +9,7 @@
|
||||||
# See LICENSE for more details.
|
# See LICENSE for more details.
|
||||||
#
|
#
|
||||||
|
|
||||||
"""Minifies the Webui JS files.
|
"""Minifies the WebUI JS files.
|
||||||
|
|
||||||
Usage: python minify_web_js.py deluge/ui/web/js/deluge-all
|
Usage: python minify_web_js.py deluge/ui/web/js/deluge-all
|
||||||
|
|
||||||
|
@ -20,18 +20,31 @@ import fnmatch
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def module_exists(module_name):
|
||||||
|
try:
|
||||||
|
__import__(module_name)
|
||||||
|
except ImportError:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
|
# slimit creates smallest file size
|
||||||
|
if module_exists('slimit'):
|
||||||
from slimit import minify
|
from slimit import minify
|
||||||
|
elif module_exists('jsmin'):
|
||||||
|
from jsmin import jsmin as minify
|
||||||
|
elif module_exists('rjsmin'):
|
||||||
|
from rjsmin import jsmin as minify
|
||||||
|
else:
|
||||||
|
raise ImportError('Minifying WebUI JS requires slimit, jsmin or rjsmin')
|
||||||
|
|
||||||
if len(sys.argv) != 2:
|
|
||||||
print 'Specify a source js directory, e.g. deluge/ui/web/js/deluge-all'
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
SOURCE_DIR = os.path.abspath(sys.argv[1])
|
def source_files_list(source_dir):
|
||||||
BUILD_NAME = os.path.basename(SOURCE_DIR)
|
|
||||||
BUILD_DIR = os.path.dirname(SOURCE_DIR)
|
|
||||||
SRC_FILE_LIST = []
|
|
||||||
|
|
||||||
for root, dirnames, filenames in os.walk(SOURCE_DIR):
|
src_file_list = []
|
||||||
|
|
||||||
|
for root, dirnames, filenames in os.walk(source_dir):
|
||||||
dirnames.sort(reverse=True)
|
dirnames.sort(reverse=True)
|
||||||
filenames_js = fnmatch.filter(filenames, '*.js')
|
filenames_js = fnmatch.filter(filenames, '*.js')
|
||||||
filenames_js.sort()
|
filenames_js.sort()
|
||||||
|
@ -51,24 +64,46 @@ for root, dirnames, filenames in os.walk(SOURCE_DIR):
|
||||||
# Ensure root directory files are bottom of list.
|
# Ensure root directory files are bottom of list.
|
||||||
if dirnames:
|
if dirnames:
|
||||||
for filename in filenames_js:
|
for filename in filenames_js:
|
||||||
SRC_FILE_LIST.append(os.path.join(root, filename))
|
src_file_list.append(os.path.join(root, filename))
|
||||||
else:
|
else:
|
||||||
for filename in reversed(filenames_js):
|
for filename in reversed(filenames_js):
|
||||||
SRC_FILE_LIST.insert(0, os.path.join(root, filename))
|
src_file_list.insert(0, os.path.join(root, filename))
|
||||||
|
|
||||||
if not SRC_FILE_LIST:
|
return src_file_list
|
||||||
print 'No js files found'
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
print 'Minifying %s ...' % BUILD_NAME
|
|
||||||
|
|
||||||
# Create the unminified debug file.
|
def concat_src_files(file_list, fileout_path):
|
||||||
file_debug_js = os.path.join(BUILD_DIR, BUILD_NAME + '-debug.js')
|
with open(fileout_path, 'w') as file_out:
|
||||||
with open(file_debug_js, 'w') as _file:
|
file_in = fileinput.input(file_list)
|
||||||
input_lines = fileinput.input(SRC_FILE_LIST)
|
file_out.writelines(file_in)
|
||||||
_file.writelines(input_lines)
|
|
||||||
|
|
||||||
file_minified_js = os.path.join(BUILD_DIR, BUILD_NAME + '.js')
|
|
||||||
with open(file_minified_js, 'w') as file_out:
|
def minify_file(file_debug, file_minified):
|
||||||
with open(file_debug_js, 'r') as file_in:
|
with open(file_minified, 'w') as file_out:
|
||||||
|
with open(file_debug, 'r') as file_in:
|
||||||
file_out.write(minify(file_in.read()))
|
file_out.write(minify(file_in.read()))
|
||||||
|
|
||||||
|
|
||||||
|
def minify_js_dir(source_dir):
|
||||||
|
build_name = os.path.basename(source_dir)
|
||||||
|
build_dir = os.path.dirname(source_dir)
|
||||||
|
file_debug_js = os.path.join(build_dir, build_name + '-debug.js')
|
||||||
|
file_minified_js = os.path.join(build_dir, build_name + '.js')
|
||||||
|
source_files = source_files_list(source_dir)
|
||||||
|
|
||||||
|
if not source_files:
|
||||||
|
print 'No js files found, skipping %s' % source_dir
|
||||||
|
return
|
||||||
|
|
||||||
|
concat_src_files(source_files, file_debug_js)
|
||||||
|
minify_file(file_debug_js, file_minified_js)
|
||||||
|
print 'Minified %s' % source_dir
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
if len(sys.argv) != 2:
|
||||||
|
JS_SOURCE_DIRS = ['deluge/ui/web/js/deluge-all', 'deluge/ui/web/js/extjs/ext-extensions']
|
||||||
|
else:
|
||||||
|
JS_SOURCE_DIRS = [os.path.abspath(sys.argv[1])]
|
||||||
|
|
||||||
|
for source_dir in JS_SOURCE_DIRS:
|
||||||
|
minify_js_dir(source_dir)
|
||||||
|
|
Loading…
Reference in New Issue