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 /// Bootstrap to initialize polymer applications. This library is not in use |
| 6 /// yet but it will replace boot.js in the near future (see dartbug.com/18007). |
| 7 /// |
| 8 /// This script contains logic to bootstrap polymer apps during development. It |
| 9 /// internally discovers special Dart script tags through HTML imports, and |
| 10 /// constructs a new entrypoint for the application that is then launched in an |
| 11 /// isolate. |
| 12 /// |
| 13 /// For each script tag found, we will load the corresponding Dart library and |
| 14 /// execute all methods annotated with `@initMethod` and register all classes |
| 15 /// labeled with `@CustomTag`. We keep track of the order of imports and execute |
| 16 /// initializers in the same order. |
| 17 /// |
| 18 /// All polymer applications use this bootstrap logic. It is included |
| 19 /// automatically when you include the polymer.html import: |
| 20 /// |
| 21 /// <link rel="import" href="packages/polymer/polymer.html"> |
| 22 /// |
| 23 /// There are two important changes compared to previous versions of polymer |
| 24 /// (0.10.0-pre.6 and older): |
| 25 /// |
| 26 /// * Use 'application/dart;component=1' instead of 'application/dart': |
| 27 /// Dartium already limits to have a single script tag per document, but it |
| 28 /// will be changing semantics soon and make them even stricter. Multiple |
| 29 /// script tags are not going to be running on the same isolate after this |
| 30 /// change. For polymer applications we'll use a parameter on the script tags |
| 31 /// mime-type to prevent Dartium from loading them separately. Instead this |
| 32 /// bootstrap script combines those special script tags and creates the |
| 33 /// application Dartium needs to run. |
| 34 /// |
| 35 // If you had: |
| 36 /// |
| 37 /// <polymer-element name="x-foo"> ... |
| 38 /// <script type="application/dart" src="x_foo.dart'></script> |
| 39 /// |
| 40 /// Now you need to write: |
| 41 /// |
| 42 /// <polymer-element name="x-foo"> ... |
| 43 /// <script type="application/dart;component=1" src="x_foo.dart'></script> |
| 44 /// |
| 45 /// * `initPolymer` is gone: we used to initialize applications in two |
| 46 /// possible ways: using init.dart or invoking initPolymer in your main. Any |
| 47 /// of these initialization patterns can be replaced to use an `@initMethod` |
| 48 /// instead. For example, If you need to run some initialization code before |
| 49 /// any other code is executed, include a "application/dart;component=1" |
| 50 /// script tag that contains an initializer method with the body of your old |
| 51 /// main, and make sure this tag is placed above other html-imports that load |
| 52 /// the rest of the application. Initialization methods are executed in the |
| 53 /// order in which they are discovered in the HTML document. |
| 54 library polymer.src.boot; |
| 55 |
| 56 import 'dart:html'; |
| 57 import 'dart:mirrors'; |
| 58 |
| 59 main() { |
| 60 var scripts = _discoverScripts(document, window.location.href); |
| 61 var sb = new StringBuffer()..write('library bootstrap;\n\n'); |
| 62 int count = 0; |
| 63 for (var s in scripts) { |
| 64 sb.writeln("import '$s' as prefix_$count;"); |
| 65 count++; |
| 66 } |
| 67 sb.writeln("import 'package:polymer/src/mirror_loader.dart';"); |
| 68 sb.write('\nmain() => startPolymerInDevelopment([\n'); |
| 69 for (var s in scripts) { |
| 70 sb.writeln(" '$s',"); |
| 71 } |
| 72 sb.write(']);\n'); |
| 73 var isolateUri = _asDataUri(sb.toString()); |
| 74 spawnDomUri(Uri.parse(isolateUri), [], ''); |
| 75 } |
| 76 |
| 77 /// Internal state used in [_discoverScripts]. |
| 78 class _State { |
| 79 /// Documents that we have visited thus far. |
| 80 final Set<Document> seen = new Set(); |
| 81 |
| 82 /// Scripts that have been discovered, in tree order. |
| 83 final List<String> scripts = []; |
| 84 |
| 85 /// Whether we've seen a type="application/dart" script tag, since we expect |
| 86 /// to have only one of those. |
| 87 bool scriptSeen = false; |
| 88 } |
| 89 |
| 90 /// Walks the HTML import structure to discover all script tags that are |
| 91 /// implicitly loaded. This code is only used in Dartium and should only be |
| 92 /// called after all HTML imports are resolved. Polymer ensures this by asking |
| 93 /// users to put their Dart script tags after all HTML imports (this is checked |
| 94 /// by the linter, and Dartium will otherwise show an error message). |
| 95 List<String> _discoverScripts(Document doc, String baseUri, [_State state]) { |
| 96 if (state == null) state = new _State(); |
| 97 if (doc == null) { |
| 98 print('warning: $baseUri not found.'); |
| 99 return state.scripts; |
| 100 } |
| 101 if (!state.seen.add(doc)) return state.scripts; |
| 102 |
| 103 for (var node in doc.querySelectorAll('script,link[rel="import"]')) { |
| 104 if (node is LinkElement) { |
| 105 _discoverScripts(node.import, node.href, state); |
| 106 } else if (node is ScriptElement) { |
| 107 if (node.type == 'application/dart;component=1') { |
| 108 if (node.src != '' || |
| 109 node.text != "export 'package:polymer/boot.dart';") { |
| 110 state.scripts.add(_getScriptUrl(node)); |
| 111 } |
| 112 } |
| 113 |
| 114 if (node.type == 'application/dart') { |
| 115 if (state.scriptSeen) { |
| 116 print('Dartium currently only allows a single Dart script tag ' |
| 117 'per application, and in the future it will run them in ' |
| 118 'separtate isolates. To prepare for this Dart script ' |
| 119 'tags need to be updated to use the mime-type ' |
| 120 '"application/dart;component=1" instead of "application/dart":'); |
| 121 } |
| 122 state.scriptSeen = true; |
| 123 } |
| 124 } |
| 125 } |
| 126 return state.scripts; |
| 127 } |
| 128 |
| 129 // TODO(sigmund): explore other (cheaper) ways to resolve URIs relative to the |
| 130 // root library (see dartbug.com/12612) |
| 131 final _rootUri = currentMirrorSystem().isolate.rootLibrary.uri; |
| 132 |
| 133 /// Returns a URI that can be used to load the contents of [script] in a Dart |
| 134 /// import. This is either the source URI if [script] has a `src` attribute, or |
| 135 /// a base64 encoded `data:` URI if the [script] contents are inlined. |
| 136 String _getScriptUrl(script) { |
| 137 var uriString = script.src; |
| 138 if (uriString != '') { |
| 139 var uri = _rootUri.resolve(uriString); |
| 140 if (!_isHttpStylePackageUrl(uri)) return '$uri'; |
| 141 // Use package: urls if available. This rule here is more permissive than |
| 142 // how we translate urls in polymer-build, but we expect Dartium to limit |
| 143 // the cases where there are differences. The polymer-build issues an error |
| 144 // when using packages/ inside lib without properly stepping out all the way |
| 145 // to the packages folder. If users don't create symlinks in the source |
| 146 // tree, then Dartium will also complain because it won't find the file seen |
| 147 // in an HTML import. |
| 148 var packagePath = uri.path.substring( |
| 149 uri.path.lastIndexOf('packages/') + 'packages/'.length); |
| 150 return 'package:$packagePath'; |
| 151 } |
| 152 |
| 153 return _asDataUri(script.text); |
| 154 } |
| 155 |
| 156 /// Whether [uri] is an http URI that contains a 'packages' segment, and |
| 157 /// therefore could be converted into a 'package:' URI. |
| 158 bool _isHttpStylePackageUrl(Uri uri) { |
| 159 var uriPath = uri.path; |
| 160 return uri.scheme == _rootUri.scheme && |
| 161 // Don't process cross-domain uris. |
| 162 uri.authority == _rootUri.authority && |
| 163 uriPath.endsWith('.dart') && |
| 164 (uriPath.contains('/packages/') || uriPath.startsWith('packages/')); |
| 165 } |
| 166 |
| 167 /// Returns a base64 `data:` uri with the contents of [s]. |
| 168 // TODO(sigmund): change back to application/dart: using text/javascript seems |
| 169 // wrong but it hides a warning in Dartium (dartbug.com/18000). |
| 170 _asDataUri(s) => 'data:text/javascript;base64,${window.btoa(s)}'; |
OLD | NEW |