Skip to content

Commit 90c9bda

Browse files
ahornaceVladimir Kotal
authored andcommitted
Minify js and css files (#2891)
1 parent 6c2a95b commit 90c9bda

File tree

6 files changed

+97
-34
lines changed

6 files changed

+97
-34
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/web/PageConfig.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public final class PageConfig {
110110
protected static final String ALL_PROJECT_SEARCH = "searchall";
111111
protected static final String PROJECT_PARAM_NAME = "project";
112112
protected static final String GROUP_PARAM_NAME = "group";
113+
private static final String DEBUG_PARAM_NAME = "debug";
113114

114115
// TODO if still used, get it from the app context
115116

@@ -1401,13 +1402,17 @@ public String getUriEncodedPath() {
14011402
* @param name name of the script to search for
14021403
* @return this
14031404
*
1404-
* @see Scripts#addScript(java.lang.String, java.lang.String)
1405+
* @see Scripts#addScript(String, String, Scripts.Type)
14051406
*/
14061407
public PageConfig addScript(String name) {
1407-
this.scripts.addScript(this.req.getContextPath(), name);
1408+
this.scripts.addScript(this.req.getContextPath(), name, isDebug() ? Scripts.Type.DEBUG : Scripts.Type.MINIFIED);
14081409
return this;
14091410
}
14101411

1412+
private boolean isDebug() {
1413+
return Boolean.parseBoolean(req.getParameter(DEBUG_PARAM_NAME));
1414+
}
1415+
14111416
/**
14121417
* Return the page scripts.
14131418
*

opengrok-indexer/src/main/java/org/opengrok/indexer/web/Scripts.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
*/
3939
public class Scripts implements Iterable<Scripts.Script> {
4040

41+
private static final String DEBUG_SUFFIX = "-debug";
42+
43+
enum Type {
44+
MINIFIED, DEBUG
45+
}
46+
4147
/**
4248
* A script wrapper.
4349
*/
@@ -103,11 +109,15 @@ public String toHtml() {
103109
SCRIPTS.put("jquery", new FileScript("js/jquery-3.4.1.min.js", 10));
104110
SCRIPTS.put("jquery-ui", new FileScript("js/jquery-ui-1.12.1-custom.min.js", 11));
105111
SCRIPTS.put("jquery-tablesorter", new FileScript("js/jquery-tablesorter-2.26.6.min.js", 12));
106-
SCRIPTS.put("tablesorter-parsers", new FileScript("js/tablesorter-parsers-0.0.1.js", 13));
112+
SCRIPTS.put("tablesorter-parsers", new FileScript("js/tablesorter-parsers-0.0.1.min.js", 13));
113+
SCRIPTS.put("tablesorter-parsers" + DEBUG_SUFFIX, new FileScript("js/tablesorter-parsers-0.0.1.js", 13));
107114
SCRIPTS.put("searchable-option-list", new FileScript("js/searchable-option-list-2.0.7.min.js", 14));
108-
SCRIPTS.put("utils", new FileScript("js/utils-0.0.31.js", 15));
109-
SCRIPTS.put("repos", new FileScript("js/repos-0.0.1.js", 20));
110-
SCRIPTS.put("diff", new FileScript("js/diff-0.0.3.js", 20));
115+
SCRIPTS.put("utils", new FileScript("js/utils-0.0.31.min.js", 15));
116+
SCRIPTS.put("utils" + DEBUG_SUFFIX, new FileScript("js/utils-0.0.31.js", 15));
117+
SCRIPTS.put("repos", new FileScript("js/repos-0.0.1.min.js", 20));
118+
SCRIPTS.put("repos" + DEBUG_SUFFIX, new FileScript("js/repos-0.0.1.js", 20));
119+
SCRIPTS.put("diff", new FileScript("js/diff-0.0.3.min.js", 20));
120+
SCRIPTS.put("diff" + DEBUG_SUFFIX, new FileScript("js/diff-0.0.3.js", 20));
111121
SCRIPTS.put("jquery-caret", new FileScript("js/jquery.caret-1.5.2.min.js", 25));
112122
}
113123

@@ -180,20 +190,26 @@ public Iterator<Script> iterator() {
180190
*
181191
* @param contextPath given context path for the used URL
182192
* @param scriptName name of the script
193+
* @param type type of the script to add
183194
* @return true if script was added; false otherwise
184195
*/
185-
public boolean addScript(String contextPath, String scriptName) {
196+
public boolean addScript(String contextPath, String scriptName, Type type) {
186197
contextPath = contextPath == null || contextPath.isEmpty() ? "/" : contextPath + "/";
187-
if (SCRIPTS.containsKey(scriptName)) {
188-
this.addScript(
189-
// put the context path end append the script path
190-
new FileScript(contextPath + SCRIPTS.get(scriptName).getScriptData(),
191-
SCRIPTS.get(scriptName).getPriority()));
198+
if (type == Type.DEBUG && SCRIPTS.containsKey(scriptName + DEBUG_SUFFIX)) {
199+
addScript(contextPath, scriptName + DEBUG_SUFFIX);
200+
return true;
201+
} else if (SCRIPTS.containsKey(scriptName)) {
202+
addScript(contextPath, scriptName);
192203
return true;
193204
}
194205
return false;
195206
}
196207

208+
private void addScript(String contextPath, String scriptName) {
209+
this.addScript(new FileScript(contextPath + SCRIPTS.get(scriptName).getScriptData(),
210+
SCRIPTS.get(scriptName).getPriority()));
211+
}
212+
197213
/**
198214
* Add a script to the page, taking the script priority into account.
199215
*

opengrok-indexer/src/test/java/org/opengrok/indexer/web/ScriptsTest.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package org.opengrok.indexer.web;
2424

2525
import static org.junit.Assert.assertEquals;
26+
import static org.junit.Assert.assertFalse;
2627
import static org.junit.Assert.assertTrue;
2728
import static org.junit.Assert.fail;
2829

@@ -108,10 +109,10 @@ public void testContent() {
108109

109110
@Test
110111
public void testLookup() {
111-
scripts.addScript("", "utils");
112-
scripts.addScript("", "jquery");
113-
scripts.addScript("", "diff");
114-
scripts.addScript("", "jquery-tablesorter");
112+
scripts.addScript("", "utils", Scripts.Type.MINIFIED);
113+
scripts.addScript("", "jquery", Scripts.Type.MINIFIED);
114+
scripts.addScript("", "diff", Scripts.Type.MINIFIED);
115+
scripts.addScript("", "jquery-tablesorter", Scripts.Type.MINIFIED);
115116

116117
assertEquals(4, scripts.size());
117118

@@ -143,10 +144,10 @@ public void testLookup() {
143144
@Test
144145
public void testLookupWithContextPath() {
145146
String contextPath = "/source";
146-
scripts.addScript(contextPath, "utils");
147-
scripts.addScript(contextPath, "jquery");
148-
scripts.addScript(contextPath, "diff");
149-
scripts.addScript(contextPath, "jquery-tablesorter");
147+
scripts.addScript(contextPath, "utils", Scripts.Type.MINIFIED);
148+
scripts.addScript(contextPath, "jquery", Scripts.Type.MINIFIED);
149+
scripts.addScript(contextPath, "diff", Scripts.Type.MINIFIED);
150+
scripts.addScript(contextPath, "jquery-tablesorter", Scripts.Type.MINIFIED);
150151

151152
assertEquals(4, scripts.size());
152153

@@ -174,4 +175,17 @@ public void testLookupWithContextPath() {
174175
+ " data-priority=\"" + s.getValue().getPriority() + "\"></script>"));
175176
}
176177
}
178+
179+
@Test
180+
public void testAddMinified() {
181+
scripts.addScript("", "utils", Scripts.Type.MINIFIED);
182+
assertTrue(scripts.iterator().next().scriptData.endsWith("min.js"));
183+
}
184+
185+
@Test
186+
public void testAddDebug() {
187+
scripts.addScript("", "utils", Scripts.Type.DEBUG);
188+
assertFalse(scripts.iterator().next().scriptData.endsWith("min.js"));
189+
}
190+
177191
}

opengrok-web/pom.xml

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ information: Portions Copyright [yyyy] [name of copyright owner]
1818
1919
CDDL HEADER END
2020
21-
Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
21+
Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
2222
Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
2323
2424
-->
@@ -200,6 +200,26 @@ Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
200200
</argLine>
201201
</configuration>
202202
</plugin>
203+
<plugin>
204+
<groupId>net.alchim31.maven</groupId>
205+
<artifactId>yuicompressor-maven-plugin</artifactId>
206+
<version>1.5.1</version>
207+
<executions>
208+
<execution>
209+
<goals>
210+
<goal>jslint</goal>
211+
<goal>compress</goal>
212+
</goals>
213+
</execution>
214+
</executions>
215+
<configuration>
216+
<suffix>.min</suffix>
217+
<excludes>
218+
<exclude>**/*min.js</exclude>
219+
<exclude>**/*min.css</exclude>
220+
</excludes>
221+
</configuration>
222+
</plugin>
203223
<plugin>
204224
<groupId>org.apache.maven.plugins</groupId>
205225
<artifactId>maven-checkstyle-plugin</artifactId>
@@ -234,4 +254,12 @@ Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
234254
</profile>
235255
</profiles>
236256

257+
<pluginRepositories>
258+
<pluginRepository>
259+
<name>oss.sonatype.org</name>
260+
<id>oss.sonatype.org</id>
261+
<url>http://oss.sonatype.org/content/groups/public</url>
262+
</pluginRepository>
263+
</pluginRepositories>
264+
237265
</project>

opengrok-web/src/main/webapp/httpheader.jspf

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ information: Portions Copyright [yyyy] [name of copyright owner]
1818

1919
CDDL HEADER END
2020

21-
Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
21+
Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.
2222
Portions Copyright 2011 Jens Elkner.
2323
Portions Copyright (c) 2017-2018, Chris Fraire <[email protected]>.
2424

@@ -43,8 +43,8 @@ org.opengrok.indexer.web.PageConfig"
4343
PageConfig cfg = PageConfig.get(request);
4444
String styleDir = cfg.getCssDir();
4545
String ctxPath = request.getContextPath();
46-
String dstyle = styleDir + '/' + "style.css";
47-
String pstyle = styleDir + '/' + "print.css";
46+
String dstyle = styleDir + '/' + "style.min.css";
47+
String pstyle = styleDir + '/' + "print.min.css";
4848
%><!DOCTYPE html>
4949
<html lang="en"
5050
class="<%= request.getServletPath().length() > 0 ? request.getServletPath().substring(1) : "" %>">
@@ -58,13 +58,13 @@ org.opengrok.indexer.web.PageConfig"
5858
title="Default" href="<%= dstyle %>" />
5959
<link rel="alternate stylesheet" type="text/css" media="all"
6060
title="Paper White" href="<%= pstyle %>" />
61-
<link rel="stylesheet" type="text/css" href="<%=styleDir%>/mandoc.css" media="all" />
62-
<link rel="stylesheet" type="text/css" href="<%=styleDir%>/print.css" media="print" />
61+
<link rel="stylesheet" type="text/css" href="<%=styleDir%>/mandoc.min.css" media="all" />
62+
<link rel="stylesheet" type="text/css" href="<%=styleDir%>/print.min.css" media="print" />
6363
<link rel="stylesheet" type="text/css" href="<%=styleDir%>/jquery-ui-1.12.1-custom.min.css" />
6464
<link rel="stylesheet" type="text/css" href="<%=styleDir%>/jquery-ui-1.12.1-custom.structure.min.css" />
6565
<link rel="stylesheet" type="text/css" href="<%=styleDir%>/jquery-ui-1.12.1-custom.theme.min.css" />
66-
<link rel="stylesheet" type="text/css" href="<%=styleDir%>/jquery.tooltip.css" />
67-
<link rel="stylesheet" type="text/css" href="<%=styleDir%>/jquery.tablesorter.css" />
66+
<link rel="stylesheet" type="text/css" href="<%=styleDir%>/jquery.tooltip.min.css" />
67+
<link rel="stylesheet" type="text/css" href="<%=styleDir%>/jquery.tablesorter.min.css" />
6868
<link rel="stylesheet" type="text/css" href="<%=styleDir%>/searchable-option-list-2.0.3.min.css" />
6969

7070
<%

opengrok-web/src/main/webapp/js/utils-0.0.31.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,14 +1846,14 @@ function initAutocompleteForField(inputId, field, config, dataFunction, errorEle
18461846
});
18471847
if (config.showTime) {
18481848
$("<li>", {
1849-
class: "ui-state-disabled",
1849+
"class": "ui-state-disabled",
18501850
style: 'padding-left: 5px;',
18511851
text: time + ' ms'
18521852
}).appendTo(ul);
18531853
}
18541854
if (partialResult) {
18551855
$("<li>", {
1856-
class: "ui-state-disabled",
1856+
"class": "ui-state-disabled",
18571857
style: 'padding-left: 5px;',
18581858
text: 'Partial result due to timeout'
18591859
}).appendTo(ul);
@@ -1934,7 +1934,7 @@ function showError(errorText, errorElem) {
19341934
var span = parent.find('#autocomplete-error')[0];
19351935
if (!span) {
19361936
span = $("<span>", {
1937-
class: "important-note important-note-rounded",
1937+
"class": "important-note important-note-rounded",
19381938
style: "right: -10px; position: absolute; top: 0px;",
19391939
text: "!",
19401940
id: 'autocomplete-error'
@@ -1966,17 +1966,17 @@ function hideError(errorElem) {
19661966
function getSuggestionListItem(itemData, config) {
19671967
if (itemData.selectable === false) {
19681968
return $("<li>", {
1969-
class: "ui-state-disabled",
1969+
"class": "ui-state-disabled",
19701970
text: itemData.phrase
19711971
});
19721972
}
19731973

19741974
var listItem = $("<li>", {
1975-
class: "ui-menu-item",
1975+
"class": "ui-menu-item",
19761976
style: "display: block;"
19771977
});
19781978
var listItemChild = $("<div>", {
1979-
class: "ui-menu-item-wrapper",
1979+
"class": "ui-menu-item-wrapper",
19801980
style: "height: 20px; padding: 0;",
19811981
tabindex: "-1"
19821982
});

0 commit comments

Comments
 (0)