Chromium Code Reviews| 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 ddc.src.codegen.js_codegen; | 5 library ddc.src.codegen.js_codegen; |
| 6 | 6 |
| 7 import 'dart:io' show Directory, File; | 7 import 'dart:io' show Directory, File; |
| 8 | 8 |
| 9 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; | 9 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; |
| 10 import 'package:analyzer/src/generated/ast.dart' hide ConstantEvaluator; | 10 import 'package:analyzer/src/generated/ast.dart' hide ConstantEvaluator; |
| (...skipping 1698 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1709 final JSCodeOptions options; | 1709 final JSCodeOptions options; |
| 1710 | 1710 |
| 1711 JSGenerator(String outDir, Uri root, TypeRules rules, this.options) | 1711 JSGenerator(String outDir, Uri root, TypeRules rules, this.options) |
| 1712 : super(outDir, root, rules); | 1712 : super(outDir, root, rules); |
| 1713 | 1713 |
| 1714 void generateLibrary(Iterable<CompilationUnit> units, LibraryInfo info, | 1714 void generateLibrary(Iterable<CompilationUnit> units, LibraryInfo info, |
| 1715 CheckerReporter reporter) { | 1715 CheckerReporter reporter) { |
| 1716 JS.Block jsTree = | 1716 JS.Block jsTree = |
| 1717 new JSCodegenVisitor(info, rules).generateLibrary(units, reporter); | 1717 new JSCodegenVisitor(info, rules).generateLibrary(units, reporter); |
| 1718 | 1718 |
| 1719 var outputPath = path.join(outDir, jsOutputPath(info)); | 1719 var outputPath = path.join(outDir, jsOutputPath(info, root)); |
| 1720 new Directory(path.dirname(outputPath)).createSync(recursive: true); | 1720 new Directory(path.dirname(outputPath)).createSync(recursive: true); |
| 1721 | 1721 |
| 1722 if (options.emitSourceMaps) { | 1722 if (options.emitSourceMaps) { |
| 1723 var outFilename = path.basename(outputPath); | 1723 var outFilename = path.basename(outputPath); |
| 1724 var printer = new srcmaps.Printer(outFilename); | 1724 var printer = new srcmaps.Printer(outFilename); |
| 1725 var context = | 1725 var context = |
| 1726 new SourceMapPrintingContext(printer, path.dirname(outputPath)); | 1726 new SourceMapPrintingContext(printer, path.dirname(outputPath)); |
| 1727 _writeLibrary(context, jsTree); | 1727 _writeLibrary(context, jsTree); |
| 1728 printer.add('//# sourceMappingURL=$outFilename.map'); | 1728 printer.add('//# sourceMappingURL=$outFilename.map'); |
| 1729 // Write output file and source map | 1729 // Write output file and source map |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 1751 new JS.Printer(opts, context).visit(node); | 1751 new JS.Printer(opts, context).visit(node); |
| 1752 // Write output file and source map | 1752 // Write output file and source map |
| 1753 return context.getText(); | 1753 return context.getText(); |
| 1754 } | 1754 } |
| 1755 | 1755 |
| 1756 /// Choose a canonical name from the library element. | 1756 /// Choose a canonical name from the library element. |
| 1757 /// This never uses the library's name (the identifier in the `library` | 1757 /// This never uses the library's name (the identifier in the `library` |
| 1758 /// declaration) as it doesn't have any meaningful rules enforced. | 1758 /// declaration) as it doesn't have any meaningful rules enforced. |
| 1759 String jsLibraryName(LibraryElement library) => canonicalLibraryName(library); | 1759 String jsLibraryName(LibraryElement library) => canonicalLibraryName(library); |
| 1760 | 1760 |
| 1761 /// Path to file that will be generated for [info]. | 1761 /// Path to file that will be generated for [info]. In case it's url is a |
| 1762 // TODO(jmesserly): library directory should be relative to its package | 1762 /// `file:` url, we use [root] to determine the relative path from the entry |
| 1763 // root. For example, "package:dev_compiler/src/codegen/js_codegen.dart" would b e: | 1763 /// point file. |
| 1764 // "ddc/src/codegen/js_codegen.js" under the output directory. | 1764 String jsOutputPath(LibraryInfo info, Uri root) { |
| 1765 String jsOutputPath(LibraryInfo info) => '${info.name}/${info.name}.js'; | 1765 var uri = info.library.source.uri; |
| 1766 var filepath = '${path.withoutExtension(uri.path)}.js'; | |
| 1767 if (uri.scheme == 'dart') { | |
| 1768 filepath = 'dart/$filepath'; | |
| 1769 } else if (uri.scheme == 'package') { | |
| 1770 filepath = 'packages/$filepath'; | |
|
Jennifer Messerly
2015/03/02 18:40:51
hmmm. I wonder about this. Does it make sense in a
Siggi Cherem (dart-lang)
2015/03/02 18:57:29
Good point - this also brings a related question a
Siggi Cherem (dart-lang)
2015/03/03 02:09:03
Done. I updated the CL to match Option A.
| |
| 1771 } else if (uri.scheme == 'file') { | |
| 1772 filepath = path.relative(filepath, from: path.dirname(root.path)); | |
| 1773 } | |
| 1774 return filepath; | |
| 1775 } | |
| 1766 | 1776 |
| 1767 class SourceMapPrintingContext extends JS.JavaScriptPrintingContext { | 1777 class SourceMapPrintingContext extends JS.JavaScriptPrintingContext { |
| 1768 final srcmaps.Printer printer; | 1778 final srcmaps.Printer printer; |
| 1769 final String outputDir; | 1779 final String outputDir; |
| 1770 | 1780 |
| 1771 CompilationUnit unit; | 1781 CompilationUnit unit; |
| 1772 Uri uri; | 1782 Uri uri; |
| 1773 | 1783 |
| 1774 SourceMapPrintingContext(this.printer, this.outputDir); | 1784 SourceMapPrintingContext(this.printer, this.outputDir); |
| 1775 | 1785 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1821 | 1831 |
| 1822 // TODO(jmesserly): in many cases marking the end will be unncessary. | 1832 // TODO(jmesserly): in many cases marking the end will be unncessary. |
| 1823 printer.mark(_location(node.end)); | 1833 printer.mark(_location(node.end)); |
| 1824 } | 1834 } |
| 1825 | 1835 |
| 1826 String _getIdentifier(AstNode node) { | 1836 String _getIdentifier(AstNode node) { |
| 1827 if (node is SimpleIdentifier) return node.name; | 1837 if (node is SimpleIdentifier) return node.name; |
| 1828 return null; | 1838 return null; |
| 1829 } | 1839 } |
| 1830 } | 1840 } |
| OLD | NEW |