Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(119)

Side by Side Diff: lib/src/codegen/html_codegen.dart

Issue 1235503010: fixes #219, able to compile multiple entry points (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/compiler.dart' show AbstractCompiler;
13 import 'package:dev_compiler/src/options.dart'; 13 import 'package:dev_compiler/src/server/dependency_graph.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 jsOutputPath, jsOutputBase;
17
18 /// Emits an entry point HTML file corresponding to [inputFile] that can load 16 /// Emits an entry point HTML file corresponding to [inputFile] that can load
19 /// the code generated by the dev compiler. 17 /// the code generated by the dev compiler.
20 /// 18 ///
21 /// This internally transforms the given HTML [document]. When compiling to 19 /// 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 20 /// 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 21 /// 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 22 /// application. When compiling to Dart, we ensure that the document contains a
25 /// single Dart script tag, but otherwise emit the original document 23 /// single Dart script tag, but otherwise emit the original document
26 /// unmodified. 24 /// unmodified.
27 String generateEntryHtml(HtmlSourceNode root, CompilerOptions options) { 25 String generateEntryHtml(HtmlSourceNode root, AbstractCompiler compiler) {
26 var options = compiler.options;
28 var document = root.document.clone(true); 27 var document = root.document.clone(true);
29 var scripts = document.querySelectorAll('script[type="application/dart"]'); 28 var scripts = document.querySelectorAll('script[type="application/dart"]');
30 if (scripts.isEmpty) { 29 if (scripts.isEmpty) {
31 _log.warning('No <script type="application/dart"> found in ${root.uri}'); 30 _log.warning('No <script type="application/dart"> found in ${root.uri}');
32 return '${document.outerHtml}\n'; 31 return '${document.outerHtml}\n';
33 } 32 }
34 scripts.skip(1).forEach((s) { 33 scripts.skip(1).forEach((s) {
35 // TODO(sigmund): allow more than one Dart script tags? 34 // TODO(sigmund): allow more than one Dart script tags?
36 _log.warning(s.sourceSpan.message( 35 _log.warning(s.sourceSpan.message(
37 'unexpected script. Only one Dart script tag allowed ' 36 'unexpected script. Only one Dart script tag allowed '
(...skipping 28 matching lines...) Expand all
66 65
67 var fragment = new DocumentFragment(); 66 var fragment = new DocumentFragment();
68 for (var resource in resources) { 67 for (var resource in resources) {
69 var resourcePath = 68 var resourcePath =
70 resourceOutputPath(resource.uri, root.uri, options.runtimeDir); 69 resourceOutputPath(resource.uri, root.uri, options.runtimeDir);
71 var ext = path.extension(resourcePath); 70 var ext = path.extension(resourcePath);
72 if (resource.cachingHash != null) { 71 if (resource.cachingHash != null) {
73 resourcePath = _addHash(resourcePath, resource.cachingHash); 72 resourcePath = _addHash(resourcePath, resource.cachingHash);
74 } 73 }
75 if (ext == '.js') { 74 if (ext == '.js') {
76 fragment.nodes.add(_libraryInclude(resourcePath)); 75 fragment.nodes.add(libraryInclude(resourcePath));
77 } else if (ext == '.css') { 76 } else if (ext == '.css') {
78 var stylesheetLink = '<link rel="stylesheet" href="$resourcePath">\n'; 77 var stylesheetLink = '<link rel="stylesheet" href="$resourcePath">\n';
79 fragment.nodes.add(parseFragment(stylesheetLink)); 78 fragment.nodes.add(parseFragment(stylesheetLink));
80 } 79 }
81 } 80 }
82 81
83 String mainLibraryName; 82 String mainLibraryName;
84 for (var lib in libraries) { 83 for (var lib in libraries) {
85 var info = lib.info; 84 var info = lib.info;
86 if (info == null) continue; 85 if (info == null) continue;
87 var jsPath = jsOutputPath(info.library, root.uri); 86 var uri = info.library.source.uri;
88 if (info.isEntry) mainLibraryName = jsOutputBase(info.library, root.uri); 87 var jsPath = compiler.getModulePath(uri);
88 if (info.isEntry) mainLibraryName = compiler.getModuleName(uri);
89 if (lib.cachingHash != null) { 89 if (lib.cachingHash != null) {
90 jsPath = _addHash(jsPath, lib.cachingHash); 90 jsPath = _addHash(jsPath, lib.cachingHash);
91 } 91 }
92 fragment.nodes.add(_libraryInclude(jsPath)); 92 fragment.nodes.add(libraryInclude(jsPath));
93 } 93 }
94 fragment.nodes.add(_invokeMain(mainLibraryName)); 94 fragment.nodes.add(invokeMain(mainLibraryName));
95 scripts[0].replaceWith(fragment); 95 scripts[0].replaceWith(fragment);
96 return '${document.outerHtml}\n'; 96 return '${document.outerHtml}\n';
97 } 97 }
98 98
99 // TODO(jmesserly): the string interpolation in these could lead to injection
100 // bugs. Not really a security issue since input is trusted, but the resulting
101 // parse tree may not match expectations if interpolated strings contain quotes.
102
99 /// A script tag that loads the .js code for a compiled library. 103 /// A script tag that loads the .js code for a compiled library.
100 Node _libraryInclude(String jsUrl) => 104 Node libraryInclude(String jsUrl) =>
101 parseFragment('<script src="$jsUrl"></script>\n'); 105 parseFragment('<script src="$jsUrl"></script>\n');
102 106
103 /// A script tag that invokes the main function on the entry point library. 107 /// A script tag that invokes the main function on the entry point library.
104 Node _invokeMain(String mainLibraryName) { 108 Node invokeMain(String mainLibraryName) {
105 var code = mainLibraryName == null 109 var code = mainLibraryName == null
106 ? 'console.error("dev_compiler error: main was not generated");' 110 ? 'console.error("dev_compiler error: main was not generated");'
107 // TODO(vsm): Can we simplify this? 111 // TODO(vsm): Can we simplify this?
108 // See: https://github.com/dart-lang/dev_compiler/issues/164 112 // See: https://github.com/dart-lang/dev_compiler/issues/164
109 : "dart_library.start('$mainLibraryName');"; 113 : "dart_library.start('$mainLibraryName');";
110 return parseFragment('<script>$code</script>\n'); 114 return parseFragment('<script>$code</script>\n');
111 } 115 }
112 116
113 /// Convert the outputPath to include the hash in it. This function is the 117 /// Convert the outputPath to include the hash in it. This function is the
114 /// reverse of what the server does to determine whether a request needs to have 118 /// reverse of what the server does to determine whether a request needs to have
115 /// cache headers added to it. 119 /// cache headers added to it.
116 _addHash(String outPath, String hash) { 120 _addHash(String outPath, String hash) {
117 // (the ____ prefix makes it look better in the web inspector) 121 // (the ____ prefix makes it look better in the web inspector)
118 return '$outPath?____cached=$hash'; 122 return '$outPath?____cached=$hash';
119 } 123 }
120 124
121 final _log = new Logger('dev_compiler.src.codegen.html_codegen'); 125 final _log = new Logger('dev_compiler.src.codegen.html_codegen');
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698