Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(320)

Side by Side Diff: pkg/dev_compiler/lib/src/compiler/compiler.dart

Issue 2747513003: Inline source maps as part of each script. (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 used to location where to insline the source map
vsm 2017/03/13 15:25:28 insline -> inline
Jacob 2017/03/13 15:51:12 Done.
447 /// in an existing generated JS file
448 ///
449 /// Due to execution order constriants we have cannot generate the source map
vsm 2017/03/13 15:25:27 constriants -> constraints
Jacob 2017/03/13 15:51:12 cleaned up this comment.
450 /// by the time we would need it to insert the sourcemap directly so instead
451 /// we have to generate the JS with this ID and then replace the ID with the
452 /// actual sourcemap after the fact.
453 static String sourceMapHoleID = 'SourceMap3G5a8h6JVhHfdGuDxZr1EF9GQC8y0e6u';
454
446 JSModuleFile( 455 JSModuleFile(
447 this.name, this.errors, this.options, this.moduleTree, this.summaryBytes); 456 this.name, this.errors, this.options, this.moduleTree, this.summaryBytes);
448 457
449 JSModuleFile.invalid(this.name, this.errors, this.options) 458 JSModuleFile.invalid(this.name, this.errors, this.options)
450 : moduleTree = null, 459 : moduleTree = null,
451 summaryBytes = null; 460 summaryBytes = null;
452 461
453 /// True if this library was successfully compiled. 462 /// True if this library was successfully compiled.
454 bool get isValid => moduleTree != null; 463 bool get isValid => moduleTree != null;
455 464
(...skipping 22 matching lines...) Expand all
478 487
479 var tree = 488 var tree =
480 transformModuleFormat(format, moduleTree, singleOutFile: singleOutFile); 489 transformModuleFormat(format, moduleTree, singleOutFile: singleOutFile);
481 tree.accept( 490 tree.accept(
482 new JS.Printer(opts, printer, localNamer: new JS.TemporaryNamer(tree))); 491 new JS.Printer(opts, printer, localNamer: new JS.TemporaryNamer(tree)));
483 492
484 Map builtMap; 493 Map builtMap;
485 if (options.sourceMap && sourceMap != null) { 494 if (options.sourceMap && sourceMap != null) {
486 builtMap = 495 builtMap =
487 placeSourceMap(sourceMap.build(jsUrl), mapUrl, options.bazelMapping); 496 placeSourceMap(sourceMap.build(jsUrl), mapUrl, options.bazelMapping);
488 497 if (name == 'dart_sdk') {
498 builtMap = cleanupSdkSourcemap(builtMap);
499 }
489 if (options.sourceMapComment) { 500 if (options.sourceMapComment) {
490 var relativeMapUrl = path 501 var relativeMapUrl = path
491 .toUri( 502 .toUri(
492 path.relative(path.fromUri(mapUrl), from: path.dirname(jsUrl))) 503 path.relative(path.fromUri(mapUrl), from: path.dirname(jsUrl)))
493 .toString(); 504 .toString();
494 assert(path.dirname(jsUrl) == path.dirname(mapUrl)); 505 assert(path.dirname(jsUrl) == path.dirname(mapUrl));
495 printer.emit('\n//# sourceMappingURL='); 506 printer.emit('\n//# sourceMappingURL=');
496 if (options.inlineSourceMap) { 507 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'); 508 printer.emit('\n');
504 } 509 }
505 } 510 }
506 511
507 return new JSModuleCode(printer.getText(), builtMap); 512 var text = printer.getText();
513 var rawSourceMap = options.inlineSourceMap ? JSON.encode(builtMap) : null;
514 // Encode the sourcemap as an escaped string rather than JSON
515 // as Dart code using the sourcemap can't take advantage of it being JS
516 // JSON so we might as well encode it as a String which should be quicker
517 // to parse.
518 text = text.replaceFirst(sourceMapHoleID, JSON.encode(rawSourceMap));
vsm 2017/03/13 15:25:27 So, are we sending the source map twice now? Once
Jacob 2017/03/13 15:51:12 We are encoding the source map in the source file
519
520 return new JSModuleCode(text, builtMap);
508 } 521 }
509 522
510 /// Similar to [getCode] but immediately writes the resulting files. 523 /// Similar to [getCode] but immediately writes the resulting files.
511 /// 524 ///
512 /// If [mapPath] is not supplied but [options.sourceMap] is set, mapPath 525 /// If [mapPath] is not supplied but [options.sourceMap] is set, mapPath
513 /// will default to [jsPath].map. 526 /// will default to [jsPath].map.
514 void writeCodeSync(ModuleFormat format, String jsPath, 527 void writeCodeSync(ModuleFormat format, String jsPath,
515 {bool singleOutFile: false}) { 528 {bool singleOutFile: false}) {
516 String mapPath = jsPath + '.map'; 529 String mapPath = jsPath + '.map';
517 var code = getCode(format, jsPath, mapPath, singleOutFile: singleOutFile); 530 var code = getCode(format, jsPath, mapPath, singleOutFile: singleOutFile);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
577 // Fall back to a relative path. 590 // Fall back to a relative path.
578 return path.toUri(path.relative(path.fromUri(uri), from: dir)).toString(); 591 return path.toUri(path.relative(path.fromUri(uri), from: dir)).toString();
579 } 592 }
580 593
581 for (int i = 0; i < list.length; i++) { 594 for (int i = 0; i < list.length; i++) {
582 list[i] = transformUri(list[i]); 595 list[i] = transformUri(list[i]);
583 } 596 }
584 map['file'] = transformUri(map['file']); 597 map['file'] = transformUri(map['file']);
585 return map; 598 return map;
586 } 599 }
600
601 /// Cleanup the dart_sdk source map.
602 ///
603 /// Strip out files that should not be included in the sdk sourcemap as they
604 /// are implementation details that would just confuse users.
605 /// Normalize sdk urls to use "dart:" for more understandable stack traces.
606 Map cleanupSdkSourcemap(Map sourceMap) {
607 var map = new Map.from(sourceMap);
608 var list = new List.from(map['sources']);
609 map['sources'] = list;
610 for (var i = 0; i < list.length; ++i) {
611 var url = list[i];
612 if (url.contains('/js_lib/')) {
vsm 2017/03/13 15:25:28 Perhaps everything under _internal?
Jacob 2017/03/13 15:51:13 Done.
613 list[i] = null;
614 } else {
615 // TODO(jacobr): use a cleaner way to normalize dart: urls.
vsm 2017/03/13 15:25:28 Yeah, should be able to plumb the analyzer normali
Jacob 2017/03/13 15:51:12 I think these relative urls are technically correc
616 list[i] = url.replaceFirst('../../../gen/patched_sdk/lib/', 'dart:');
617 }
618 }
619 return map;
620 }
OLDNEW
« no previous file with comments | « pkg/dev_compiler/lib/src/compiler/code_generator.dart ('k') | pkg/dev_compiler/test/codegen_expected/BenchmarkBase.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698