Replace YUI with custom CSS minifier

This commit is contained in:
chylex 2020-04-18 19:23:55 +02:00
parent 3ee8d6514d
commit 016adadd3e
3 changed files with 45 additions and 22 deletions

View File

@ -25,8 +25,6 @@ Now you can modify the source code:
After you've done changes to the source code, you will need to build it. Before that, download and install:
* (**required**) [Python 3](https://www.python.org/downloads)
* Use to run the build script
* (optional) [Java 7+](https://www.java.com/inc/BrowserRedirect1.jsp) / [OpenJDK 7+](http://openjdk.java.net/install/index.html)
* Only required for optional [CSS minification](#Minification)
* (optional) [Node + npm](https://nodejs.org/en) & command line [uglify-es](https://www.npmjs.com/package/uglify-es)
* Not required on Windows
* Only required for optional [JS minification](#Minification) on Linux/Mac
@ -41,11 +39,9 @@ You can tweak the build process using the following flags:
### Minification
The build process automatically minifies the CSS using `YUI Compressor` and JS files using `UglifyJS@3`.
The build process automatically minifies JS using `UglifyJS@3`, and CSS using a custom minifier.
* If the `--nominify` flag is used, minification will be completely disabled
* If Java is not available from the command line, CSS minification will be skipped
* You can check whether you have installed Java correctly by running `java -version` in the command line, and making sure the version is `1.7.xyz` or higher
* If `uglify-es` is not available from the command line, JS minification will be skipped
* When building on Windows 64-bit, the build script will use the included Node runner and packages
* When building on Windows 32-bit, you will need to download [Node 32-bit](https://nodejs.org/en/download) and replace the included one in `lib/`

View File

@ -5,6 +5,7 @@ import glob
import shutil
import sys
import os
import re
import distutils.dir_util
@ -14,20 +15,16 @@ VERSION_FULL = VERSION_SHORT + ", released 18 Apr 2020"
EXEC_UGLIFYJS_WIN = "{2}/lib/uglifyjs.cmd --parse bare_returns --compress --mangle toplevel --mangle-props keep_quoted,reserved=[{3}] --output \"{1}\" \"{0}\""
EXEC_UGLIFYJS_AUTO = "uglifyjs --parse bare_returns --compress --mangle toplevel --mangle-props keep_quoted,reserved=[{3}] --output \"{1}\" \"{0}\""
EXEC_YUI = "java -jar lib/yuicompressor-2.4.8.jar --charset utf-8 --line-break 160 --type css -o \"{1}\" \"{0}\""
USE_UGLIFYJS = "--nominify" not in sys.argv
USE_JAVA = "--nominify" not in sys.argv
USE_MINIFICATION = "--nominify" not in sys.argv
BUILD_WEBSITE = "--website" in sys.argv
CLIPBOARD_TRACKER = "--copytracker" in sys.argv
WORKING_DIR = os.getcwd()
if USE_JAVA and shutil.which("java") is None:
USE_JAVA = False
print("Could not find 'java', CSS minification will be disabled")
# UglifyJS Setup
if os.name == "nt":
EXEC_UGLIFYJS = EXEC_UGLIFYJS_WIN
@ -39,16 +36,51 @@ else:
print("Could not find 'uglifyjs', JS minification will be disabled")
with open("reserve.txt", "r") as reserved:
RESERVED_PROPS = ",".join(line.strip() for line in reserved.readlines())
if USE_UGLIFYJS:
with open("reserve.txt", "r") as reserved:
RESERVED_PROPS = ",".join(line.strip() for line in reserved.readlines())
# File Utilities
def combine_files(input_pattern, output_file):
is_first_file = True
with fileinput.input(sorted(glob.glob(input_pattern))) as stream:
for line in stream:
if stream.isfirstline():
if is_first_file:
is_first_file = False
else:
output_file.write("\n")
output_file.write(line.replace("{{{version:full}}}", VERSION_FULL))
def minify_css(input_file, output_file):
if not USE_MINIFICATION:
if input_file != output_file:
shutil.copyfile(input_file, output_file)
return
with open(input_file, "r") as fin:
css = fin.read()
css = re.sub(r"^\s+(.+?):\s*(.+?)(?:\s*(!important))?;\n", r"\1:\2\3;", css, flags = re.M) # remove spaces after colons
css = re.sub(r"\{\n", r"{", css, flags = re.M) # remove new lines after {
css = re.sub(r"\n\}", r"}", css, flags = re.M) # remove new lines before }
css = re.sub(r"\n\n", r"\n", css, flags = re.M) # remove empty lines
css = re.sub(r";\}$", r"}", css, flags = re.M) # remove last semicolons
css = re.sub(r"rgb\((.*?),\s*(.*?),\s*(.*?)\)", r"rgb(\1,\2,\3)", css, flags = re.M) # remove spaces after commas in rgb()
css = re.sub(r"rgba\((.*?),\s*(.*?),\s*(.*?),\s*(.*?)\)", r"rgba(\1,\2,\3,\4)", css, flags = re.M) # remove spaces after commas in rgba()
with open(output_file, "w") as out:
out.write(css)
# Build System
def build_tracker_html():
output_file_raw = "bld/track.js"
output_file_html = "bld/track.html"
@ -111,11 +143,7 @@ def build_renderer():
with open(tmp_css_file_combined, "w") as out:
combine_files(input_css_pattern, out)
if USE_JAVA:
os.system(EXEC_YUI.format(tmp_css_file_combined, tmp_css_file_minified))
else:
shutil.copyfile(tmp_css_file_combined, tmp_css_file_minified)
minify_css(tmp_css_file_combined, tmp_css_file_minified)
os.remove(tmp_css_file_combined)
input_js_pattern = "src/renderer/*.js"
@ -172,11 +200,11 @@ def build_website():
shutil.copyfile(tracker_file_html, "bld/web/build/track.html")
shutil.copyfile(tracker_file_userscript, "bld/web/build/track.user.js")
shutil.copyfile(viewer_file, "bld/web/build/viewer.html")
if USE_JAVA:
os.system(EXEC_YUI.format(web_style_file, web_style_file))
minify_css(web_style_file, web_style_file)
# Build Process
os.makedirs("bld", exist_ok = True)
print("Building tracker html...")

View File

@ -7,7 +7,6 @@
<script type="text/javascript">
/*{js}*/
</script>
<style type="text/css">
/*{css}*/
</style>