You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2019/08/28 15:46:18 UTC

[myfaces-tobago] branch master updated: support for type="module"

This is an automated email from the ASF dual-hosted git repository.

lofwyr pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git


The following commit(s) were added to refs/heads/master by this push:
     new 8065244  support for type="module"
8065244 is described below

commit 8065244ef5806e29418de8340101f661585c6b87
Author: Udo Schnurpfeil <lo...@apache.org>
AuthorDate: Wed Aug 28 12:47:44 2019 +0200

    support for type="module"
    
    issue: TOBAGO-1633: TS refactoring
---
 .../org/apache/myfaces/tobago/context/Theme.java   |  4 +--
 .../apache/myfaces/tobago/context/ThemeImpl.java   | 40 +++++++++++++---------
 .../apache/myfaces/tobago/context/ThemeScript.java | 32 +++++++++++++++++
 .../internal/component/AbstractUIScript.java       |  2 ++
 .../tobago/internal/config/TobagoConfigParser.java |  2 ++
 .../internal/renderkit/renderer/PageRenderer.java  | 20 +++++------
 .../renderkit/renderer/ScriptRenderer.java         | 18 +---------
 .../taglib/component/ScriptTagDeclaration.java     |  8 ++++-
 .../myfaces/tobago/config/tobago-config-5.0.xsd    |  1 +
 .../config/TobagoConfigMergingUnitTest.java        | 11 ++++--
 .../tobago/internal/mock/faces/MockTheme.java      | 10 +++---
 .../src/main/resources/META-INF/tobago-config.xml  |  2 +-
 12 files changed, 96 insertions(+), 54 deletions(-)

diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/context/Theme.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/context/Theme.java
index a9fc079..091dede 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/context/Theme.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/context/Theme.java
@@ -28,9 +28,9 @@ public interface Theme {
 
   String getDisplayName();
 
-  String[] getScriptResources(boolean production);
+  ThemeScript[] getScriptResources(boolean production);
 
-  String[] getStyleResources(boolean production);
+  ThemeStyle[] getStyleResources(boolean production);
 
   String getVersion();
 
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/context/ThemeImpl.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/context/ThemeImpl.java
index 696febb..4e9e34b 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/context/ThemeImpl.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/context/ThemeImpl.java
@@ -42,10 +42,10 @@ public class ThemeImpl implements Theme, Serializable {
   private List<Theme> fallbackList;
   private ThemeResources productionResources;
   private ThemeResources resources;
-  private String[] productionScripts;
-  private String[] productionStyles;
-  private String[] scripts;
-  private String[] styles;
+  private ThemeScript[] productionScripts;
+  private ThemeStyle[] productionStyles;
+  private ThemeScript[] scripts;
+  private ThemeStyle[] styles;
   private boolean versioned;
   private String version;
 
@@ -160,20 +160,26 @@ public class ThemeImpl implements Theme, Serializable {
 
   public void init() {
     checkLocked();
-    productionScripts = sort(productionResources.getScriptList());
-    productionStyles = sort(productionResources.getStyleList());
-    scripts = sort(resources.getScriptList());
-    styles = sort(resources.getStyleList());
+    productionScripts = sortScripts(productionResources.getScriptList());
+    productionStyles = sortStyles(productionResources.getStyleList());
+    scripts = sortScripts(resources.getScriptList());
+    styles = sortStyles(resources.getStyleList());
   }
 
-  private String[] sort(List<? extends ThemeResource> list) {
-    final List<ThemeResource> copy = new ArrayList<>(list);
+  private ThemeScript[] sortScripts(List<ThemeScript> list) {
+    final List<ThemeScript> copy = new ArrayList<>(list);
     copy.sort(Comparator.comparingInt(ThemeResource::getPriority));
-    return copy.stream().map(ThemeResource::getName).toArray(String[]::new);
+    return copy.toArray(new ThemeScript[0]);
+  }
+
+  private ThemeStyle[] sortStyles(List<ThemeStyle> list) {
+    final List<ThemeStyle> copy = new ArrayList<>(list);
+    copy.sort(Comparator.comparingInt(ThemeResource::getPriority));
+    return copy.toArray(new ThemeStyle[0]);
   }
 
   @Override
-  public String[] getScriptResources(final boolean production) {
+  public ThemeScript[] getScriptResources(final boolean production) {
     if (production) {
       return productionScripts;
     } else {
@@ -182,7 +188,7 @@ public class ThemeImpl implements Theme, Serializable {
   }
 
   @Override
-  public String[] getStyleResources(final boolean production) {
+  public ThemeStyle[] getStyleResources(final boolean production) {
     if (production) {
       return productionStyles;
     } else {
@@ -223,22 +229,22 @@ public class ThemeImpl implements Theme, Serializable {
       builder.append("null");
     }
     builder.append(", \nproductionScripts=[");
-    for (final String s : productionScripts != null ? productionScripts : new String[0]) {
+    for (final ThemeScript s : productionScripts) {
       builder.append("\n");
       builder.append(s);
     }
     builder.append("], \nscripts=[");
-    for (final String s : scripts != null ? scripts : new String[0]) {
+    for (final ThemeScript s : scripts) {
       builder.append("\n");
       builder.append(s);
     }
     builder.append("], \nproductionStyles=[");
-    for (final String s : productionStyles != null ? productionStyles : new String[0]) {
+    for (final ThemeStyle s : productionStyles) {
       builder.append("\n");
       builder.append(s);
     }
     builder.append("], \nstyles=[");
-    for (final String s : styles != null ? styles : new String[0]) {
+    for (final ThemeStyle s : styles) {
       builder.append("\n");
       builder.append(s);
     }
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/context/ThemeScript.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/context/ThemeScript.java
index 17fc51b..ce9b4e6 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/context/ThemeScript.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/context/ThemeScript.java
@@ -19,8 +19,40 @@
 
 package org.apache.myfaces.tobago.context;
 
+import java.util.Objects;
+
 /**
  * @since 1.5.0
  */
 public final class ThemeScript extends ThemeResource {
+
+  private String type;
+
+  public String getType() {
+    return type;
+  }
+
+  public void setType(String type) {
+    this.type = type;
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+    if (!super.equals(o)) {
+      return false;
+    }
+    ThemeScript that = (ThemeScript) o;
+    return Objects.equals(type, that.type);
+  }
+
+  @Override
+  public int hashCode() {
+    return Objects.hash(super.hashCode(), type);
+  }
 }
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIScript.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIScript.java
index c4aaca2..3c83e18 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIScript.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIScript.java
@@ -67,4 +67,6 @@ public abstract class AbstractUIScript extends UIComponentBase {
   public abstract String getFile();
   public abstract void setFile(String file);
 
+  public abstract String getType();
+  public abstract void setType(String type);
 }
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParser.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParser.java
index 7db2ccf..c51c487 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParser.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParser.java
@@ -110,6 +110,7 @@ public class TobagoConfigParser extends TobagoConfigEntityResolver {
   private static final String ATTR_NAME = "name";
   private static final String ATTR_KEY = "key";
   private static final String ATTR_PRIORITY = "priority";
+  private static final String ATTR_TYPE = "type";
 
   private static final int MAX_PRIORITY = 65536;
 
@@ -210,6 +211,7 @@ public class TobagoConfigParser extends TobagoConfigEntityResolver {
       case SCRIPT:
         final ThemeScript script = new ThemeScript();
         script.setName(attributes.getValue(ATTR_NAME));
+        script.setType(attributes.getValue(ATTR_TYPE));
         final String scriptPriority = attributes.getValue(ATTR_PRIORITY);
         script.setPriority(scriptPriority != null ? Integer.parseUnsignedInt(scriptPriority) : MAX_PRIORITY);
         if (production) {
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/PageRenderer.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/PageRenderer.java
index e586ebf..ba1b9d0 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/PageRenderer.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/PageRenderer.java
@@ -25,6 +25,8 @@ import org.apache.myfaces.tobago.component.Tags;
 import org.apache.myfaces.tobago.config.TobagoConfig;
 import org.apache.myfaces.tobago.context.Markup;
 import org.apache.myfaces.tobago.context.Theme;
+import org.apache.myfaces.tobago.context.ThemeScript;
+import org.apache.myfaces.tobago.context.ThemeStyle;
 import org.apache.myfaces.tobago.context.TobagoContext;
 import org.apache.myfaces.tobago.internal.component.AbstractUIMeta;
 import org.apache.myfaces.tobago.internal.component.AbstractUIMetaLink;
@@ -193,13 +195,13 @@ public class PageRenderer extends RendererBase {
 
     // style files from theme
     AbstractUIStyle style = null;
-    for (final String styleFile : theme.getStyleResources(productionMode)) {
+    for (final ThemeStyle themeStyle : theme.getStyleResources(productionMode)) {
       if (style == null) {
         style = (AbstractUIStyle) facesContext.getApplication()
            .createComponent(facesContext, Tags.style.componentType(), RendererTypes.Style.name());
         style.setTransient(true);
       }
-      style.setFile(contextPath + styleFile);
+      style.setFile(contextPath + themeStyle.getName());
       style.encodeAll(facesContext);
     }
 
@@ -209,14 +211,12 @@ public class PageRenderer extends RendererBase {
     }
 
     // script files from theme
-    AbstractUIScript script = null;
-    for (final String scriptFile : theme.getScriptResources(productionMode)) {
-      if (script == null) {
-        script = (AbstractUIScript) facesContext.getApplication()
-            .createComponent(facesContext, Tags.script.componentType(), RendererTypes.Script.name());
-        script.setTransient(true);
-      }
-      script.setFile(contextPath + scriptFile);
+    for (final ThemeScript themeScript : theme.getScriptResources(productionMode)) {
+      final AbstractUIScript script = (AbstractUIScript) facesContext.getApplication()
+          .createComponent(facesContext, Tags.script.componentType(), RendererTypes.Script.name());
+      script.setTransient(true);
+      script.setFile(contextPath + themeScript.getName());
+      script.setType(themeScript.getType());
       script.encodeAll(facesContext);
     }
 
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/ScriptRenderer.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/ScriptRenderer.java
index b626251..dcc0082 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/ScriptRenderer.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/ScriptRenderer.java
@@ -39,23 +39,7 @@ public class ScriptRenderer extends RendererBase {
 
     writer.startElement(HtmlElements.SCRIPT);
     writer.writeAttribute(HtmlAttributes.SRC, script.getFile(), true);
-//  TODO: new attribute DEFER
-// XXX with defer activated, pages are not shown reliable
-//        writer.writeAttribute(HtmlAttributes.DEFER, true);
-    if (script.getFile().contains("myfaces")
-        || script.getFile().contains("deltaspike")
-        || script.getFile().contains("jquery")
-        || script.getFile().contains("qunit")
-        || script.getFile().contains("datetimepicker")
-        || script.getFile().contains("bootstrap.js")
-        || script.getFile().contains("moment")
-        || script.getFile().contains("popper")
-        || script.getFile().contains("typeahead")
-        || script.getFile().contains("tether")) {
-      writer.writeAttribute(HtmlAttributes.TYPE, "text/javascript", false);
-    } else {
-      writer.writeAttribute(HtmlAttributes.TYPE, "module", false);
-    }
+    writer.writeAttribute(HtmlAttributes.TYPE, script.getType(), true);
     writer.endElement(HtmlElements.SCRIPT);
   }
 
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/taglib/component/ScriptTagDeclaration.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/taglib/component/ScriptTagDeclaration.java
index c62223b..5b338ec 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/taglib/component/ScriptTagDeclaration.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/taglib/component/ScriptTagDeclaration.java
@@ -43,10 +43,16 @@ public interface ScriptTagDeclaration extends HasIdBindingAndRendered {
    * File name to include into the rendered page. The name must be full qualified, or relative.
    * If using a complete path from root, you'll need to add the contextPath from the web application.
    * This can be done with the EL #{request.contextPath}.
-   * @param file A JavaScript file.
+   * @param file A Script file.
    */
   @TagAttribute()
   @UIComponentTagAttribute()
   void setFile(String file);
 
+  /**
+   * Type of the script.
+   */
+  @TagAttribute()
+  @UIComponentTagAttribute(defaultValue = "text/javascript")
+  void setType(String type);
 }
diff --git a/tobago-core/src/main/resources/org/apache/myfaces/tobago/config/tobago-config-5.0.xsd b/tobago-core/src/main/resources/org/apache/myfaces/tobago/config/tobago-config-5.0.xsd
index 977b8aa..57845f8 100644
--- a/tobago-core/src/main/resources/org/apache/myfaces/tobago/config/tobago-config-5.0.xsd
+++ b/tobago-core/src/main/resources/org/apache/myfaces/tobago/config/tobago-config-5.0.xsd
@@ -364,6 +364,7 @@
 
   <xs:complexType name="script-type">
     <xs:attribute name="name" type="xs:string" use="required"/>
+    <xs:attribute name="type" type="xs:string"/>
     <xs:attribute name="priority" type="xs:unsignedShort"/>
   </xs:complexType>
 
diff --git a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/TobagoConfigMergingUnitTest.java b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/TobagoConfigMergingUnitTest.java
index e8cd242..ff76081 100644
--- a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/TobagoConfigMergingUnitTest.java
+++ b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/TobagoConfigMergingUnitTest.java
@@ -19,6 +19,7 @@
 
 package org.apache.myfaces.tobago.internal.config;
 
+import org.apache.myfaces.tobago.context.ThemeScript;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 import org.xml.sax.SAXException;
@@ -152,11 +153,17 @@ public class TobagoConfigMergingUnitTest {
         "script-undefined-d.js",
         "script-undefined-e.js"
     };
+    final ThemeScript[] ex = new ThemeScript[expected.length];
+    int i = 0;
+    for (String script : expected) {
+      ex[i] = new ThemeScript();
+      ex[i++].setName(script);
+    }
 
     config.resolveThemes();
-    final String[] scripts = config.getDefaultTheme().getScriptResources(true);
+    final ThemeScript[] scripts = config.getDefaultTheme().getScriptResources(true);
 
-    Assertions.assertArrayEquals(expected, scripts);
+    Assertions.assertArrayEquals(ex, scripts);
   }
 
   public static TobagoConfigImpl loadAndMerge(final String... names)
diff --git a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/mock/faces/MockTheme.java b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/mock/faces/MockTheme.java
index 931884f..c0e7162 100644
--- a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/mock/faces/MockTheme.java
+++ b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/mock/faces/MockTheme.java
@@ -21,6 +21,8 @@ package org.apache.myfaces.tobago.internal.mock.faces;
 
 import org.apache.myfaces.tobago.context.Theme;
 import org.apache.myfaces.tobago.context.ThemeImpl;
+import org.apache.myfaces.tobago.context.ThemeScript;
+import org.apache.myfaces.tobago.context.ThemeStyle;
 
 import java.util.List;
 
@@ -58,13 +60,13 @@ public class MockTheme extends ThemeImpl {
   }
 
   @Override
-  public String[] getScriptResources(final boolean production) {
-    return new String[0];
+  public ThemeScript[] getScriptResources(final boolean production) {
+    return new ThemeScript[0];
   }
 
   @Override
-  public String[] getStyleResources(final boolean production) {
-    return new String[0];
+  public ThemeStyle[] getStyleResources(final boolean production) {
+    return new ThemeStyle[0];
   }
 
   @Override
diff --git a/tobago-theme/tobago-theme-standard/src/main/resources/META-INF/tobago-config.xml b/tobago-theme/tobago-theme-standard/src/main/resources/META-INF/tobago-config.xml
index b9d45e7..d72a3b9 100644
--- a/tobago-theme/tobago-theme-standard/src/main/resources/META-INF/tobago-config.xml
+++ b/tobago-theme/tobago-theme-standard/src/main/resources/META-INF/tobago-config.xml
@@ -68,7 +68,7 @@
           <script name="/tobago/standard/tobago-bootstrap/${project.version}/js/tobago-deltaspike.js"/>
 <!--          <script name="/tobago/standard/tobago-bootstrap/${project.version}/node_modules/@babel/polyfill/dist/polyfill.js"/>-->
           <script name="/tobago/standard/tobago-bootstrap/${project.version}/js/tobago-polyfill.js"/>
-          <script name="/tobago/standard/tobago-bootstrap/${project.version}/js/tobago-all.js"/>
+          <script type="module" name="/tobago/standard/tobago-bootstrap/${project.version}/js/tobago-all.js"/>
 <!--
           <script name="/tobago/standard/tobago-bootstrap/${project.version}/js/tobago-listener.js"/>
           <script name="/tobago/standard/tobago-bootstrap/${project.version}/js/tobago-core.js"/>