You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2013/11/23 02:21:43 UTC

[2/2] git commit: Optimize JavaScript loading, especially when aggregation is enabled - jQuery now always loaded (in no-conflict mode) as part of core stack

Optimize JavaScript loading, especially when aggregation is enabled
- jQuery now always loaded (in no-conflict mode) as part of core stack


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/c29f4101
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/c29f4101
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/c29f4101

Branch: refs/heads/master
Commit: c29f4101dbb514c494f3ab797bff81fd0919f269
Parents: 5cf6a7e
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Fri Nov 22 17:21:36 2013 -0800
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Fri Nov 22 17:21:36 2013 -0800

----------------------------------------------------------------------
 .../org/apache/tapestry5/SymbolConstants.java   |  8 ---
 .../apache/tapestry5/modules/AssetsModule.java  |  1 -
 .../tapestry5/modules/JavaScriptModule.java     | 59 ++++++++++----------
 .../services/javascript/StackExtension.java     | 21 +++++++
 .../org/apache/tapestry5/t5-core-dom.coffee     |  2 +-
 .../assets/tapestry5/jquery-noconflict.js       | 16 ++++++
 .../META-INF/assets/tapestry5/jquery-shim.js    | 10 +++-
 .../assets/tapestry5/underscore-shim.js         |  5 +-
 8 files changed, 77 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/c29f4101/tapestry-core/src/main/java/org/apache/tapestry5/SymbolConstants.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/SymbolConstants.java b/tapestry-core/src/main/java/org/apache/tapestry5/SymbolConstants.java
index dcf4a44..9414917 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/SymbolConstants.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/SymbolConstants.java
@@ -352,14 +352,6 @@ public class SymbolConstants
     public static final String COMPRESSED_ASSET_PATH_PREFIX = "tapestry.compressed-asset-path-prefix";
 
     /**
-     * The Asset path to the copy of Require JS to include in rendered pages. Any page that makes use of any JavaScript will
-     * automatically import this library (as well as the core JavaScript stack).
-     *
-     * @since 5.4
-     */
-    public static final String REQUIRE_JS = "tapestry.requirejs";
-
-    /**
      * Identifies the context path of the application, as determined from {@link javax.servlet.ServletContext#getContextPath()}.
      * This is either a blank string or a string that starts with a slash but does not end with one.
      *

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/c29f4101/tapestry-core/src/main/java/org/apache/tapestry5/modules/AssetsModule.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/modules/AssetsModule.java b/tapestry-core/src/main/java/org/apache/tapestry5/modules/AssetsModule.java
index 0b94700..ca928e6 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/modules/AssetsModule.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/modules/AssetsModule.java
@@ -80,7 +80,6 @@ public class AssetsModule
         configuration.add(SymbolConstants.COMPRESSED_ASSET_PATH_PREFIX, "${tapestry.asset-path-prefix}.gz");
 
         configuration.add(SymbolConstants.BOOTSTRAP_ROOT, "${tapestry.asset.root}/bootstrap-3.0.2");
-        configuration.add(SymbolConstants.REQUIRE_JS, "${tapestry.asset.root}/require-2.1.9.js");
 
         configuration.add("tapestry.asset.root", "classpath:META-INF/assets/tapestry5");
     }

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/c29f4101/tapestry-core/src/main/java/org/apache/tapestry5/modules/JavaScriptModule.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/modules/JavaScriptModule.java b/tapestry-core/src/main/java/org/apache/tapestry5/modules/JavaScriptModule.java
index 220e575..9475594 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/modules/JavaScriptModule.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/modules/JavaScriptModule.java
@@ -90,6 +90,9 @@ public class JavaScriptModule
      * <dt>underscore-library, underscore-module</dt>
      * <dt>The Underscore JavaScript library, and the shim that allows underscore to be injected</dt>
      * <dt>t5/core/init</dt> <dd>Optional module related to t53-compatibility.js</dd>
+     * <dt>jquery-library</dt> <dd>The jQuery library</dd>
+     * <dt>jquery-noconflict</dt> <dd>Switches jQuery to no-conflic mode (only present when the infrastructure is "prototype").</dd>
+     * <dt>jquery</dt> <dd>A module shim that allows jQuery to be injected (and also switches jQuery to no-conflict mode)</dd>
      * <dt>bootstrap.css, tapestry.css, exception-frame.css, tapestry-console.css, tree.css</dt>
      * <dd>CSS files</dd>
      * <dt>t5/core/[ajax, dom, events, console, exception-frame, pageinit, messages, utils]</dt>
@@ -102,32 +105,45 @@ public class JavaScriptModule
      */
     @Contribute(JavaScriptStack.class)
     @Core
