Avoid calling compiler.toSource (which is expensive for large programs) when we're not actually outputting any source.
@@ -923,7 +923,7 @@ public abstract class AbstractCommandLineRunner<A extends Compiler,
String fileName = getModuleOutputFileName(m);
String baseName = new File(fileName).getName();
- writeOutput(out, compiler, compiler.toSource(m),
+ writeOutput(out, compiler, m,
parsedModuleWrappers.get(m.getName()).replace("%basename%", baseName),
"%s", null);
}
@@ -931,8 +931,9 @@ public abstract class AbstractCommandLineRunner<A extends Compiler,
/**
* Writes code to an output stream, optionally wrapping it in an arbitrary
* wrapper that contains a placeholder where the code should be inserted.
+ * @param module Which module to write. If this is null, write the entire AST.
*/
- static void writeOutput(Appendable out, Compiler compiler, String code,
+ static void writeOutput(Appendable out, Compiler compiler, @Nullable JSModule module,
String wrapper, String codePlaceholder,
@Nullable Function<String, String> escaper)
throws IOException {
@@ -942,6 +943,18 @@ public abstract class AbstractCommandLineRunner<A extends Compiler,
}
Preconditions.checkState(compiler.getOptions().outputJs == OutputJs.NORMAL);
+ String code = module == null ? compiler.toSource() : compiler.toSource(module);
+ writeOutput(out, compiler, code, wrapper, codePlaceholder, escaper);
+ }
+
+ /**
+ * Writes code to an output stream, optionally wrapping it in an arbitrary
+ * wrapper that contains a placeholder where the code should be inserted.
+ */
+ static void writeOutput(Appendable out, Compiler compiler, String code,
+ String wrapper, String codePlaceholder,
+ @Nullable Function<String, String> escaper)
+ throws IOException {
int pos = wrapper.indexOf(codePlaceholder);
if (pos != -1) {
String prefix = "";
@@ -1230,7 +1243,7 @@ public abstract class AbstractCommandLineRunner<A extends Compiler,
Appendable jsOutput = createDefaultOutput();
writeOutput(
- jsOutput, compiler, compiler.toSource(), config.outputWrapper,
+ jsOutput, compiler, (JSModule) null, config.outputWrapper,
marker, escaper);
closeAppendable(jsOutput);
}
@@ -1244,7 +1257,7 @@ public abstract class AbstractCommandLineRunner<A extends Compiler,
Function<String, String> escaper) throws IOException {
Appendable jsOutput = new StringBuilder();
writeOutput(
- jsOutput, compiler, compiler.toSource(), config.outputWrapper,
+ jsOutput, compiler, (JSModule) null, config.outputWrapper,
outputMarker, escaper);
JsonFileSpec jsonOutput = new JsonFileSpec(jsOutput.toString(),
@@ -1406,8 +1419,7 @@ public abstract class AbstractCommandLineRunner<A extends Compiler,
}
protected List<SourceFile> createExterns(CompilerOptions options) throws IOException {
- return isInTestMode() ? externsSupplierForTesting.get() :
- createExternInputs(config.externs);
+ return isInTestMode() ? externsSupplierForTesting.get() : createExternInputs(config.externs);
}
/**
