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.html_codegen; | 5 library dev_compiler.src.codegen.html_codegen; |
6 | 6 |
7 import 'package:html5lib/dom.dart'; | 7 import 'package:html5lib/dom.dart'; |
8 import 'package:html5lib/parser.dart' show parseFragment; | 8 import 'package:html5lib/parser.dart' show parseFragment; |
9 import 'package:logging/logging.dart' show Logger; | 9 import 'package:logging/logging.dart' show Logger; |
10 | 10 |
(...skipping 29 matching lines...) Expand all Loading... |
40 }); | 40 }); |
41 | 41 |
42 if (options.outputDart) return '${document.outerHtml}\n'; | 42 if (options.outputDart) return '${document.outerHtml}\n'; |
43 | 43 |
44 var libraries = []; | 44 var libraries = []; |
45 visitInPostOrder(root, (n) { | 45 visitInPostOrder(root, (n) { |
46 if (n is DartSourceNode) libraries.add(n); | 46 if (n is DartSourceNode) libraries.add(n); |
47 }, includeParts: false); | 47 }, includeParts: false); |
48 | 48 |
49 String mainLibraryName; | 49 String mainLibraryName; |
50 var fragment = _loadRuntimeScripts(); | 50 var fragment = _loadRuntimeScripts(options); |
51 if (!options.checkSdk) fragment.nodes.add(_miniMockSdk); | 51 if (!options.checkSdk) fragment.nodes.add(_miniMockSdk); |
52 for (var lib in libraries) { | 52 for (var lib in libraries) { |
53 var info = lib.info; | 53 var info = lib.info; |
54 if (info == null) continue; | 54 if (info == null) continue; |
55 if (info.isEntry) mainLibraryName = jsLibraryName(info.library); | 55 if (info.isEntry) mainLibraryName = jsLibraryName(info.library); |
56 fragment.nodes.add(_libraryInclude(jsOutputPath(info, root.uri))); | 56 fragment.nodes.add(_libraryInclude(jsOutputPath(info, root.uri))); |
57 } | 57 } |
58 fragment.nodes.add(_invokeMain(mainLibraryName)); | 58 fragment.nodes.add(_invokeMain(mainLibraryName)); |
59 scripts[0].replaceWith(fragment); | 59 scripts[0].replaceWith(fragment); |
60 return '${document.outerHtml}\n'; | 60 return '${document.outerHtml}\n'; |
61 } | 61 } |
62 | 62 |
63 /// A document fragment with scripts that check for harmony features and that | 63 /// A document fragment with scripts that check for harmony features and that |
64 /// inject our runtime. | 64 /// inject our runtime. |
65 Node _loadRuntimeScripts() => parseFragment(''' | 65 Node _loadRuntimeScripts(options) { |
66 <script src="dev_compiler/runtime/harmony_feature_check.js"></script> | 66 // TODO(sigmund): use dev_compiler to generate messages_widget in the future. |
67 <script src="dev_compiler/runtime/dart_runtime.js"></script> | 67 var widgetCode = options.serverMode |
68 '''); | 68 ? '<script src="dev_compiler/runtime/messages_widget.js"></script>\n' |
| 69 '<link rel="stylesheet" href="dev_compiler/runtime/messages.css">' |
| 70 : ''; |
| 71 return parseFragment( |
| 72 '<script src="dev_compiler/runtime/harmony_feature_check.js"></script>\n' |
| 73 '<script src="dev_compiler/runtime/dart_runtime.js"></script>\n' |
| 74 '$widgetCode'); |
| 75 } |
69 | 76 |
70 /// A script tag that loads the .js code for a compiled library. | 77 /// A script tag that loads the .js code for a compiled library. |
71 Node _libraryInclude(String jsUrl) => | 78 Node _libraryInclude(String jsUrl) => |
72 parseFragment('<script src="$jsUrl"></script>\n'); | 79 parseFragment('<script src="$jsUrl"></script>\n'); |
73 | 80 |
74 /// A script tag that invokes the main function on the entry point library. | 81 /// A script tag that invokes the main function on the entry point library. |
75 Node _invokeMain(String mainLibraryName) => | 82 Node _invokeMain(String mainLibraryName) { |
76 parseFragment('<script>$mainLibraryName.main();</script>\n'); | 83 var code = mainLibraryName == null |
| 84 ? 'console.error("dev_compiler error: main was not generated");' |
| 85 : '$mainLibraryName.main();'; |
| 86 return parseFragment('<script>$code</script>\n'); |
| 87 } |
77 | 88 |
78 /// A script tag with a tiny mock of the core SDK. This is just used for testing | 89 /// A script tag with a tiny mock of the core SDK. This is just used for testing |
79 /// some small samples. | 90 /// some small samples. |
80 // TODO(sigmund,jmesserly): remove. | 91 // TODO(sigmund,jmesserly): remove. |
81 Node get _miniMockSdk => parseFragment(''' | 92 Node get _miniMockSdk => parseFragment(''' |
82 <script> | 93 <script> |
83 /* placehorder for unimplemented code libraries */ | 94 /* placehorder for unimplemented code libraries */ |
84 var math = Math; | 95 var math = Math; |
85 var core = { int: { parse: Number }, print: e => console.log(e) }; | 96 var core = { int: { parse: Number }, print: e => console.log(e) }; |
86 var dom = { document: document }; | 97 var dom = { document: document }; |
87 </script>'''); | 98 </script>'''); |
88 final _log = new Logger('dev_compiler.src.codegen.html_codegen'); | 99 final _log = new Logger('dev_compiler.src.codegen.html_codegen'); |
OLD | NEW |