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

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

Issue 1145893008: fixes #182, avoid injecting a duplicate link rel=stylesheet (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 6 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
« no previous file with comments | « no previous file | lib/src/dependency_graph.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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;
(...skipping 25 matching lines...) Expand all
36 _log.warning(s.sourceSpan.message( 36 _log.warning(s.sourceSpan.message(
37 'unexpected script. Only one Dart script tag allowed ' 37 'unexpected script. Only one Dart script tag allowed '
38 '(see https://github.com/dart-lang/dart-dev-compiler/issues/53).', 38 '(see https://github.com/dart-lang/dart-dev-compiler/issues/53).',
39 color: options.useColors ? colorOf('warning') : false)); 39 color: options.useColors ? colorOf('warning') : false));
40 s.remove(); 40 s.remove();
41 }); 41 });
42 42
43 if (options.outputDart) return '${document.outerHtml}\n'; 43 if (options.outputDart) return '${document.outerHtml}\n';
44 44
45 var libraries = []; 45 var libraries = [];
46 var resources = []; 46 var resources = new Set();
47 visitInPostOrder(root, (n) { 47 visitInPostOrder(root, (n) {
48 if (n is DartSourceNode) libraries.add(n); 48 if (n is DartSourceNode) libraries.add(n);
49 if (n is ResourceSourceNode) resources.add(n); 49 if (n is ResourceSourceNode) resources.add(n);
50 }, includeParts: false); 50 }, includeParts: false);
51 51
52 String mainLibraryName; 52 root.htmlResourceNodes.forEach((element, resource) {
53 // Make sure we don't try and add this node again.
54 resources.remove(resource);
55
56 var resourcePath = resourceOutputPath(resource.uri, root.uri);
57 if (resource.cachingHash != null) {
58 resourcePath = _addHash(resourcePath, resource.cachingHash);
59 }
60 var attrs = element.attributes;
61 if (attrs.containsKey('href')) {
62 attrs['href'] = resourcePath;
63 } else if (attrs.containsKey('src')) {
64 attrs['src'] = resourcePath;
65 }
66 });
67
53 var fragment = new DocumentFragment(); 68 var fragment = new DocumentFragment();
54 for (var resource in resources) { 69 for (var resource in resources) {
55 var resourcePath = resourceOutputPath(resource.uri, root.uri); 70 var resourcePath = resourceOutputPath(resource.uri, root.uri);
56 var ext = path.extension(resourcePath); 71 var ext = path.extension(resourcePath);
57 if (resource.cachingHash != null) { 72 if (resource.cachingHash != null) {
58 resourcePath = _addHash(resourcePath, resource.cachingHash); 73 resourcePath = _addHash(resourcePath, resource.cachingHash);
59 } 74 }
60 if (ext == '.css') { 75 if (ext == '.js') {
61 fragment.nodes.add(_cssInclude(resourcePath));
62 } else if (ext == '.js') {
63 fragment.nodes.add(_libraryInclude(resourcePath)); 76 fragment.nodes.add(_libraryInclude(resourcePath));
77 } else if (ext == '.css') {
78 var stylesheetLink = '<link rel="stylesheet" href="$resourcePath">\n';
79 fragment.nodes.add(parseFragment(stylesheetLink));
64 } 80 }
65 } 81 }
82
83 String mainLibraryName;
66 for (var lib in libraries) { 84 for (var lib in libraries) {
67 var info = lib.info; 85 var info = lib.info;
68 if (info == null) continue; 86 if (info == null) continue;
69 if (info.isEntry) mainLibraryName = jsLibraryName(info.library); 87 if (info.isEntry) mainLibraryName = jsLibraryName(info.library);
70 var jsPath = jsOutputPath(info, root.uri); 88 var jsPath = jsOutputPath(info, root.uri);
71 if (lib.cachingHash != null) { 89 if (lib.cachingHash != null) {
72 jsPath = _addHash(jsPath, lib.cachingHash); 90 jsPath = _addHash(jsPath, lib.cachingHash);
73 } 91 }
74 fragment.nodes.add(_libraryInclude(jsPath)); 92 fragment.nodes.add(_libraryInclude(jsPath));
75 } 93 }
76 fragment.nodes.add(_invokeMain(mainLibraryName)); 94 fragment.nodes.add(_invokeMain(mainLibraryName));
77 scripts[0].replaceWith(fragment); 95 scripts[0].replaceWith(fragment);
78 return '${document.outerHtml}\n'; 96 return '${document.outerHtml}\n';
79 } 97 }
80 98
81 /// A script tag that loads the .js code for a compiled library. 99 /// A script tag that loads the .js code for a compiled library.
82 Node _libraryInclude(String jsUrl) => 100 Node _libraryInclude(String jsUrl) =>
83 parseFragment('<script src="$jsUrl"></script>\n'); 101 parseFragment('<script src="$jsUrl"></script>\n');
84 102
85 /// A tag that loads the .css code.
86 Node _cssInclude(String cssUrl) =>
87 parseFragment('<link rel="stylesheet" href="$cssUrl">\n');
88
89 /// A script tag that invokes the main function on the entry point library. 103 /// A script tag that invokes the main function on the entry point library.
90 Node _invokeMain(String mainLibraryName) { 104 Node _invokeMain(String mainLibraryName) {
91 var code = mainLibraryName == null 105 var code = mainLibraryName == null
92 ? 'console.error("dev_compiler error: main was not generated");' 106 ? 'console.error("dev_compiler error: main was not generated");'
93 // TODO(vsm): Can we simplify this? 107 // TODO(vsm): Can we simplify this?
94 // See: https://github.com/dart-lang/dev_compiler/issues/164 108 // See: https://github.com/dart-lang/dev_compiler/issues/164
95 : '_isolate_helper.startRootIsolate($mainLibraryName.main, []);'; 109 : '_isolate_helper.startRootIsolate($mainLibraryName.main, []);';
96 return parseFragment('<script>$code</script>\n'); 110 return parseFragment('<script>$code</script>\n');
97 } 111 }
98 112
99 /// Convert the outputPath to include the hash in it. This function is the 113 /// Convert the outputPath to include the hash in it. This function is the
100 /// reverse of what the server does to determine whether a request needs to have 114 /// reverse of what the server does to determine whether a request needs to have
101 /// cache headers added to it. 115 /// cache headers added to it.
102 _addHash(String outPath, String hash) { 116 _addHash(String outPath, String hash) {
103 // (the ____ prefix makes it look better in the web inspector) 117 // (the ____ prefix makes it look better in the web inspector)
104 return '$outPath?____cached=$hash'; 118 return '$outPath?____cached=$hash';
105 } 119 }
106 120
107 final _log = new Logger('dev_compiler.src.codegen.html_codegen'); 121 final _log = new Logger('dev_compiler.src.codegen.html_codegen');
OLDNEW
« no previous file with comments | « no previous file | lib/src/dependency_graph.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698