| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package com.google.dart.compiler.backend.js; | 5 package com.google.dart.compiler.backend.js; |
| 6 | 6 |
| 7 import com.google.common.collect.Lists; | |
| 8 import com.google.common.io.CharStreams; | 7 import com.google.common.io.CharStreams; |
| 9 import com.google.common.io.Closeables; | 8 import com.google.common.io.Closeables; |
| 10 import com.google.dart.compiler.DartCompilerContext; | 9 import com.google.dart.compiler.DartCompilerContext; |
| 11 import com.google.dart.compiler.DartSource; | 10 import com.google.dart.compiler.DartSource; |
| 12 import com.google.dart.compiler.LibrarySource; | 11 import com.google.dart.compiler.LibrarySource; |
| 13 import com.google.dart.compiler.ast.LibraryNode; | 12 import com.google.dart.compiler.ast.LibraryNode; |
| 14 import com.google.dart.compiler.ast.LibraryUnit; | 13 import com.google.dart.compiler.ast.LibraryUnit; |
| 15 import com.google.dart.compiler.backend.js.analysis.TreeShaker; | 14 import com.google.dart.compiler.backend.js.analysis.TreeShaker; |
| 16 import com.google.dart.compiler.metrics.CompilerMetrics; | 15 import com.google.dart.compiler.metrics.CompilerMetrics; |
| 17 import com.google.dart.compiler.resolver.CoreTypeProvider; | 16 import com.google.dart.compiler.resolver.CoreTypeProvider; |
| 18 | 17 |
| 19 import java.io.IOException; | 18 import java.io.IOException; |
| 20 import java.io.Reader; | 19 import java.io.Reader; |
| 21 import java.io.Writer; | 20 import java.io.Writer; |
| 22 import java.util.Collection; | 21 import java.util.Collection; |
| 23 import java.util.List; | |
| 24 | 22 |
| 25 /** | 23 /** |
| 26 * A compiler backend that produces raw Javascript. | 24 * A compiler backend that produces raw Javascript. |
| 27 */ | 25 */ |
| 28 public class JavascriptBackend extends AbstractJsBackend { | 26 public class JavascriptBackend extends AbstractJsBackend { |
| 29 | 27 |
| 30 public static final String EXTENSION_APP_JS_COMPLETE = EXTENSION_APP_JS + ".co
mplete"; | 28 public static final String EXTENSION_APP_JS_COMPLETE = EXTENSION_APP_JS + ".co
mplete"; |
| 31 | 29 |
| 32 /** | 30 /** |
| 33 * Wraps an Appendable and keeps track of the current offset as line/columns. | 31 * Wraps an Appendable and keeps track of the current offset as line/columns. |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 DependencyBuilder.build(context.getAppLibraryUnit(), callback); | 127 DependencyBuilder.build(context.getAppLibraryUnit(), callback); |
| 130 return callback.getCharsWritten(); | 128 return callback.getCharsWritten(); |
| 131 } | 129 } |
| 132 | 130 |
| 133 @Override | 131 @Override |
| 134 public void packageApp(LibrarySource app, | 132 public void packageApp(LibrarySource app, |
| 135 Collection<LibraryUnit> libraries, | 133 Collection<LibraryUnit> libraries, |
| 136 DartCompilerContext context, | 134 DartCompilerContext context, |
| 137 CoreTypeProvider typeProvider) | 135 CoreTypeProvider typeProvider) |
| 138 throws IOException { | 136 throws IOException { |
| 139 | 137 |
| 140 LibraryUnit appLibraryUnit = context.getAppLibraryUnit(); | 138 LibraryUnit appLibraryUnit = context.getAppLibraryUnit(); |
| 141 boolean hasEntryPoint = appLibraryUnit.getElement().getEntryPoint() != null; | 139 boolean hasEntryPoint = appLibraryUnit.getElement().getEntryPoint() != null; |
| 142 | 140 |
| 143 String completeArtifactName = EXTENSION_APP_JS; | 141 String completeArtifactName = EXTENSION_APP_JS; |
| 144 if (hasEntryPoint) { | 142 if (hasEntryPoint) { |
| 145 // Apps with entry points will be reduced so we write into a different fil
e name | 143 // Apps with entry points will be reduced so we write into a different fil
e name |
| 146 completeArtifactName = EXTENSION_APP_JS_COMPLETE; | 144 completeArtifactName = EXTENSION_APP_JS_COMPLETE; |
| 147 } | 145 } |
| 148 | 146 |
| 149 Writer out = context.getArtifactWriter(app, "", completeArtifactName); | 147 Writer out = context.getArtifactWriter(app, "", completeArtifactName); |
| 150 long outputFileSize = 0; | 148 long outputFileSize = 0; |
| 151 boolean failed = true; | 149 boolean failed = true; |
| 152 try { | 150 try { |
| 153 // Emit the concatenated Javascript sources in dependency order. | 151 // Emit the concatenated Javascript sources in dependency order. |
| 154 outputFileSize = packageLibs(out, context); | 152 outputFileSize = packageLibs(out, context); |
| 155 outputFileSize += writeEntryPointCall(getMangledEntryPoint(context), out); | 153 outputFileSize += writeEntryPointCall(getMangledEntryPoint(context), out); |
| 156 failed = false; | 154 failed = false; |
| 157 } finally { | 155 } finally { |
| 158 Closeables.close(out, failed); | 156 Closeables.close(out, failed); |
| 159 } | 157 } |
| 160 | 158 |
| 161 | 159 |
| 162 if (hasEntryPoint) { | 160 if (hasEntryPoint) { |
| 163 Writer artifactWriter = context.getArtifactWriter(app, "", EXTENSION_APP_J
S); | 161 Writer artifactWriter = context.getArtifactWriter(app, "", EXTENSION_APP_J
S); |
| 164 try { | 162 try { |
| 165 failed = true; | 163 failed = true; |
| 166 outputFileSize = TreeShaker.reduce(app, context, completeArtifactName, a
rtifactWriter); | 164 outputFileSize = TreeShaker.reduce(app, context, completeArtifactName, a
rtifactWriter); |
| 167 failed = false; | 165 failed = false; |
| 168 } finally { | 166 } finally { |
| 169 Closeables.close(artifactWriter, failed); | 167 Closeables.close(artifactWriter, failed); |
| 170 } | 168 } |
| 171 } | 169 } |
| 172 | 170 |
| 173 CompilerMetrics compilerMetrics = context.getCompilerMetrics(); | 171 CompilerMetrics compilerMetrics = context.getCompilerMetrics(); |
| 174 if (compilerMetrics != null) { | 172 if (compilerMetrics != null) { |
| 175 compilerMetrics.packagedJsApplication(outputFileSize, -1); | 173 compilerMetrics.packagedJsApplication(outputFileSize, -1); |
| 176 } | 174 } |
| 177 } | 175 } |
| 178 | 176 |
| 179 @Override | 177 @Override |
| 180 public String getAppExtension() { | 178 public String getAppExtension() { |
| 181 return EXTENSION_APP_JS; | 179 return EXTENSION_APP_JS; |
| 182 } | 180 } |
| 183 } | 181 } |
| OLD | NEW |