| 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:html/dom.dart'; | 7 import 'package:html/dom.dart'; |
| 8 import 'package:html/parser.dart' show parseFragment; | 8 import 'package:html/parser.dart' show parseFragment; |
| 9 import 'package:logging/logging.dart' show Logger; | 9 import 'package:logging/logging.dart' show Logger; |
| 10 import 'package:path/path.dart' as path; | 10 import 'package:path/path.dart' as path; |
| 11 | 11 |
| 12 import 'package:dev_compiler/src/dependency_graph.dart'; | 12 import 'package:dev_compiler/src/dependency_graph.dart'; |
| 13 import 'package:dev_compiler/src/options.dart'; | 13 import 'package:dev_compiler/src/options.dart'; |
| 14 import 'package:dev_compiler/src/utils.dart' show colorOf, resourceOutputPath; | 14 import 'package:dev_compiler/src/utils.dart' show colorOf, resourceOutputPath; |
| 15 | 15 |
| 16 import 'js_codegen.dart' show jsLibraryName, jsOutputPath; | 16 import 'js_codegen.dart' show jsOutputPath, jsOutputBase; |
| 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 dev 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 our | 22 /// JavaScript, we remove any Dart script tags, add new script tags to load our |
| 23 /// 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. |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 } else if (ext == '.css') { | 75 } else if (ext == '.css') { |
| 76 var stylesheetLink = '<link rel="stylesheet" href="$resourcePath">\n'; | 76 var stylesheetLink = '<link rel="stylesheet" href="$resourcePath">\n'; |
| 77 fragment.nodes.add(parseFragment(stylesheetLink)); | 77 fragment.nodes.add(parseFragment(stylesheetLink)); |
| 78 } | 78 } |
| 79 } | 79 } |
| 80 | 80 |
| 81 String mainLibraryName; | 81 String mainLibraryName; |
| 82 for (var lib in libraries) { | 82 for (var lib in libraries) { |
| 83 var info = lib.info; | 83 var info = lib.info; |
| 84 if (info == null) continue; | 84 if (info == null) continue; |
| 85 if (info.isEntry) mainLibraryName = jsLibraryName(info.library); | 85 var jsPath = jsOutputPath(info.library, root.uri); |
| 86 var jsPath = jsOutputPath(info, root.uri); | 86 if (info.isEntry) mainLibraryName = jsOutputBase(info.library, root.uri); |
| 87 if (lib.cachingHash != null) { | 87 if (lib.cachingHash != null) { |
| 88 jsPath = _addHash(jsPath, lib.cachingHash); | 88 jsPath = _addHash(jsPath, lib.cachingHash); |
| 89 } | 89 } |
| 90 fragment.nodes.add(_libraryInclude(jsPath)); | 90 fragment.nodes.add(_libraryInclude(jsPath)); |
| 91 } | 91 } |
| 92 fragment.nodes.add(_invokeMain(mainLibraryName)); | 92 fragment.nodes.add(_invokeMain(mainLibraryName)); |
| 93 scripts[0].replaceWith(fragment); | 93 scripts[0].replaceWith(fragment); |
| 94 return '${document.outerHtml}\n'; | 94 return '${document.outerHtml}\n'; |
| 95 } | 95 } |
| 96 | 96 |
| 97 /// A script tag that loads the .js code for a compiled library. | 97 /// A script tag that loads the .js code for a compiled library. |
| 98 Node _libraryInclude(String jsUrl) => | 98 Node _libraryInclude(String jsUrl) => |
| 99 parseFragment('<script src="$jsUrl"></script>\n'); | 99 parseFragment('<script src="$jsUrl"></script>\n'); |
| 100 | 100 |
| 101 /// A script tag that invokes the main function on the entry point library. | 101 /// A script tag that invokes the main function on the entry point library. |
| 102 Node _invokeMain(String mainLibraryName) { | 102 Node _invokeMain(String mainLibraryName) { |
| 103 var code = mainLibraryName == null | 103 var code = mainLibraryName == null |
| 104 ? 'console.error("dev_compiler error: main was not generated");' | 104 ? 'console.error("dev_compiler error: main was not generated");' |
| 105 // TODO(vsm): Can we simplify this? | 105 // TODO(vsm): Can we simplify this? |
| 106 // See: https://github.com/dart-lang/dev_compiler/issues/164 | 106 // See: https://github.com/dart-lang/dev_compiler/issues/164 |
| 107 : '_isolate_helper.startRootIsolate($mainLibraryName.main, []);'; | 107 : "dart.start('$mainLibraryName');"; |
| 108 return parseFragment('<script>$code</script>\n'); | 108 return parseFragment('<script>$code</script>\n'); |
| 109 } | 109 } |
| 110 | 110 |
| 111 /// Convert the outputPath to include the hash in it. This function is the | 111 /// Convert the outputPath to include the hash in it. This function is the |
| 112 /// reverse of what the server does to determine whether a request needs to have | 112 /// reverse of what the server does to determine whether a request needs to have |
| 113 /// cache headers added to it. | 113 /// cache headers added to it. |
| 114 _addHash(String outPath, String hash) { | 114 _addHash(String outPath, String hash) { |
| 115 // (the ____ prefix makes it look better in the web inspector) | 115 // (the ____ prefix makes it look better in the web inspector) |
| 116 return '$outPath?____cached=$hash'; | 116 return '$outPath?____cached=$hash'; |
| 117 } | 117 } |
| 118 | 118 |
| 119 final _log = new Logger('dev_compiler.src.codegen.html_codegen'); | 119 final _log = new Logger('dev_compiler.src.codegen.html_codegen'); |
| OLD | NEW |