OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 dev_compiler.src.codegen.js_printer; | 5 library dev_compiler.src.codegen.js_printer; |
6 | 6 |
7 import 'dart:io' show Directory, File; | 7 import 'dart:io' show Directory, File; |
8 import 'package:analyzer/src/generated/ast.dart'; | 8 import 'package:analyzer/src/generated/ast.dart'; |
9 import 'package:path/path.dart' as path; | 9 import 'package:path/path.dart' as path; |
10 import 'package:source_maps/source_maps.dart' as srcmaps show Printer; | 10 import 'package:source_maps/source_maps.dart' as srcmaps show Printer; |
11 import 'package:source_maps/source_maps.dart' show SourceMapSpan; | 11 import 'package:source_maps/source_maps.dart' show SourceMapSpan; |
12 import 'package:source_span/source_span.dart' show SourceLocation; | 12 import 'package:source_span/source_span.dart' show SourceLocation; |
13 | 13 |
14 import 'package:dev_compiler/src/js/js_ast.dart' as JS; | 14 import 'package:dev_compiler/src/js/js_ast.dart' as JS; |
15 import 'package:dev_compiler/src/utils.dart' | 15 import 'package:dev_compiler/src/utils.dart' |
16 show computeHash, locationForOffset; | 16 show computeHash, locationForOffset; |
17 | 17 |
18 import 'js_names.dart' show TemporaryNamer; | 18 import 'js_names.dart' show TemporaryNamer; |
19 | 19 |
20 String writeJsLibrary(JS.Program jsTree, String outputPath, | 20 String writeJsLibrary(JS.Program jsTree, String outputPath, |
21 {bool emitSourceMaps: false}) { | 21 {bool emitSourceMaps: false, bool arrowFnBindThisWorkaround: false}) { |
22 new Directory(path.dirname(outputPath)).createSync(recursive: true); | 22 var outFilename = path.basename(outputPath); |
| 23 var outDir = path.dirname(outputPath); |
| 24 new Directory(outDir).createSync(recursive: true); |
| 25 |
| 26 JS.JavaScriptPrintingContext context; |
23 if (emitSourceMaps) { | 27 if (emitSourceMaps) { |
24 var outFilename = path.basename(outputPath); | |
25 var printer = new srcmaps.Printer(outFilename); | 28 var printer = new srcmaps.Printer(outFilename); |
26 writeNodeToContext(jsTree, | 29 context = new SourceMapPrintingContext(printer, outDir); |
27 new SourceMapPrintingContext(printer, path.dirname(outputPath))); | 30 } else { |
| 31 context = new JS.SimpleJavaScriptPrintingContext(); |
| 32 } |
| 33 |
| 34 var opts = new JS.JavaScriptPrintingOptions( |
| 35 allowKeywordsInProperties: true, |
| 36 arrowFnBindThisWorkaround: arrowFnBindThisWorkaround); |
| 37 var jsNamer = new TemporaryNamer(jsTree); |
| 38 jsTree.accept(new JS.Printer(opts, context, localNamer: jsNamer)); |
| 39 |
| 40 String text; |
| 41 if (context is SourceMapPrintingContext) { |
| 42 var printer = context.printer; |
28 printer.add('//# sourceMappingURL=$outFilename.map'); | 43 printer.add('//# sourceMappingURL=$outFilename.map'); |
29 // Write output file and source map | 44 // Write output file and source map |
30 var text = printer.text; | 45 text = printer.text; |
31 new File(outputPath).writeAsStringSync(text); | |
32 new File('$outputPath.map').writeAsStringSync(printer.map); | 46 new File('$outputPath.map').writeAsStringSync(printer.map); |
33 return computeHash(text); | |
34 } else { | 47 } else { |
35 var text = jsNodeToString(jsTree); | 48 text = (context as JS.SimpleJavaScriptPrintingContext).getText(); |
36 new File(outputPath).writeAsStringSync(text); | |
37 return computeHash(text); | |
38 } | 49 } |
39 } | 50 new File(outputPath).writeAsStringSync(text); |
40 | 51 return computeHash(text); |
41 void writeNodeToContext(JS.Node node, JS.JavaScriptPrintingContext context) { | |
42 var opts = new JS.JavaScriptPrintingOptions(allowKeywordsInProperties: true); | |
43 node.accept( | |
44 new JS.Printer(opts, context, localNamer: new TemporaryNamer(node))); | |
45 } | |
46 | |
47 String jsNodeToString(JS.Node node) { | |
48 var context = new JS.SimpleJavaScriptPrintingContext(); | |
49 writeNodeToContext(node, context); | |
50 return context.getText(); | |
51 } | 52 } |
52 | 53 |
53 class SourceMapPrintingContext extends JS.JavaScriptPrintingContext { | 54 class SourceMapPrintingContext extends JS.JavaScriptPrintingContext { |
54 final srcmaps.Printer printer; | 55 final srcmaps.Printer printer; |
55 final String outputDir; | 56 final String outputDir; |
56 | 57 |
57 CompilationUnit unit; | 58 CompilationUnit unit; |
58 Uri uri; | 59 Uri uri; |
59 | 60 |
60 SourceMapPrintingContext(this.printer, this.outputDir); | 61 SourceMapPrintingContext(this.printer, this.outputDir); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 103 |
103 // TODO(jmesserly): in many cases marking the end will be unnecessary. | 104 // TODO(jmesserly): in many cases marking the end will be unnecessary. |
104 printer.mark(_location(node.end)); | 105 printer.mark(_location(node.end)); |
105 } | 106 } |
106 | 107 |
107 String _getIdentifier(AstNode node) { | 108 String _getIdentifier(AstNode node) { |
108 if (node is SimpleIdentifier) return node.name; | 109 if (node is SimpleIdentifier) return node.name; |
109 return null; | 110 return null; |
110 } | 111 } |
111 } | 112 } |
OLD | NEW |