Chromium Code Reviews| Index: lib/src/mirror_initializer.dart |
| diff --git a/lib/src/mirror_initializer.dart b/lib/src/mirror_initializer.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ff25ad89ff400d8b7182799a49b3020982e437fe |
| --- /dev/null |
| +++ b/lib/src/mirror_initializer.dart |
| @@ -0,0 +1,145 @@ |
| +// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +/// Contains logic to initialize web_components apps during development. This |
| +/// implementation uses dart:mirrors to load each library as they are discovered |
| +/// through HTML imports. This is only meant to be used during development in |
| +/// dartium, and the web_components transformers replace this implementation |
| +/// for deployment. |
| +library web_components.src.mirror_initializer; |
| + |
| +import 'dart:async'; |
| +import 'dart:collection' show LinkedHashMap; |
| +import 'dart:mirrors'; |
| +import 'dart:html'; |
| +import 'package:initialize/initialize.dart' as init; |
| + |
| +Future run({List<Type> typeFilter, init.InitializerFilter customFilter}) async { |
| + var libraryUris = _discoverLibrariesToLoad(document, window.location.href) |
| + .map(Uri.parse); |
| + |
| + for (var uri in libraryUris) { |
| + await init.run( |
| + typeFilter: typeFilter, customFilter: customFilter, from: uri); |
| + } |
| + return null; |
| +} |
| + |
| +/// Walks the HTML import structure to discover all script tags that are |
| +/// implicitly loaded. This code is only used in Dartium and should only be |
| +/// called after all HTML imports are resolved. Polymer ensures this by asking |
| +/// users to put their Dart script tags after all HTML imports (this is checked |
| +/// by the linter, and Dartium will otherwise show an error message). |
| +Iterable<_ScriptInfo> _discoverScripts( |
| + Document doc, String baseUri, [_State state]) { |
| + if (state == null) state = new _State(); |
| + if (doc == null) { |
| + print('warning: $baseUri not found.'); |
| + return state.scripts.values; |
| + } |
| + if (!state.seen.add(doc)) return state.scripts.values; |
| + |
| + for (var node in doc.querySelectorAll('script,link[rel="import"]')) { |
| + if (node is LinkElement) { |
| + _discoverScripts(node.import, node.href, state); |
| + } else if (node is ScriptElement && node.type == 'application/dart') { |
| + var info = _scriptInfoFor(node, baseUri); |
| + if (state.scripts.containsKey(info.resolvedUrl)) { |
| + print('warning: Script `${info.resolvedUrl}` included more than once. ' |
| + 'See http://goo.gl/5HPeuP#polymer_44 for more details.'); |
|
Siggi Cherem (dart-lang)
2015/03/23 22:16:26
we might want to relabel this warning in the futur
jakemac
2015/03/23 22:32:41
Acknowledged.
|
| + } else { |
| + state.scripts[info.resolvedUrl] = info; |
| + } |
| + } |
| + } |
| + return state.scripts.values; |
| +} |
| + |
| +/// Internal state used in [_discoverScripts]. |
| +class _State { |
| + /// Documents that we have visited thus far. |
| + final Set<Document> seen = new Set(); |
| + |
| + /// Scripts that have been discovered, in tree order. |
| + final LinkedHashMap<String, _ScriptInfo> scripts = {}; |
| +} |
| + |
| +/// Holds information about a Dart script tag. |
| +class _ScriptInfo { |
| + /// The original URL seen in the tag fully resolved. |
| + final String resolvedUrl; |
| + |
| + /// Whether there was no URL, but code inlined instead. |
| + bool get isInline => text != null; |
| + |
| + /// Whether it seems to be a 'package:' URL (starts with the package-root). |
| + bool get isPackage => packageUrl != null; |
| + |
| + /// The equivalent 'package:' URL, if any. |
| + final String packageUrl; |
| + |
| + /// The inlined text, if any. |
| + final String text; |
| + |
| + /// Returns a base64 `data:` uri with the contents of [text]. |
| + // TODO(sigmund): change back to application/dart: using text/javascript seems |
| + // wrong but it hides a warning in Dartium (dartbug.com/18000). |
| + String get dataUrl => 'data:text/javascript;base64,${window.btoa(text)}'; |
| + |
| + /// URL to import the contents of the original script from Dart. This is |
| + /// either the source URL if the script tag had a `src` attribute, or a base64 |
| + /// encoded `data:` URL if the script contents are inlined, or a `package:` |
| + /// URL if the script can be resolved via a package URL. |
| + String get importUrl => |
| + isInline ? dataUrl : (isPackage ? packageUrl : resolvedUrl); |
| + |
| + _ScriptInfo(this.resolvedUrl, {this.packageUrl, this.text}); |
| +} |
| + |
| + |
| +// TODO(sigmund): explore other (cheaper) ways to resolve URIs relative to the |
| +// root library (see dartbug.com/12612) |
| +final _rootUri = currentMirrorSystem().isolate.rootLibrary.uri; |
| + |
| +/// Returns [_ScriptInfo] for [script] which was seen in [baseUri]. |
| +_ScriptInfo _scriptInfoFor(script, baseUri) { |
| + var uriString = script.src; |
| + if (uriString != '') { |
| + var uri = _rootUri.resolve(uriString); |
| + if (!_isHttpStylePackageUrl(uri)) return new _ScriptInfo('$uri'); |
| + // Use package: urls if available. This rule here is more permissive than |
| + // how we translate urls in polymer-build, but we expect Dartium to limit |
| + // the cases where there are differences. The polymer-build issues an error |
| + // when using packages/ inside lib without properly stepping out all the way |
| + // to the packages folder. If users don't create symlinks in the source |
| + // tree, then Dartium will also complain because it won't find the file seen |
| + // in an HTML import. |
| + var packagePath = uri.path.substring( |
| + uri.path.lastIndexOf('packages/') + 'packages/'.length); |
| + return new _ScriptInfo('$uri', packageUrl: 'package:$packagePath'); |
| + } |
| + |
| + return new _ScriptInfo(baseUri, text: script.text); |
| +} |
| + |
| +/// Whether [uri] is an http URI that contains a 'packages' segment, and |
| +/// therefore could be converted into a 'package:' URI. |
| +bool _isHttpStylePackageUrl(Uri uri) { |
| + var uriPath = uri.path; |
| + return uri.scheme == _rootUri.scheme && |
| + // Don't process cross-domain uris. |
| + uri.authority == _rootUri.authority && |
| + uriPath.endsWith('.dart') && |
| + (uriPath.contains('/packages/') || uriPath.startsWith('packages/')); |
| +} |
| + |
| +Iterable<String> _discoverLibrariesToLoad(Document doc, String baseUri) => |
| + _discoverScripts(doc, baseUri).map( |
| + (info) => _packageUrlExists(info) ? info.packageUrl : info.resolvedUrl); |
|
Siggi Cherem (dart-lang)
2015/03/23 22:16:26
I think that to support inline Dart script tags, w
jakemac
2015/03/23 22:32:41
worked on this together, and stripped out some mor
|
| + |
| +/// All libraries in the current isolate. |
| +final _libs = currentMirrorSystem().libraries; |
| + |
| +bool _packageUrlExists(_ScriptInfo info) => |
| + info.isPackage && _libs[Uri.parse(info.packageUrl)] != null; |