-    public static void setupCoreJavaScriptStack(OrderedConfiguration<StackExtension> configuration, Compatibility compatibility,
-                                                @Symbol(SymbolConstants.REQUIRE_JS)
-                                                String requireJS,
+    public static void setupCoreJavaScriptStack(OrderedConfiguration<StackExtension> configuration,
+                                                Compatibility compatibility,
                                                 @Symbol(SymbolConstants.JAVASCRIPT_INFRASTRUCTURE_PROVIDER)
                                                 String provider)
     {
-        configuration.add("requirejs", new StackExtension(StackExtensionType.LIBRARY, requireJS));
-        configuration.add("underscore-library", new StackExtension(StackExtensionType.LIBRARY, "${tapestry.asset.root}/underscore-1.5.2.js"));
 
         final String ROOT = "${tapestry.asset.root}";
 
-        if (provider.equals("prototype") && compatibility.enabled(Trait.SCRIPTACULOUS))
+        configuration.add("requirejs", StackExtension.library(ROOT + "/require-2.1.9.js"));
+        configuration.add("underscore-library", StackExtension.library(ROOT + "/underscore-1.5.2.js"));
+
+        if (provider.equals("prototype"))
         {
-            add(configuration, StackExtensionType.LIBRARY,
-                    "${tapestry.scriptaculous}/scriptaculous.js",
-                    "${tapestry.scriptaculous}/effects.js");
+            final String SCRIPTY = "${tapestry.scriptaculous}";
+
+            add(configuration, StackExtensionType.LIBRARY, SCRIPTY + "/prototype.js");
+
+            if (compatibility.enabled(Trait.SCRIPTACULOUS))
+            {
+                add(configuration, StackExtensionType.LIBRARY,
+                        SCRIPTY + "/scriptaculous.js",
+                        SCRIPTY + "/effects.js");
+            }
         }
 
         if (compatibility.enabled(Trait.INITIALIZERS))
         {
-            add(configuration, StackExtensionType.LIBRARY,
-                    ROOT + "/t53-compatibility.js"
-            );
+            add(configuration, StackExtensionType.LIBRARY, ROOT + "/t53-compatibility.js");
             configuration.add("t5/core/init", new StackExtension(StackExtensionType.MODULE, "t5/core/init"));
         }
 
+        configuration.add("jquery-library", StackExtension.library(ROOT + "/jquery-1.9.1.js"));
+
+        if (provider.equals("prototype")) {
+            configuration.add("jquery-noconflict", StackExtension.library(ROOT + "/jquery-noconflict.js"));
+        }
+
+        add(configuration, StackExtensionType.MODULE, "jquery");
+
         add(configuration, StackExtensionType.STYLESHEET,
                 "${" + SymbolConstants.BOOTSTRAP_ROOT + "}/css/bootstrap.css",
 
@@ -145,12 +161,7 @@ public class JavaScriptModule
             configuration.add(full, new StackExtension(StackExtensionType.MODULE, full));
         }
 
-        configuration.add("underscore-module", new StackExtension(StackExtensionType.MODULE, "underscore"));
-
-        if (provider.equals("jquery"))
-        {
-            add(configuration, StackExtensionType.MODULE, "jquery");
-        }
+        configuration.add("underscore-module", StackExtension.module("underscore"));
     }
 
     private static void add(OrderedConfiguration<StackExtension> configuration, StackExtensionType type, String... paths)
@@ -271,12 +282,6 @@ public class JavaScriptModule
                                         @Path("${tapestry.asset.root}/jquery-shim.js")
                                         Resource jqueryShim,
 
-                                        @Path("${tapestry.scriptaculous}/prototype.js")
-                                        Resource prototype,
-
-                                        @Path("${tapestry.asset.root}/jquery-1.9.1.js")
-                                        Resource jQuery,
-
                                         @Path("${tapestry.asset.root}/typeahead-0.9.3.js")
                                         Resource typeahead,
 
@@ -285,17 +290,13 @@ public class JavaScriptModule
     {
         // The underscore shim module allows Underscore to be injected
         configuration.add("underscore", new JavaScriptModuleConfiguration(underscoreShim));
-        // Hacking around https://github.com/jrburke/requirejs/issues/534
-        configuration.add("jquery-library", new JavaScriptModuleConfiguration(jQuery));
         configuration.add("jquery", new JavaScriptModuleConfiguration(jqueryShim));
-        configuration.add("prototype", new JavaScriptModuleConfiguration(prototype));
 
         configuration.add("bootstrap/transition", new JavaScriptModuleConfiguration(transition).dependsOn("jquery"));
 
         for (String name : new String[]{"affix", "alert", "button", "carousel", "collapse", "dropdown", "modal",
                 "scrollspy", "tab", "tooltip"})
         {
-
             Resource lib = transition.forFile(name + ".js");
 
             configuration.add("bootstrap/" + name, new JavaScriptModuleConfiguration(lib).dependsOn("bootstrap/transition"));
@@ -335,7 +336,7 @@ public class JavaScriptModule
             configuration.add("t5/core/dom", new JavaScriptModuleConfiguration(domJQuery));
         }
 
-        // If someone wants to support a different infastructure, they should set the provider symbol to some other value
+        // If someone wants to support a different infrastructure, they should set the provider symbol to some other value
         // and contribute their own version of the t5/core/dom module.
     }
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/c29f4101/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/StackExtension.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/StackExtension.java b/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/StackExtension.java
index 92cd06e..449050a 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/StackExtension.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/StackExtension.java
@@ -44,4 +44,25 @@ public class StackExtension
         return String.format("StackExtension[%s %s]", type.name(), value);
     }
 
