OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 /// Contains logic to initialize web_components apps during development. This |
| 6 /// implementation uses dart:mirrors to load each library as they are discovered |
| 7 /// through HTML imports. This is only meant to be used during development in |
| 8 /// dartium, and the web_components transformers replace this implementation |
| 9 /// for deployment. |
| 10 library web_components.src.mirror_initializer; |
| 11 |
| 12 import 'dart:async'; |
| 13 import 'dart:collection' show LinkedHashMap; |
| 14 import 'dart:mirrors'; |
| 15 import 'dart:html'; |
| 16 import 'package:initialize/initialize.dart' as init; |
| 17 |
| 18 Future run({List<Type> typeFilter, init.InitializerFilter customFilter}) async { |
| 19 var libraryUris = _discoverLibrariesToLoad(document, window.location.href) |
| 20 .map(Uri.parse); |
| 21 |
| 22 for (var uri in libraryUris) { |
| 23 await init.run( |
| 24 typeFilter: typeFilter, customFilter: customFilter, from: uri); |
| 25 } |
| 26 return null; |
| 27 } |
| 28 |
| 29 /// Walks the HTML import structure to discover all script tags that are |
| 30 /// implicitly loaded. This code is only used in Dartium and should only be |
| 31 /// called after all HTML imports are resolved. Polymer ensures this by asking |
| 32 /// users to put their Dart script tags after all HTML imports (this is checked |
| 33 /// by the linter, and Dartium will otherwise show an error message). |
| 34 Iterable<_ScriptInfo> _discoverScripts( |
| 35 Document doc, String baseUri, [_State state]) { |
| 36 if (state == null) state = new _State(); |
| 37 if (doc == null) { |
| 38 print('warning: $baseUri not found.'); |
| 39 return state.scripts.values; |
| 40 } |
| 41 if (!state.seen.add(doc)) return state.scripts.values; |
| 42 |
| 43 for (var node in doc.querySelectorAll('script,link[rel="import"]')) { |
| 44 if (node is LinkElement) { |
| 45 _discoverScripts(node.import, node.href, state); |
| 46 } else if (node is ScriptElement && node.type == 'application/dart') { |
| 47 var info = _scriptInfoFor(node, baseUri); |
| 48 if (state.scripts.containsKey(info.resolvedUrl)) { |
| 49 // TODO(jakemac): Move this to a web_components uri. |
| 50 print('warning: Script `${info.resolvedUrl}` included more than once. ' |
| 51 'See http://goo.gl/5HPeuP#polymer_44 for more details.'); |
| 52 } else { |
| 53 state.scripts[info.resolvedUrl] = info; |
| 54 } |
| 55 } |
| 56 } |
| 57 return state.scripts.values; |
| 58 } |
| 59 |
| 60 /// Internal state used in [_discoverScripts]. |
| 61 class _State { |
| 62 /// Documents that we have visited thus far. |
| 63 final Set<Document> seen = new Set(); |
| 64 |
| 65 /// Scripts that have been discovered, in tree order. |
| 66 final LinkedHashMap<String, _ScriptInfo> scripts = {}; |
| 67 } |
| 68 |
| 69 /// Holds information about a Dart script tag. |
| 70 class _ScriptInfo { |
| 71 /// The original URL seen in the tag fully resolved. |
| 72 final String resolvedUrl; |
| 73 |
| 74 /// Whether it seems to be a 'package:' URL (starts with the package-root). |
| 75 bool get isPackage => packageUrl != null; |
| 76 |
| 77 /// The equivalent 'package:' URL, if any. |
| 78 final String packageUrl; |
| 79 |
| 80 _ScriptInfo(this.resolvedUrl, {this.packageUrl}); |
| 81 } |
| 82 |
| 83 |
| 84 // TODO(sigmund): explore other (cheaper) ways to resolve URIs relative to the |
| 85 // root library (see dartbug.com/12612) |
| 86 final _rootUri = currentMirrorSystem().isolate.rootLibrary.uri; |
| 87 |
| 88 /// Returns [_ScriptInfo] for [script] which was seen in [baseUri]. |
| 89 _ScriptInfo _scriptInfoFor(script, baseUri) { |
| 90 var uriString = script.src; |
| 91 if (uriString != '') { |
| 92 var uri = _rootUri.resolve(uriString); |
| 93 if (!_isHttpStylePackageUrl(uri)) return new _ScriptInfo('$uri'); |
| 94 // Use package: urls if available. This rule here is more permissive than |
| 95 // how we translate urls in polymer-build, but we expect Dartium to limit |
| 96 // the cases where there are differences. The polymer-build issues an error |
| 97 // when using packages/ inside lib without properly stepping out all the way |
| 98 // to the packages folder. If users don't create symlinks in the source |
| 99 // tree, then Dartium will also complain because it won't find the file seen |
| 100 // in an HTML import. |
| 101 var packagePath = uri.path.substring( |
| 102 uri.path.lastIndexOf('packages/') + 'packages/'.length); |
| 103 return new _ScriptInfo('$uri', packageUrl: 'package:$packagePath'); |
| 104 } |
| 105 |
| 106 // Even in the case of inline scripts its ok to just use the baseUri since |
| 107 // there can only be one per page. |
| 108 return new _ScriptInfo(baseUri); |
| 109 } |
| 110 |
| 111 /// Whether [uri] is an http URI that contains a 'packages' segment, and |
| 112 /// therefore could be converted into a 'package:' URI. |
| 113 bool _isHttpStylePackageUrl(Uri uri) { |
| 114 var uriPath = uri.path; |
| 115 return uri.scheme == _rootUri.scheme && |
| 116 // Don't process cross-domain uris. |
| 117 uri.authority == _rootUri.authority && |
| 118 uriPath.endsWith('.dart') && |
| 119 (uriPath.contains('/packages/') || uriPath.startsWith('packages/')); |
| 120 } |
| 121 |
| 122 Iterable<String> _discoverLibrariesToLoad(Document doc, String baseUri) => |
| 123 _discoverScripts(doc, baseUri).map( |
| 124 (info) => _packageUrlExists(info) ? info.packageUrl : info.resolvedUrl); |
| 125 |
| 126 /// All libraries in the current isolate. |
| 127 final _libs = currentMirrorSystem().libraries; |
| 128 |
| 129 bool _packageUrlExists(_ScriptInfo info) => |
| 130 info.isPackage && _libs[Uri.parse(info.packageUrl)] != null; |
OLD | NEW |