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 2012/10/25 23:41:59 UTC

[9/13] git commit: Upgrade bundled RequireJS to version 2.1.1 Bundle jQuery 1.8.2 Rework how ShimModules operate - don't configure for shims that are simply Resource overrides - support an initialization expression as well as exports - for shims with jus

Upgrade bundled RequireJS to version 2.1.1
Bundle jQuery 1.8.2
Rework how ShimModules operate
- don't configure for shims that are simply Resource overrides
- support an initialization expression as well as exports
- for shims with just dependencies, use the more concise configuration
- Make configuring a ShimModule more fluid


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

Branch: refs/heads/5.4-js-rewrite
Commit: 92321efcd6192efefb753826dcf92d5a398f7cb2
Parents: 7015dd0
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Thu Oct 25 09:43:34 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Thu Oct 25 09:43:34 2012 -0700

----------------------------------------------------------------------
 .../services/javascript/ModuleManagerImpl.java     |   58 +-
 .../apache/tapestry5/services/TapestryModule.java  |    2 +-
 .../services/javascript/JavaScriptModule.java      |   18 +-
 .../tapestry5/services/javascript/ShimModule.java  |   90 +-
 .../META-INF/assets/tapestry5/jquery-1.8.2.js      | 9442 +++++++++++++++
 .../META-INF/assets/tapestry5/require-2.1.1.js     | 1981 +++
 .../META-INF/assets/tapestry5/require_2.0.2.js     | 2037 ----
 7 files changed, 11565 insertions(+), 2063 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/92321efc/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/ModuleManagerImpl.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/ModuleManagerImpl.java b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/ModuleManagerImpl.java
index 3509f79..39cecf8 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/ModuleManagerImpl.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/ModuleManagerImpl.java
@@ -25,8 +25,8 @@ import org.apache.tapestry5.ioc.Resource;
 import org.apache.tapestry5.ioc.annotations.PostInjection;
 import org.apache.tapestry5.ioc.annotations.Symbol;
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
-import org.apache.tapestry5.ioc.internal.util.InternalUtils;
 import org.apache.tapestry5.json.JSONArray;
+import org.apache.tapestry5.json.JSONLiteral;
 import org.apache.tapestry5.json.JSONObject;
 import org.apache.tapestry5.services.AssetSource;
 import org.apache.tapestry5.services.ComponentClassResolver;
@@ -99,7 +99,7 @@ public class ModuleManagerImpl implements ModuleManager
 
     private String buildRequireJSConfig(String baseURL, Map<String, ShimModule> configuration, boolean devMode)
     {
-        JSONObject config = new JSONObject().put("baseUrl", baseURL);
+        JSONObject config = new JSONObject("baseUrl", baseURL);
 
         // In DevMode, wait up to five minutes for a script, as the developer may be using the debugger.
         if (devMode)
@@ -113,24 +113,54 @@ public class ModuleManagerImpl implements ModuleManager
 
             shimModuleNameToResource.put(name, module.resource);
 
-            JSONObject shim = config.in("shim").in(name);
-
-            if (module.dependencies != null && !module.dependencies.isEmpty())
+            // Some modules (particularly overrides) just need an alternate location for their content
+            // on the server.
+            if (module.getNeedsConfiguration())
             {
-                for (String dep : module.dependencies)
-                {
-                    shim.accumulate("deps", dep);
-                }
+                // Others are libraries being shimmed as AMD modules, and need some configuration
+                // to ensure that everything hooks up properly on the client.
+                addModuleToConfig(config, name, module);
             }
+        }
+
+        return String.format("require.config(%s);\n", config.toString(compactJSON));
+    }
+
+    private void addModuleToConfig(JSONObject config, String name, ShimModule module)
+    {
+        JSONObject shimConfig = config.in("shim");
+
+        boolean nestDependencies = false;
+
+        String exports = module.getExports();
+
+        if (exports != null)
+        {
+            shimConfig.in(name).put("exports", exports);
+            nestDependencies = true;
+        }
+
+        String initExpression = module.getInitExpression();
+
+        if (initExpression != null)
+        {
+            String function = String.format("function() { return %s; }", initExpression);
+            shimConfig.in(name).put("init", new JSONLiteral(function));
+            nestDependencies = true;
+        }
+
+        List<String> dependencies = module.getDependencies();
 
-            if (InternalUtils.isNonBlank(module.exports))
+        if (dependencies != null)
+        {
+            JSONObject container = nestDependencies ? shimConfig.in(name) : shimConfig;
+            String key = nestDependencies ? "deps" : name;
+
+            for (String dep : dependencies)
             {
-                shim.put("exports", module.exports);
+                container.append(key, dep);
             }
         }
-
-        return String.format("require.config(%s);\n",
-                config.toString(compactJSON));
     }
 
     @PostInjection

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/92321efc/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java b/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
index 6dc0e56..d3eeb2c 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
@@ -2206,7 +2206,7 @@ public final class TapestryModule
         // By default, no page is on the whitelist unless it has the @WhitelistAccessOnly annotation
         configuration.add(MetaDataConstants.WHITELIST_ONLY_PAGE, false);
 
