OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library pub.dart2js_transformer; | 5 library pub.dart2js_transformer; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 | 10 |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 } | 178 } |
179 } | 179 } |
180 | 180 |
181 /// Defines an interface for dart2js to communicate with barback and pub. | 181 /// Defines an interface for dart2js to communicate with barback and pub. |
182 /// | 182 /// |
183 /// Note that most of the implementation of diagnostic handling here was | 183 /// Note that most of the implementation of diagnostic handling here was |
184 /// copied from [FormattingDiagnosticHandler] in dart2js. The primary | 184 /// copied from [FormattingDiagnosticHandler] in dart2js. The primary |
185 /// difference is that it uses barback's logging code and, more importantly, it | 185 /// difference is that it uses barback's logging code and, more importantly, it |
186 /// handles missing source files more gracefully. | 186 /// handles missing source files more gracefully. |
187 class _BarbackCompilerProvider implements dart.CompilerProvider { | 187 class _BarbackCompilerProvider implements dart.CompilerProvider { |
188 Uri get libraryRoot => Uri.parse("${path.toUri(_libraryRootPath)}/"); | |
189 | |
188 final BuildEnvironment _environment; | 190 final BuildEnvironment _environment; |
189 final Transform _transform; | 191 final Transform _transform; |
192 String _libraryRootPath; | |
190 | 193 |
191 /// The map of previously loaded files. | 194 /// The map of previously loaded files. |
192 /// | 195 /// |
193 /// Used to show where an error occurred in a source file. | 196 /// Used to show where an error occurred in a source file. |
194 final _sourceFiles = new Map<String, SourceFile>(); | 197 final _sourceFiles = new Map<String, SourceFile>(); |
195 | 198 |
196 // TODO(rnystrom): Make these configurable. | 199 // TODO(rnystrom): Make these configurable. |
197 /// Whether or not warnings should be logged. | 200 /// Whether or not warnings should be logged. |
198 var _showWarnings = true; | 201 var _showWarnings = true; |
199 | 202 |
(...skipping 15 matching lines...) Expand all Loading... | |
215 compiler.Diagnostic _lastKind = null; | 218 compiler.Diagnostic _lastKind = null; |
216 | 219 |
217 static final int _FATAL = | 220 static final int _FATAL = |
218 compiler.Diagnostic.CRASH.ordinal | | 221 compiler.Diagnostic.CRASH.ordinal | |
219 compiler.Diagnostic.ERROR.ordinal; | 222 compiler.Diagnostic.ERROR.ordinal; |
220 static final int _INFO = | 223 static final int _INFO = |
221 compiler.Diagnostic.INFO.ordinal | | 224 compiler.Diagnostic.INFO.ordinal | |
222 compiler.Diagnostic.VERBOSE_INFO.ordinal; | 225 compiler.Diagnostic.VERBOSE_INFO.ordinal; |
223 | 226 |
224 _BarbackCompilerProvider(this._environment, this._transform, | 227 _BarbackCompilerProvider(this._environment, this._transform, |
225 {this.generateSourceMaps: true}); | 228 {this.generateSourceMaps: true}) { |
229 // Dart2js outputs source maps that reference the Dart SDK sources. For | |
230 // that to work, those sources need to be inside the build environment. We | |
231 // do that by placing them in a special "$sdk" pseudo-package. In order for | |
232 // dart2js to generate the right URLs to point to that package, we give it | |
233 // a library root that corresponds to where that package can be found | |
234 // relative to the public build directory containing that entrypoint. | |
235 // | |
236 // For example, say the package being compiled is "/dev/myapp", the | |
237 // entrypoint is "web/sub/foo/bar.dart", and the build directory is | |
238 // "web/sub". This means the SDK sources will be (conceptually) at: | |
239 // | |
240 // /dev/myapp/web/sub/packages/$sdk/lib/ | |
241 // | |
242 // This implies that the asset path for a file in the SDK is: | |
243 // | |
244 // $sdk|lib/lib/... | |
245 // | |
246 // TODO(rnystrom): Fix this if $17751 is fixed. | |
nweiz
2014/03/24 23:17:14
"$" -> "#"
Bob Nystrom
2014/03/25 00:49:27
Done.
| |
247 var buildDir = _environment.getBuildDirectoryContaining( | |
248 _transform.primaryInput.id.path); | |
249 _libraryRootPath = path.join(_environment.rootPackage.dir, | |
250 buildDir, "packages", r"$sdk"); | |
251 } | |
226 | 252 |
227 /// A [CompilerInputProvider] for dart2js. | 253 /// A [CompilerInputProvider] for dart2js. |
228 Future<String> provideInput(Uri resourceUri) { | 254 Future<String> provideInput(Uri resourceUri) { |
229 // We only expect to get absolute "file:" URLs from dart2js. | 255 // We only expect to get absolute "file:" URLs from dart2js. |
230 assert(resourceUri.isAbsolute); | 256 assert(resourceUri.isAbsolute); |
231 assert(resourceUri.scheme == "file"); | 257 assert(resourceUri.scheme == "file"); |
232 | 258 |
233 var sourcePath = path.fromUri(resourceUri); | 259 var sourcePath = path.fromUri(resourceUri); |
234 return _readResource(resourceUri).then((source) { | 260 return _readResource(resourceUri).then((source) { |
235 _sourceFiles[resourceUri.toString()] = | 261 _sourceFiles[resourceUri.toString()] = |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
322 } | 348 } |
323 } | 349 } |
324 | 350 |
325 if (fatal && _throwOnError) { | 351 if (fatal && _throwOnError) { |
326 _isAborting = true; | 352 _isAborting = true; |
327 throw new AbortLeg(message); | 353 throw new AbortLeg(message); |
328 } | 354 } |
329 } | 355 } |
330 | 356 |
331 Future<String> _readResource(Uri url) { | 357 Future<String> _readResource(Uri url) { |
332 // See if the path is within a package. If so, use Barback so we can use | 358 // Find the corresponding asset in barback. |
333 // generated Dart assets. | |
334 | |
335 var id = _sourceUrlToId(url); | 359 var id = _sourceUrlToId(url); |
336 if (id != null) return _transform.readInputAsString(id); | 360 if (id != null) return _transform.readInputAsString(id); |
337 | 361 |
338 // If we get here, the path doesn't appear to be in a package, so we'll | 362 // If we get here, the path doesn't appear to be in any package, so we'll |
339 // skip Barback and just hit the file system. This will occur at the very | 363 // skip Barback and just hit the file system. This will only occur for |
340 // least for dart2js's implementations of the core libraries. | 364 // absolute file paths in imports that don't touch a package. |
341 var sourcePath = path.fromUri(url); | 365 var sourcePath = path.fromUri(url); |
342 return Chain.track(new File(sourcePath).readAsString()); | 366 return Chain.track(new File(sourcePath).readAsString()); |
343 } | 367 } |
344 | 368 |
345 AssetId _sourceUrlToId(Uri url) { | 369 AssetId _sourceUrlToId(Uri url) { |
346 // See if it's a special path with "packages" or "assets" in it. | 370 // See if it's a special path with "packages" or "assets" in it. |
347 var id = specialUrlToId(url); | 371 var id = specialUrlToId(url); |
348 if (id != null) return id; | 372 if (id != null) return id; |
349 | 373 |
350 // See if it's a path to a "public" asset within the root package. All | 374 // See if it's a path to a "public" asset within the root package. All |
(...skipping 11 matching lines...) Expand all Loading... | |
362 } | 386 } |
363 } | 387 } |
364 | 388 |
365 /// An [EventSink] that discards all data. Provided to dart2js when we don't | 389 /// An [EventSink] that discards all data. Provided to dart2js when we don't |
366 /// want an actual output. | 390 /// want an actual output. |
367 class NullSink<T> implements EventSink<T> { | 391 class NullSink<T> implements EventSink<T> { |
368 void add(T event) {} | 392 void add(T event) {} |
369 void addError(errorEvent, [StackTrace stackTrace]) {} | 393 void addError(errorEvent, [StackTrace stackTrace]) {} |
370 void close() {} | 394 void close() {} |
371 } | 395 } |
OLD | NEW |