[Pkg-mozext-commits] [jpm.sh] 27/43: Implemented not-so-smart code minifier

Hema Prathaban hemaprathaban-guest at moszumanska.debian.org
Thu Aug 17 07:52:35 UTC 2017


This is an automated email from the git hooks/post-receive script.

hemaprathaban-guest pushed a commit to branch master
in repository jpm.sh.

commit 9e6c2ca7fdee7619dd83ade416df54be6cdeb72e
Author: nord-stream <nord-stream at ochaken.jp.eu.org>
Date:   Tue Mar 15 13:40:29 2016 +0000

    Implemented not-so-smart code minifier
---
 lib/code-minifier.sh | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/xpi.sh           |  4 ++++
 2 files changed, 63 insertions(+)

diff --git a/lib/code-minifier.sh b/lib/code-minifier.sh
new file mode 100644
index 0000000..0b969b6
--- /dev/null
+++ b/lib/code-minifier.sh
@@ -0,0 +1,59 @@
+# -*- sh; tab-width: 4; coding: utf-8 -*-
+# vim: ts=4 noet ai ft=sh
+
+# Minify various code
+# This file is part of JPM.sh.
+# Copyright (C) 2016 the Desktopd developers
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+
+# jpmMinifyHtml <dir>
+jpmMinifyHtml () {
+	aDir=$1
+	jpmConsoleLog "Minifying HTML files..."
+	
+	# Remove empty lines
+	html_egrep_v='^\s*$|vim:'
+	find "$aDir" -type f -name '*.html' -execdir \
+		sh -c 'egrep -v "$2" "$1" > "$1.tmp" && mv -f "$1.tmp" "$1"' \
+			-- {} "$html_egrep_v" \;
+}
+
+# jpmMinifyCss <dir>
+jpmMinifyCss () {
+	aDir=$1
+	# CSS minifier is aggressive.
+	jpmConsoleLog "Minifying CSS files..."
+	css_egrep_v='^\s*$|vim:'
+	css_sed='s/\s\{1,\}/ /g ; s/\s*\([{:>+,;]\)\s*/\1/g ; s/;}/}/g'
+	find "$aDir" -type f -name '*.css' -execdir \
+		sh -c \
+		'egrep -v "$2" "$1" | tr -d "\t\n" | sed "$3" > "$1.tmp" && mv -f "$1.tmp" "$1"' \
+			-- {} "$css_egrep_v" "$css_sed" \;
+}
+
+# jpmMinifyJs <dir>
+jpmMinifyJs () {
+	aDir=$1
+	# This new JS minifier tries to preserve line numbers.
+	# It just removes some spaces and comments.
+	jpmConsoleLog "Minifying JS files..."
+	js_sed='s/^\s*$//'
+	find "$aDir" -type f -name '*.js' -execdir \
+		sh -c \
+		'sed "$1" < "$2" > "$2.tmp" && mv -f "$2.tmp" "$2"' \
+			-- "$js_sed" {} \;
+}
+
diff --git a/lib/xpi.sh b/lib/xpi.sh
index 029f2ea..cd05b9f 100644
--- a/lib/xpi.sh
+++ b/lib/xpi.sh
@@ -25,6 +25,7 @@
 . "$gLibDir/xml.sh"
 . "$gLibDir/crypto.sh"
 . "$gLibDir/json.sh"
+. "$gLibDir/code-minifier.sh"
 
 # jpmXpiGetId
 jpmXpiGetId () {
@@ -98,6 +99,9 @@ jpmXpiBuild () (
 				-- "$preprocess_sed" {} \;
 		
 		# TODO: implement optional minification here
+		[ "$addon_minify_html" ] && jpmMinifyHtml "$buildDir"
+		[ "$addon_minify_css" ] && jpmMinifyCss "$buildDir"
+		[ "$addon_minify_js" ] && jpmMinifyJs "$buildDir"
 	:;} || jpmConsoleLog "xpi-build: Debugging build enabled."
 	
 	addon_preferences_json=''

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/jpm.sh.git



More information about the Pkg-mozext-commits mailing list