-        configuration.add(SymbolConstants.REQUIRE_JS, "${tapestry.asset.root}/require_2.0.2.js");
+        configuration.add(SymbolConstants.REQUIRE_JS, "${tapestry.asset.root}/require-2.1.1.js");
         configuration.add(SymbolConstants.CONTEXT_PATH, "");
 
         // Leaving this as the default results in a runtime error logged to the console (and a default password is used);

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/92321efc/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/JavaScriptModule.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/JavaScriptModule.java b/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/JavaScriptModule.java
index 2017fd6..e909750 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/JavaScriptModule.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/JavaScriptModule.java
@@ -179,14 +179,22 @@ public class JavaScriptModule
 
     @Contribute(ModuleManager.class)
     public static void setupBaseModuleShims(MappedConfiguration<String, Object> configuration,
-                                            @Inject @Path("classpath:META-INF/assets/tapestry5/underscore_1_4_2.js")
+                                            @Inject @Path("${tapestry.asset.root}/underscore_1_4_2.js")
                                             Resource underscore,
 
                                             @Inject @Path("${tapestry.scriptaculous}/prototype.js")
-                                            Resource prototype)
+                                            Resource prototype,
+
+                                            @Inject @Path("${tapestry.asset.root}/jquery-1.8.2.js")
+                                            Resource jQuery,
+
+                                            @Inject @Path("${" + SymbolConstants.BOOTSTRAP_ROOT + "}/js/bootstrap.js")
+                                            Resource bootstrap)
     {
-        configuration.add("_", new ShimModule(underscore, null, "_"));
-        configuration.add("prototype", new ShimModule(prototype, null, null));
+        configuration.add("_", new ShimModule(underscore).exports("_"));
+        configuration.add("$", new ShimModule(jQuery).initializeWith("jQuery.noConflict()"));
+        configuration.add("prototype", new ShimModule(prototype));
+        configuration.add("bootstrap", new ShimModule(bootstrap).dependsOn("$"));
     }
 
     @Contribute(ModuleManager.class)
@@ -200,7 +208,7 @@ public class JavaScriptModule
         {
             MessageCatalogResource resource = new MessageCatalogResource(locale, messagesSource, resourceChangeTracker, compactJSON);
 
-            configuration.add("core/messages/" + locale.toString(), new ShimModule(resource, null, null));
+            configuration.add("core/messages/" + locale.toString(), new ShimModule(resource));
         }
     }
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/92321efc/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/ShimModule.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/ShimModule.java b/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/ShimModule.java
index 9cd1150..274c134 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/ShimModule.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/ShimModule.java
@@ -16,6 +16,7 @@ package org.apache.tapestry5.services.javascript;
 
 import org.apache.tapestry5.ioc.Resource;
 
+import java.util.Arrays;
 import java.util.List;
 
 /**
@@ -25,8 +26,6 @@ import java.util.List;
  * <p/>
  * Instances of this class are contributed to the {@link ModuleManager} service;  the contribution key is the module name
  * (typically, a single word).
- * <p/>
- * Tapestry contributes a single module, {@code _} for the <a href="http://underscore.js.org">Underscore</a> JavaScript library.
  *
  * @since 5.4
  */
@@ -40,19 +39,98 @@ public final class ShimModule
     /**
      * The names of other shim modules that should be loaded before this shim module.
      */
-    public final List<String> dependencies;
+    private List<String> dependencies;
 
     /**
      * Optional (but desirable) value exported by the shim module.
      */
-    public final String exports;
+    private String exports;
+
+    private String initExpression;
+
+    private boolean needsConfiguration;
 
-    public ShimModule(Resource resource, List<String> dependencies, String exports)
+    public ShimModule(Resource resource)
     {
         assert resource != null;
 
         this.resource = resource;
-        this.dependencies = dependencies;
+    }
+
+    /**
+     * A list of other module names the shim depends on.
+     *
+     * @param moduleNames
+     * @return this ShimModule for further configuration
+     */
+    public ShimModule dependsOn(String... moduleNames)
+    {
+        assert moduleNames.length > 0;
+
+        dependencies = Arrays.asList(moduleNames);
+
+        needsConfiguration = true;
+
+        return this;
+    }
+
+    public List<String> getDependencies()
+    {
+        return dependencies;
+    }
+
+    /**
+     * The name of a global variable exported by the module. This will be the value injected into
+     * modules that depend on the shim.
+     *
+     * @return this ShimModule for further configuration
+     */
+    public ShimModule exports(String exports)
+    {
+        assert exports != null;
+
         this.exports = exports;
+
+        needsConfiguration = true;
+
+        return this;
+    }
+
+    public String getExports()
+    {
+        return exports;
+    }
+
+    /**
+     * Used as an alternative to {@linkplain #exports(String)}, this allows a short expression to be specified; the
+     * expression is used to initialize, clean up, and (usually) return the module's export value. For Underscore, this
+     * would be "_.noConflict()".  If the expression returns null, then the exports value is used.
+     *
+     * @param expression
+     *         initialization expression
+     * @return this ShimModule, for further configuration
+     */
+    public ShimModule initializeWith(String expression)
+    {
+        assert expression != null;
+
+        this.initExpression = expression;
+
+        needsConfiguration = true;
+
+        return this;
+    }
+
+    public String getInitExpression()
+    {
+        return initExpression;
+    }
+
+    /**
+     * Returns true if the module contains any additional configuration beyond its {@link Resource}.
+     */
+    public boolean getNeedsConfiguration()
+    {
+        return needsConfiguration;
     }
 }