When runAsync is not used, don't emit a __gwtStartLoadingFragment()
function.  It will never get called and so is wasted space.

Suggested by: knorton
Review by: knorton



git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@4354 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/core/ext/linker/impl/SelectionScriptLinker.java b/dev/core/src/com/google/gwt/core/ext/linker/impl/SelectionScriptLinker.java
index db5fddd..a527c56 100644
--- a/dev/core/src/com/google/gwt/core/ext/linker/impl/SelectionScriptLinker.java
+++ b/dev/core/src/com/google/gwt/core/ext/linker/impl/SelectionScriptLinker.java
@@ -118,7 +118,7 @@
     String[] js = result.getJavaScript();
     byte[][] bytes = new byte[js.length][];
     bytes[0] = generatePrimaryFragment(logger, context, js[0],
-        result.getStrongName());
+        result.getStrongName(), js.length);
     for (int i = 1; i < js.length; i++) {
       bytes[i] = Util.getBytes(js[i]);
     }
@@ -342,10 +342,28 @@
     return compilationStrongNames.get(result);
   }
 
+  /**
+   * Compute the beginning of a JavaScript file that will hold the main module
+   * implementation.
+   */
   protected abstract String getModulePrefix(TreeLogger logger,
       LinkerContext context, String strongName)
       throws UnableToCompleteException;
 
+  /**
+   * Compute the beginning of a JavaScript file that will hold the main module
+   * implementation. By default, calls
+   * {@link #getModulePrefix(TreeLogger, LinkerContext, String)}.
+   * 
+   * @param strongName strong name of the module being emitted
+   * @param numFragments the number of fragments for this module, including
+   *        the main fragment (fragment 0)
+   */
+  protected String getModulePrefix(TreeLogger logger, LinkerContext context,
+      String strongName, int numFragments) throws UnableToCompleteException {
+    return getModulePrefix(logger, context, strongName);
+  }
+
   protected abstract String getModuleSuffix(TreeLogger logger,
       LinkerContext context) throws UnableToCompleteException;
 
@@ -353,10 +371,10 @@
       LinkerContext context) throws UnableToCompleteException;
 
   private byte[] generatePrimaryFragment(TreeLogger logger,
-      LinkerContext context, String js, String strongName)
+      LinkerContext context, String js, String strongName, int numFragments)
       throws UnableToCompleteException {
     StringBuffer b = new StringBuffer();
-    b.append(getModulePrefix(logger, context, strongName));
+    b.append(getModulePrefix(logger, context, strongName, numFragments));
     b.append(js);
     b.append(getModuleSuffix(logger, context));
     return Util.getBytes(b.toString());
diff --git a/dev/core/src/com/google/gwt/core/linker/IFrameLinker.java b/dev/core/src/com/google/gwt/core/linker/IFrameLinker.java
index 9f50c76..8e54d8f 100644
--- a/dev/core/src/com/google/gwt/core/linker/IFrameLinker.java
+++ b/dev/core/src/com/google/gwt/core/linker/IFrameLinker.java
@@ -67,48 +67,13 @@
   @Override
   protected String getModulePrefix(TreeLogger logger, LinkerContext context,
       String strongName) {
-    DefaultTextOutput out = new DefaultTextOutput(context.isOutputCompact());
-    out.print("<html>");
-    out.newlineOpt();
+    return getModulePrefix(context, strongName, true);
+  }
 
-    // Setup the well-known variables.
-    out.print("<head><script>");
-    out.newlineOpt();
-    out.print("var $gwt_version = \"" + About.GWT_VERSION_NUM + "\";");
-    out.newlineOpt();
-    out.print("var $wnd = parent;");
-    out.newlineOpt();
-    out.print("var $doc = $wnd.document;");
-    out.newlineOpt();
-    out.print("var $moduleName, $moduleBase;");
-    out.newlineOpt();
-    out.print("function __gwtStartLoadingFragment(frag) {");
-    out.newlineOpt();
-    out.indentIn();
-    out.print("  var script = document.createElement('script');");
-    out.newlineOpt();
-    out.print("  script.src = '" + FRAGMENT_SUBDIR + "/" + strongName + "/' + frag + '" + FRAGMENT_EXTENSION + "';");
-    out.print("  document.getElementsByTagName('head').item(0).appendChild(script);");
-    out.indentOut();
-    out.newlineOpt();
-    out.print("};");
-    out.newlineOpt();
-    out.print("var $stats = $wnd.__gwtStatsEvent ? function(a) {return $wnd.__gwtStatsEvent(a);} : null;");
-    out.newlineOpt();
-    out.print("$stats && $stats({moduleName:'" + context.getModuleName()
-        + "',subSystem:'startup',evtGroup:'moduleStartup'"
-        + ",millis:(new Date()).getTime(),type:'moduleEvalStart'});");
-    out.newlineOpt();
-    out.print("</script></head>");
-    out.newlineOpt();
-    out.print("<body>");
-    out.newlineOpt();
-
-    // Begin a script block inside the body. It's commented out so that the
-    // browser won't mistake strings containing "<script>" for actual script.
-    out.print("<script><!--");
-    out.newline();
-    return out.toString();
+  @Override
+  protected String getModulePrefix(TreeLogger logger, LinkerContext context,
+      String strongName, int numFragments) {
+    return getModulePrefix(context, strongName, numFragments > 1);
   }
 
   @Override
@@ -136,4 +101,57 @@
     return "com/google/gwt/core/linker/IFrameTemplate.js";
   }
 
