We want to store third-party JavaScript libraries in their
non-minimized (source) form in the repository, but when users
are browsing
libvirt.org we'd rather only transmit the more
compact minified variant.
Call uglifyjs at build time, if available, to achieve this.
Signed-off-by: Andrea Bolognani <abologna(a)redhat.com>
---
.gitignore | 1 +
docs/Makefile.am | 30 +++++++++++++++++++++++++++---
docs/index.html.in | 6 +++---
docs/page.xsl | 2 +-
docs/site.xsl | 1 +
docs/subsite.xsl | 1 +
m4/virt-external-programs.m4 | 2 ++
7 files changed, 36 insertions(+), 7 deletions(-)
diff --git a/.gitignore b/.gitignore
index 727bfdb6ec..522ac762b6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -61,6 +61,7 @@
/docs/apibuild.py.stamp
/docs/devhelp/libvirt.devhelp
/docs/hvsupport.html.in
+/docs/js/*.min.js
/docs/libvirt-admin-*.xml
/docs/libvirt-api.xml
/docs/libvirt-lxc-*.xml
diff --git a/docs/Makefile.am b/docs/Makefile.am
index 9b79fc60c1..e787a7c47b 100644
--- a/docs/Makefile.am
+++ b/docs/Makefile.am
@@ -240,6 +240,7 @@ news.html.in: \
$(srcdir)/news.xml \
$(srcdir)/news-html.xsl
$(AM_V_GEN)$(XSLTPROC) --nonet \
+ --stringparam js "$(JS)" \
$(srcdir)/news-html.xsl \
$(srcdir)/news.xml \
$@-tmp \
@@ -258,7 +259,7 @@ MAINTAINERCLEANFILES += \
convert -rotate 90 $< $@
%.html.tmp: %.html.in site.xsl subsite.xsl page.xsl \
- $(acl_generated)
+ $(acl_generated) $(minified_javascript)
$(AM_V_GEN)name=`echo $@ | sed -e 's/.tmp//'`; \
dir=`dirname $@` ; \
if test "$$dir" = "."; \
@@ -270,24 +271,47 @@ MAINTAINERCLEANFILES += \
fi; \
$(XSLTPROC) --stringparam pagename $$name \
--stringparam timestamp $(timestamp) --nonet \
+ --stringparam js "$(JS)" \
$(top_srcdir)/docs/$$style $< > $@ \
+ || { rm $@ && exit 1; }; \
+ sed -i 's/@JS@/$(JS)/g' "$@" \
|| { rm $@ && exit 1; }
%.html: %.html.tmp
$(AM_V_GEN)$(XMLLINT) --nonet --format $< > $(srcdir)/$@ \
|| { rm $(srcdir)/$@ && exit 1; }
+
+if HAVE_UGLIFYJS
+JS = min.js
+minified_javascript = $(javascript:%.js=%.min.js)
+EXTRA_DIST += $(minified_javascript)
+MAINTAINERCLEANFILES += $(minified_javascript)
+
+%.min.js: %.js
+ $(AM_V_GEN)$(UGLIFYJS) -o $@ $<
+else ! HAVE_UGLIFYJS
+JS = js
+minified_javascript = $(javascript)
+
+dist-hook:
+ echo "uglifyjs is required to make dist." >&2
+ exit 1
+endif ! HAVE_UGLIFYJS
+
$(apihtml_generated): html/index.html
html/index.html: libvirt-api.xml newapi.xsl page.xsl $(APIBUILD_STAMP)
$(AM_V_GEN)$(XSLTPROC) --nonet -o $(srcdir)/ \
--stringparam builddir '$(abs_top_builddir)' \
--stringparam timestamp $(timestamp) \
+ --stringparam js "$(JS)" \
$(srcdir)/newapi.xsl $(srcdir)/libvirt-api.xml && \
$(XMLLINT) --nonet --noout $(srcdir)/html/*.html
$(addprefix $(srcdir)/,$(devhelphtml)): $(srcdir)/libvirt-api.xml $(devhelpxsl)
$(AM_V_GEN)$(XSLTPROC) --stringparam timestamp $(timestamp) \
+ --stringparam js "$(JS)" \
--nonet -o $(srcdir)/devhelp/ \
$(top_srcdir)/docs/devhelp/devhelp.xsl $(srcdir)/libvirt-api.xml
@@ -372,7 +396,7 @@ install-data-local:
for f in $(css) $(dot_html) $(gif) $(png); do \
$(INSTALL) -m 0644 $(srcdir)/$$f $(DESTDIR)$(HTML_DIR); done
$(mkinstalldirs) $(DESTDIR)$(HTML_DIR)/js
- for f in $(javascript); do \
+ for f in $(minified_javascript); do \
$(INSTALL) -m 0644 $(srcdir)/$$f $(DESTDIR)$(HTML_DIR)/js/; done
$(mkinstalldirs) $(DESTDIR)$(HTML_DIR)/logos
for f in $(logofiles); do \
@@ -401,7 +425,7 @@ uninstall-local:
for f in $(logofiles); do \
rm -f $(DESTDIR)$(HTML_DIR)/$$f; \
done
- for f in $(javascript); do \
+ for f in $(minified_javascript); do \
rm -f $(DESTDIR)$(HTML_DIR)/$$f; \
done
for h in $(apihtml); do rm -f $(DESTDIR)$(HTML_DIR)/$$h; done
diff --git a/docs/index.html.in b/docs/index.html.in
index cab387ebb6..a912d7282a 100644
--- a/docs/index.html.in
+++ b/docs/index.html.in
@@ -2,9 +2,9 @@
<!DOCTYPE html>
<html
xmlns="http://www.w3.org/1999/xhtml">
<head>
- <script type="text/javascript" src="js/jquery-3.4.1.js">
</script>
- <script type="text/javascript" src="js/moment-2.24.0.js">
</script>
- <script type="text/javascript"
src="js/jquery.rss-3.3.0.js"> </script>
+ <script type="text/javascript" src="js/jquery-3.4.1.@JS@">
</script>
+ <script type="text/javascript" src="js/moment-2.24.0.@JS@">
</script>
+ <script type="text/javascript"
src="js/jquery.rss-3.3.0.@JS@"> </script>
<script type="text/javascript">
<!--
diff --git a/docs/page.xsl b/docs/page.xsl
index 65ddeb2bb7..bf78103df3 100644
--- a/docs/page.xsl
+++ b/docs/page.xsl
@@ -99,7 +99,7 @@
<meta name="description" content="libvirt, virtualization,
virtualization API"/>
<xsl:apply-templates select="/html:html/html:head/*"
mode="content"/>
- <script type="text/javascript"
src="{$href_base}js/main.js">
+ <script type="text/javascript"
src="{$href_base}js/main.{$js}">
<xsl:comment>// forces non-empty element</xsl:comment>
</script>
</head>
diff --git a/docs/site.xsl b/docs/site.xsl
index c9d41f2c93..fb9c503993 100644
--- a/docs/site.xsl
+++ b/docs/site.xsl
@@ -28,6 +28,7 @@
<xsl:apply-templates select="." mode="page">
<xsl:with-param name="pagename" select="$pagename"/>
<xsl:with-param name="timestamp" select="$timestamp"/>
+ <xsl:with-param name="js" select="$js"/>
</xsl:apply-templates>
</xsl:template>
diff --git a/docs/subsite.xsl b/docs/subsite.xsl
index f56ac0491c..c939ed6588 100644
--- a/docs/subsite.xsl
+++ b/docs/subsite.xsl
@@ -18,6 +18,7 @@
<xsl:apply-templates select="." mode="page">
<xsl:with-param name="pagename" select="$pagename"/>
<xsl:with-param name="timestamp" select="$timestamp"/>
+ <xsl:with-param name="js" select="$js"/>
</xsl:apply-templates>
</xsl:template>
diff --git a/m4/virt-external-programs.m4 b/m4/virt-external-programs.m4
index 0f995998c3..371acb972c 100644
--- a/m4/virt-external-programs.m4
+++ b/m4/virt-external-programs.m4
@@ -34,6 +34,8 @@ AC_DEFUN([LIBVIRT_CHECK_EXTERNAL_PROGRAMS], [
AC_MSG_ERROR("xsltproc is required to build libvirt")
fi
AC_PATH_PROG([AUGPARSE], [augparse], [/usr/bin/augparse])
+ AC_PATH_PROG([UGLIFYJS], [uglifyjs], [])
+ AM_CONDITIONAL([HAVE_UGLIFYJS], [test "$UGLIFYJS"])
AC_PROG_MKDIR_P
AC_PROG_LN_S
--
2.21.0