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.html_codegen; | 5 library dev_compiler.src.codegen.html_codegen; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 | 8 |
9 import 'package:html5lib/dom.dart'; | 9 import 'package:html5lib/dom.dart'; |
10 import 'package:html5lib/parser.dart' show parseFragment; | 10 import 'package:html5lib/parser.dart' show parseFragment; |
11 import 'package:path/path.dart' as path; | 11 import 'package:path/path.dart' as path; |
12 | 12 |
13 import 'package:dev_compiler/src/info.dart'; | 13 import 'package:dev_compiler/src/info.dart'; |
14 import 'package:dev_compiler/src/options.dart'; | 14 import 'package:dev_compiler/src/options.dart'; |
15 | 15 |
16 import 'js_codegen.dart' show jsLibraryName, jsOutputPath; | 16 import 'js_codegen.dart' show jsLibraryName, jsOutputPath; |
17 | 17 |
18 /// Emits an entry point HTML file corresponding to [inputFile] that can load | 18 /// Emits an entry point HTML file corresponding to [inputFile] that can load |
19 /// the code generated by the ddc compiler. | 19 /// the code generated by the dev compiler. |
20 /// | 20 /// |
21 /// This internally transforms the given HTML [document]. When compiling to | 21 /// This internally transforms the given HTML [document]. When compiling to |
22 /// JavaScript, we remove any Dart script tags, add new script tags to load the | 22 /// JavaScript, we remove any Dart script tags, add new script tags to load our |
23 /// ddc runtime and the compiled code, and to execute the main method of the | 23 /// runtime and the compiled code, and to execute the main method of the |
24 /// application. When compiling to Dart, we ensure that the document contains a | 24 /// application. When compiling to Dart, we ensure that the document contains a |
25 /// single Dart script tag, but otherwise emit the original document | 25 /// single Dart script tag, but otherwise emit the original document |
26 /// unmodified. | 26 /// unmodified. |
27 void generateEntryHtml(String inputFile, CompilerOptions options, | 27 void generateEntryHtml(String inputFile, CompilerOptions options, |
28 CheckerResults results, Document document) { | 28 CheckerResults results, Document document) { |
29 String outputFile = path.join(options.outputDir, path.basename(inputFile)); | 29 String outputFile = path.join(options.outputDir, path.basename(inputFile)); |
30 | 30 |
31 var scripts = document.querySelectorAll('script[type="application/dart"]'); | 31 var scripts = document.querySelectorAll('script[type="application/dart"]'); |
32 var mainScript = scripts[0]; | 32 var mainScript = scripts[0]; |
33 // TODO(sigmund): allow more than one Dart script tags? | 33 // TODO(sigmund): allow more than one Dart script tags? |
(...skipping 14 matching lines...) Expand all Loading... |
48 if (lib.isEntry) mainLibraryName = jsLibraryName(lib.library); | 48 if (lib.isEntry) mainLibraryName = jsLibraryName(lib.library); |
49 fragment.nodes.add(_libraryInclude(jsOutputPath(lib, root))); | 49 fragment.nodes.add(_libraryInclude(jsOutputPath(lib, root))); |
50 } | 50 } |
51 fragment.nodes.add(_invokeMain(mainLibraryName)); | 51 fragment.nodes.add(_invokeMain(mainLibraryName)); |
52 mainScript.replaceWith(fragment); | 52 mainScript.replaceWith(fragment); |
53 new File(outputFile).writeAsStringSync('${document.outerHtml}\n'); | 53 new File(outputFile).writeAsStringSync('${document.outerHtml}\n'); |
54 } | 54 } |
55 | 55 |
56 /// A document fragment with scripts that check for harmony features and that | 56 /// A document fragment with scripts that check for harmony features and that |
57 /// inject our runtime. | 57 /// inject our runtime. |
58 Node _loadRuntimeScripts(String ddcLibDir) => parseFragment(''' | 58 Node _loadRuntimeScripts(String libDir) => parseFragment(''' |
59 <script src="$ddcLibDir/runtime/harmony_feature_check.js"></script> | 59 <script src="$libDir/runtime/harmony_feature_check.js"></script> |
60 <script src="$ddcLibDir/runtime/dart_runtime.js"></script> | 60 <script src="$libDir/runtime/dart_runtime.js"></script> |
61 '''); | 61 '''); |
62 | 62 |
63 /// A script tag that loads the .js code for a compiled library. | 63 /// A script tag that loads the .js code for a compiled library. |
64 Node _libraryInclude(String jsUrl) => | 64 Node _libraryInclude(String jsUrl) => |
65 parseFragment('<script src="$jsUrl"></script>\n'); | 65 parseFragment('<script src="$jsUrl"></script>\n'); |
66 | 66 |
67 /// A script tag that invokes the main function on the entry point library. | 67 /// A script tag that invokes the main function on the entry point library. |
68 Node _invokeMain(String mainLibraryName) => | 68 Node _invokeMain(String mainLibraryName) => |
69 parseFragment('<script>$mainLibraryName.main();</script>\n'); | 69 parseFragment('<script>$mainLibraryName.main();</script>\n'); |
70 | 70 |
71 /// A script tag with a tiny mock of the core SDK. This is just used for testing | 71 /// A script tag with a tiny mock of the core SDK. This is just used for testing |
72 /// some small samples. | 72 /// some small samples. |
73 // TODO(sigmund,jmesserly): remove. | 73 // TODO(sigmund,jmesserly): remove. |
74 Node get _miniMockSdk => parseFragment(''' | 74 Node get _miniMockSdk => parseFragment(''' |
75 <script> | 75 <script> |
76 /* placehorder for unimplemented code libraries */ | 76 /* placehorder for unimplemented code libraries */ |
77 var math = Math; | 77 var math = Math; |
78 var core = { int: { parse: Number }, print: e => console.log(e) }; | 78 var core = { int: { parse: Number }, print: e => console.log(e) }; |
79 var dom = { document: document }; | 79 var dom = { document: document }; |
80 </script>'''); | 80 </script>'''); |
OLD | NEW |