OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library dev_compiler.src.codegen.js_printer; |
| 6 |
| 7 import 'dart:io' show Directory, File; |
| 8 import 'package:analyzer/src/generated/ast.dart'; |
| 9 import 'package:path/path.dart' as path; |
| 10 import 'package:source_maps/source_maps.dart' as srcmaps show Printer; |
| 11 import 'package:source_maps/source_maps.dart' show SourceMapSpan; |
| 12 import 'package:source_span/source_span.dart' show SourceLocation; |
| 13 |
| 14 import 'package:dev_compiler/src/js/js_ast.dart' as JS; |
| 15 import 'package:dev_compiler/src/utils.dart' |
| 16 show computeHash, locationForOffset; |
| 17 |
| 18 import 'js_names.dart' show JSNamer; |
| 19 |
| 20 String writeJsLibrary(JS.Program jsTree, String outputPath, |
| 21 {bool emitSourceMaps: false}) { |
| 22 new Directory(path.dirname(outputPath)).createSync(recursive: true); |
| 23 if (emitSourceMaps) { |
| 24 var outFilename = path.basename(outputPath); |
| 25 var printer = new srcmaps.Printer(outFilename); |
| 26 writeNodeToContext(jsTree, |
| 27 new SourceMapPrintingContext(printer, path.dirname(outputPath))); |
| 28 printer.add('//# sourceMappingURL=$outFilename.map'); |
| 29 // Write output file and source map |
| 30 var text = printer.text; |
| 31 new File(outputPath).writeAsStringSync(text); |
| 32 new File('$outputPath.map').writeAsStringSync(printer.map); |
| 33 return computeHash(text); |
| 34 } else { |
| 35 var text = jsNodeToString(jsTree); |
| 36 new File(outputPath).writeAsStringSync(text); |
| 37 return computeHash(text); |
| 38 } |
| 39 } |
| 40 |
| 41 void writeNodeToContext(JS.Node node, JS.JavaScriptPrintingContext context) { |
| 42 var opts = new JS.JavaScriptPrintingOptions(allowKeywordsInProperties: true); |
| 43 node.accept(new JS.Printer(opts, context, localNamer: new JSNamer(node))); |
| 44 } |
| 45 |
| 46 String jsNodeToString(JS.Node node) { |
| 47 var context = new JS.SimpleJavaScriptPrintingContext(); |
| 48 writeNodeToContext(node, context); |
| 49 return context.getText(); |
| 50 } |
| 51 |
| 52 class SourceMapPrintingContext extends JS.JavaScriptPrintingContext { |
| 53 final srcmaps.Printer printer; |
| 54 final String outputDir; |
| 55 |
| 56 CompilationUnit unit; |
| 57 Uri uri; |
| 58 |
| 59 SourceMapPrintingContext(this.printer, this.outputDir); |
| 60 |
| 61 void emit(String string) { |
| 62 printer.add(string); |
| 63 } |
| 64 |
| 65 void enterNode(JS.Node jsNode) { |
| 66 AstNode node = jsNode.sourceInformation; |
| 67 if (node is CompilationUnit) { |
| 68 unit = node; |
| 69 uri = _makeRelativeUri(unit.element.source.uri); |
| 70 return; |
| 71 } |
| 72 if (unit == null || node == null || node.offset == -1) return; |
| 73 |
| 74 var loc = _location(node.offset); |
| 75 var name = _getIdentifier(node); |
| 76 if (name != null) { |
| 77 // TODO(jmesserly): mark only uses the beginning of the span, but |
| 78 // we're required to pass this as a valid span. |
| 79 var end = _location(node.end); |
| 80 printer.mark(new SourceMapSpan(loc, end, name, isIdentifier: true)); |
| 81 } else { |
| 82 printer.mark(loc); |
| 83 } |
| 84 } |
| 85 |
| 86 SourceLocation _location(int offset) => locationForOffset(unit, uri, offset); |
| 87 |
| 88 Uri _makeRelativeUri(Uri src) { |
| 89 return new Uri(path: path.relative(src.path, from: outputDir)); |
| 90 } |
| 91 |
| 92 void exitNode(JS.Node jsNode) { |
| 93 AstNode node = jsNode.sourceInformation; |
| 94 if (node is CompilationUnit) { |
| 95 unit = null; |
| 96 uri = null; |
| 97 return; |
| 98 } |
| 99 if (unit == null || node == null || node.offset == -1) return; |
| 100 |
| 101 // TODO(jmesserly): in many cases marking the end will be unnecessary. |
| 102 printer.mark(_location(node.end)); |
| 103 } |
| 104 |
| 105 String _getIdentifier(AstNode node) { |
| 106 if (node is SimpleIdentifier) return node.name; |
| 107 return null; |
| 108 } |
| 109 } |
OLD | NEW |