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 import 'package:html/dom.dart'; | 5 import 'package:html/dom.dart'; |
6 import 'package:html/parser.dart' show parseFragment; | 6 import 'package:html/parser.dart' show parseFragment; |
7 import 'package:logging/logging.dart' show Logger; | 7 import 'package:logging/logging.dart' show Logger; |
8 import 'package:path/path.dart' as path; | 8 import 'package:path/path.dart' as path; |
9 | 9 |
10 import '../compiler.dart' show AbstractCompiler; | 10 import '../compiler.dart' show AbstractCompiler; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 resourcePath = _addHash(resourcePath, resource.cachingHash); | 61 resourcePath = _addHash(resourcePath, resource.cachingHash); |
62 } | 62 } |
63 var attrs = element.attributes; | 63 var attrs = element.attributes; |
64 if (attrs.containsKey('href')) { | 64 if (attrs.containsKey('href')) { |
65 attrs['href'] = resourcePath; | 65 attrs['href'] = resourcePath; |
66 } else if (attrs.containsKey('src')) { | 66 } else if (attrs.containsKey('src')) { |
67 attrs['src'] = resourcePath; | 67 attrs['src'] = resourcePath; |
68 } | 68 } |
69 }); | 69 }); |
70 | 70 |
| 71 var rootDir = path.dirname(root.uri.path); |
| 72 String rootRelative(String fullPath) { |
| 73 return path.relative(path.join(compiler.inputBaseDir, fullPath), |
| 74 from: rootDir); |
| 75 } |
| 76 |
71 var fragment = new DocumentFragment(); | 77 var fragment = new DocumentFragment(); |
72 for (var resource in resources) { | 78 for (var resource in resources) { |
73 var resourcePath = | 79 var resourcePath = rootRelative( |
74 resourceOutputPath(resource.uri, root.uri, options.runtimeDir); | 80 resourceOutputPath(resource.uri, root.uri, options.runtimeDir)); |
75 var ext = path.extension(resourcePath); | 81 var ext = path.extension(resourcePath); |
76 if (resource.cachingHash != null) { | 82 if (resource.cachingHash != null) { |
77 resourcePath = _addHash(resourcePath, resource.cachingHash); | 83 resourcePath = _addHash(resourcePath, resource.cachingHash); |
78 } | 84 } |
79 if (ext == '.js') { | 85 if (ext == '.js') { |
80 fragment.nodes.add(libraryInclude(resourcePath)); | 86 fragment.nodes.add(libraryInclude(resourcePath)); |
81 } else if (ext == '.css') { | 87 } else if (ext == '.css') { |
82 var stylesheetLink = '<link rel="stylesheet" href="$resourcePath">\n'; | 88 var stylesheetLink = '<link rel="stylesheet" href="$resourcePath">\n'; |
83 fragment.nodes.add(parseFragment(stylesheetLink)); | 89 fragment.nodes.add(parseFragment(stylesheetLink)); |
84 } | 90 } |
85 } | 91 } |
86 | 92 |
87 String mainLibraryName; | 93 String mainLibraryName; |
88 var src = scripts[0].attributes["src"]; | 94 var src = scripts[0].attributes["src"]; |
89 var scriptUri = root.source.resolveRelativeUri(Uri.parse(src)); | 95 var scriptUri = root.source.resolveRelativeUri(Uri.parse(src)); |
90 | 96 |
91 for (var lib in libraries) { | 97 for (var lib in libraries) { |
92 var info = lib.info; | 98 var info = lib.info; |
93 if (info == null) continue; | 99 if (info == null) continue; |
94 var uri = info.library.source.uri; | 100 var uri = info.library.source.uri; |
95 var jsPath = compiler.getModulePath(uri); | 101 var jsPath = rootRelative(compiler.getModulePath(uri)); |
96 if (uri == scriptUri) mainLibraryName = compiler.getModuleName(uri); | 102 if (uri == scriptUri) mainLibraryName = compiler.getModuleName(uri); |
97 if (lib.cachingHash != null) { | 103 if (lib.cachingHash != null) { |
98 jsPath = _addHash(jsPath, lib.cachingHash); | 104 jsPath = _addHash(jsPath, lib.cachingHash); |
99 } | 105 } |
100 fragment.nodes.add(libraryInclude(jsPath)); | 106 fragment.nodes.add(libraryInclude(jsPath)); |
101 } | 107 } |
102 fragment.nodes.add(invokeMain(mainLibraryName)); | 108 fragment.nodes.add(invokeMain(mainLibraryName)); |
103 scripts[0].replaceWith(fragment); | 109 scripts[0].replaceWith(fragment); |
104 return '${document.outerHtml}\n'; | 110 return '${document.outerHtml}\n'; |
105 } | 111 } |
(...skipping 18 matching lines...) Expand all Loading... |
124 | 130 |
125 /// Convert the outputPath to include the hash in it. This function is the | 131 /// Convert the outputPath to include the hash in it. This function is the |
126 /// reverse of what the server does to determine whether a request needs to have | 132 /// reverse of what the server does to determine whether a request needs to have |
127 /// cache headers added to it. | 133 /// cache headers added to it. |
128 _addHash(String outPath, String hash) { | 134 _addHash(String outPath, String hash) { |
129 // (the ____ prefix makes it look better in the web inspector) | 135 // (the ____ prefix makes it look better in the web inspector) |
130 return '$outPath?____cached=$hash'; | 136 return '$outPath?____cached=$hash'; |
131 } | 137 } |
132 | 138 |
133 final _log = new Logger('dev_compiler.src.codegen.html_codegen'); | 139 final _log = new Logger('dev_compiler.src.codegen.html_codegen'); |
OLD | NEW |