+  /**
+   * This is the real implementation of <code>getModulePrefix</code> for this
+   * linker.  The other versions forward to this one.
+   */
+  private String getModulePrefix(LinkerContext context, String strongName,
+      boolean supportRunAsync) {
+    DefaultTextOutput out = new DefaultTextOutput(context.isOutputCompact());
+    out.print("<html>");
+    out.newlineOpt();
+
+    // Setup the well-known variables.
+    out.print("<head><script>");
+    out.newlineOpt();
+    out.print("var $gwt_version = \"" + About.GWT_VERSION_NUM + "\";");
+    out.newlineOpt();
+    out.print("var $wnd = parent;");
+    out.newlineOpt();
+    out.print("var $doc = $wnd.document;");
+    out.newlineOpt();
+    out.print("var $moduleName, $moduleBase;");
+    out.newlineOpt();
+    if (supportRunAsync) {
+      out.print("function __gwtStartLoadingFragment(frag) {");
+      out.newlineOpt();
+      out.indentIn();
+      out.print("  var script = document.createElement('script');");
+      out.newlineOpt();
+      out.print("  script.src = '" + FRAGMENT_SUBDIR + "/" + strongName
+          + "/' + frag + '" + FRAGMENT_EXTENSION + "';");
+      out.print("  document.getElementsByTagName('head').item(0).appendChild(script);");
+      out.indentOut();
+      out.newlineOpt();
+      out.print("};");
+      out.newlineOpt();
+    }
+    out.print("var $stats = $wnd.__gwtStatsEvent ? function(a) {return $wnd.__gwtStatsEvent(a);} : null;");
+    out.newlineOpt();
+    out.print("$stats && $stats({moduleName:'" + context.getModuleName()
+        + "',subSystem:'startup',evtGroup:'moduleStartup'"
+        + ",millis:(new Date()).getTime(),type:'moduleEvalStart'});");
+    out.newlineOpt();
+    out.print("</script></head>");
+    out.newlineOpt();
+    out.print("<body>");
+    out.newlineOpt();
+
+    // Begin a script block inside the body. It's commented out so that the
+    // browser won't mistake strings containing "<script>" for actual script.
+    out.print("<script><!--");
+    out.newline();
+    return out.toString();
+  }
+
 }