+    /**
+     * Convenience for defining a LIBRARY.
+     *
+     * @since 5.4
+     */
+    public static StackExtension library(String path)
+    {
+        return new StackExtension(StackExtensionType.LIBRARY, path);
+    }
+
+    /**
+     * Convenience for defining a MODULE.
+     *
+     * @since 5.4
+     */
+    public static StackExtension module(String name)
+    {
+
+        return new StackExtension(StackExtensionType.MODULE, name);
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/c29f4101/tapestry-core/src/main/preprocessed-coffeescript/org/apache/tapestry5/t5-core-dom.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/preprocessed-coffeescript/org/apache/tapestry5/t5-core-dom.coffee b/tapestry-core/src/main/preprocessed-coffeescript/org/apache/tapestry5/t5-core-dom.coffee
index 859dae9..f1a7afb 100644
--- a/tapestry-core/src/main/preprocessed-coffeescript/org/apache/tapestry5/t5-core-dom.coffee
+++ b/tapestry-core/src/main/preprocessed-coffeescript/org/apache/tapestry5/t5-core-dom.coffee
@@ -30,7 +30,7 @@
 # the abstract layer and gain the valuable benefit of not caring about the infrastructure framework.
 
 #if prototype
-define ["underscore", "./utils", "./events", "prototype"],
+define ["underscore", "./utils", "./events", "jquery"],
 (_, utils, events) ->
 #elseif jquery
 define ["underscore", "./utils", "./events", "jquery"],

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/c29f4101/tapestry-core/src/main/resources/META-INF/assets/tapestry5/jquery-noconflict.js
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/resources/META-INF/assets/tapestry5/jquery-noconflict.js b/tapestry-core/src/main/resources/META-INF/assets/tapestry5/jquery-noconflict.js
new file mode 100644
index 0000000..955adf3
--- /dev/null
+++ b/tapestry-core/src/main/resources/META-INF/assets/tapestry5/jquery-noconflict.js
@@ -0,0 +1,16 @@
+// Copyright 2013 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http:#www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// It is important, when using Prototype and jQuery together, to get jQuery into no-conflict mode as soon as possible.
+jQuery.noConflict();
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/c29f4101/tapestry-core/src/main/resources/META-INF/assets/tapestry5/jquery-shim.js
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/resources/META-INF/assets/tapestry5/jquery-shim.js b/tapestry-core/src/main/resources/META-INF/assets/tapestry5/jquery-shim.js
index 7487335..9d88d2b 100644
--- a/tapestry-core/src/main/resources/META-INF/assets/tapestry5/jquery-shim.js
+++ b/tapestry-core/src/main/resources/META-INF/assets/tapestry5/jquery-shim.js
@@ -1,4 +1,4 @@
-// Copyright 2012 The Apache Software Foundation
+// Copyright 2012-2013 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -12,6 +12,10 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-define(["jquery-library"], function () {
-    return $.noConflict();
+
+// Sets jQuery up for noConflict mode, and returns its value so that it can be imported
+// into other modules (as module name "jquery", usually assigned to variable '$').
+// This injection is useful for when jQuery is replaced with something compatible, such as Zepto.
+define([], function () {
+    return jQuery.noConflict();
 });

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/c29f4101/tapestry-core/src/main/resources/META-INF/assets/tapestry5/underscore-shim.js
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/resources/META-INF/assets/tapestry5/underscore-shim.js b/tapestry-core/src/main/resources/META-INF/assets/tapestry5/underscore-shim.js
index 61c3ebe..3a41800 100644
--- a/tapestry-core/src/main/resources/META-INF/assets/tapestry5/underscore-shim.js
+++ b/tapestry-core/src/main/resources/META-INF/assets/tapestry5/underscore-shim.js
@@ -12,9 +12,8 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+// Handles executing Underscore with no-conflict mode, and makes Underscore injectable as "underscore".
+// This is handy if you want to replace Underscore with something compatible, such as Lo-Dash.
 define(function () {
-    // Because Underscore can load globally without causing a conflict, we can have the true underscore library
-    // just be a part of the core stack. This doesn't work with jQuery because the $ symbol can be a conflict
-    // between Prototype and jQuery when both are supported.
     return _.noConflict();
 });