| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'dart:collection' show HashSet, Queue; | 5 import 'dart:collection' show HashSet, Queue; |
| 6 import 'dart:convert' show BASE64, JSON, UTF8; | 6 import 'dart:convert' show BASE64, JSON, UTF8; |
| 7 import 'dart:io' show File; | 7 import 'dart:io' show File; |
| 8 | 8 |
| 9 import 'package:analyzer/analyzer.dart' | 9 import 'package:analyzer/analyzer.dart' |
| 10 show AnalysisError, CompilationUnit, ErrorSeverity; | 10 show AnalysisError, CompilationUnit, ErrorSeverity; |
| (...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 /// module. | 436 /// module. |
| 437 final JS.Program moduleTree; | 437 final JS.Program moduleTree; |
| 438 | 438 |
| 439 /// The compiler options used to generate this module. | 439 /// The compiler options used to generate this module. |
| 440 final CompilerOptions options; | 440 final CompilerOptions options; |
| 441 | 441 |
| 442 /// The binary contents of the API summary file, including APIs from each of | 442 /// The binary contents of the API summary file, including APIs from each of |
| 443 /// the libraries in this module. | 443 /// the libraries in this module. |
| 444 final List<int> summaryBytes; | 444 final List<int> summaryBytes; |
| 445 | 445 |
| 446 /// Unique identifier indicating hole to inline the source map. |
| 447 /// |
| 448 /// We cannot generate the source map before the script it is for is |
| 449 /// generated so we have generate the script including this id and then |
| 450 /// replace the ID once the source map is generated. |
| 451 static String sourceMapHoleID = 'SourceMap3G5a8h6JVhHfdGuDxZr1EF9GQC8y0e6u'; |
| 452 |
| 446 JSModuleFile( | 453 JSModuleFile( |
| 447 this.name, this.errors, this.options, this.moduleTree, this.summaryBytes); | 454 this.name, this.errors, this.options, this.moduleTree, this.summaryBytes); |
| 448 | 455 |
| 449 JSModuleFile.invalid(this.name, this.errors, this.options) | 456 JSModuleFile.invalid(this.name, this.errors, this.options) |
| 450 : moduleTree = null, | 457 : moduleTree = null, |
| 451 summaryBytes = null; | 458 summaryBytes = null; |
| 452 | 459 |
| 453 /// True if this library was successfully compiled. | 460 /// True if this library was successfully compiled. |
| 454 bool get isValid => moduleTree != null; | 461 bool get isValid => moduleTree != null; |
| 455 | 462 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 478 | 485 |
| 479 var tree = | 486 var tree = |
| 480 transformModuleFormat(format, moduleTree, singleOutFile: singleOutFile); | 487 transformModuleFormat(format, moduleTree, singleOutFile: singleOutFile); |
| 481 tree.accept( | 488 tree.accept( |
| 482 new JS.Printer(opts, printer, localNamer: new JS.TemporaryNamer(tree))); | 489 new JS.Printer(opts, printer, localNamer: new JS.TemporaryNamer(tree))); |
| 483 | 490 |
| 484 Map builtMap; | 491 Map builtMap; |
| 485 if (options.sourceMap && sourceMap != null) { | 492 if (options.sourceMap && sourceMap != null) { |
| 486 builtMap = | 493 builtMap = |
| 487 placeSourceMap(sourceMap.build(jsUrl), mapUrl, options.bazelMapping); | 494 placeSourceMap(sourceMap.build(jsUrl), mapUrl, options.bazelMapping); |
| 488 | 495 if (name == 'dart_sdk') { |
| 496 builtMap = cleanupSdkSourcemap(builtMap); |
| 497 } |
| 489 if (options.sourceMapComment) { | 498 if (options.sourceMapComment) { |
| 490 var relativeMapUrl = path | 499 var relativeMapUrl = path |
| 491 .toUri( | 500 .toUri( |
| 492 path.relative(path.fromUri(mapUrl), from: path.dirname(jsUrl))) | 501 path.relative(path.fromUri(mapUrl), from: path.dirname(jsUrl))) |
| 493 .toString(); | 502 .toString(); |
| 494 assert(path.dirname(jsUrl) == path.dirname(mapUrl)); | 503 assert(path.dirname(jsUrl) == path.dirname(mapUrl)); |
| 495 printer.emit('\n//# sourceMappingURL='); | 504 printer.emit('\n//# sourceMappingURL='); |
| 496 if (options.inlineSourceMap) { | 505 printer.emit(relativeMapUrl); |
| 497 var bytes = UTF8.encode(JSON.encode(builtMap)); | |
| 498 var base64 = BASE64.encode(bytes); | |
| 499 printer..emit('data:application/json;base64,')..emit(base64); | |
| 500 } else { | |
| 501 printer.emit(relativeMapUrl); | |
| 502 } | |
| 503 printer.emit('\n'); | 506 printer.emit('\n'); |
| 504 } | 507 } |
| 505 } | 508 } |
| 506 | 509 |
| 507 return new JSModuleCode(printer.getText(), builtMap); | 510 var text = printer.getText(); |
| 511 var rawSourceMap = options.inlineSourceMap ? JSON.encode(builtMap) : null; |
| 512 // Encode the sourcemap as an escaped string rather than JSON |
| 513 // as Dart code using the sourcemap can't take advantage of it being JS |
| 514 // JSON so we might as well encode it as a String which should be quicker |
| 515 // to parse. |
| 516 text = text.replaceFirst(sourceMapHoleID, JSON.encode(rawSourceMap)); |
| 517 |
| 518 return new JSModuleCode(text, builtMap); |
| 508 } | 519 } |
| 509 | 520 |
| 510 /// Similar to [getCode] but immediately writes the resulting files. | 521 /// Similar to [getCode] but immediately writes the resulting files. |
| 511 /// | 522 /// |
| 512 /// If [mapPath] is not supplied but [options.sourceMap] is set, mapPath | 523 /// If [mapPath] is not supplied but [options.sourceMap] is set, mapPath |
| 513 /// will default to [jsPath].map. | 524 /// will default to [jsPath].map. |
| 514 void writeCodeSync(ModuleFormat format, String jsPath, | 525 void writeCodeSync(ModuleFormat format, String jsPath, |
| 515 {bool singleOutFile: false}) { | 526 {bool singleOutFile: false}) { |
| 516 String mapPath = jsPath + '.map'; | 527 String mapPath = jsPath + '.map'; |
| 517 var code = getCode(format, jsPath, mapPath, singleOutFile: singleOutFile); | 528 var code = getCode(format, jsPath, mapPath, singleOutFile: singleOutFile); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 577 // Fall back to a relative path. | 588 // Fall back to a relative path. |
| 578 return path.toUri(path.relative(path.fromUri(uri), from: dir)).toString(); | 589 return path.toUri(path.relative(path.fromUri(uri), from: dir)).toString(); |
| 579 } | 590 } |
| 580 | 591 |
| 581 for (int i = 0; i < list.length; i++) { | 592 for (int i = 0; i < list.length; i++) { |
| 582 list[i] = transformUri(list[i]); | 593 list[i] = transformUri(list[i]); |
| 583 } | 594 } |
| 584 map['file'] = transformUri(map['file']); | 595 map['file'] = transformUri(map['file']); |
| 585 return map; | 596 return map; |
| 586 } | 597 } |
| 598 |
| 599 /// Cleanup the dart_sdk source map. |
| 600 /// |
| 601 /// Strip out files that should not be included in the sdk sourcemap as they |
| 602 /// are implementation details that would just confuse users. |
| 603 /// Normalize sdk urls to use "dart:" for more understandable stack traces. |
| 604 Map cleanupSdkSourcemap(Map sourceMap) { |
| 605 var map = new Map.from(sourceMap); |
| 606 var list = new List.from(map['sources']); |
| 607 map['sources'] = map['sources'] |
| 608 .map((url) => url.contains('/_internal/') ? null : url) |
| 609 .toList(); |
| 610 return map; |
| 611 } |
| OLD | NEW |