| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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 /// Bootstrap to initialize polymer applications. This library is not in use | 5 /// This library contains logic to help bootstrap polymer apps during |
| 6 /// yet but it will replace boot.js in the near future (see dartbug.com/18007). | 6 /// development. Mainly, this is code to discover Dart script tags through HTML |
| 7 /// | 7 /// imports, which `initPolymer` can use to load an application. |
| 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; | 8 library polymer.src.boot; |
| 55 | 9 |
| 56 import 'dart:html'; | 10 import 'dart:html'; |
| 11 |
| 12 // Technically, we shouldn't need any @MirrorsUsed, since this is for |
| 13 // development only, but our test bots don't yet run pub-build. See more details |
| 14 // on the comments of the mirrors import in `lib/polymer.dart`. |
| 15 @MirrorsUsed(symbols: const []) |
| 57 import 'dart:mirrors'; | 16 import 'dart:mirrors'; |
| 58 | 17 |
| 59 main() { | 18 /// Internal state used in [discoverScripts]. |
| 60 var scripts = _discoverScripts(document, window.location.href); | 19 class State { |
| 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. | 20 /// Documents that we have visited thus far. |
| 80 final Set<Document> seen = new Set(); | 21 final Set<Document> seen = new Set(); |
| 81 | 22 |
| 82 /// Scripts that have been discovered, in tree order. | 23 /// Scripts that have been discovered, in tree order. |
| 83 final List<String> scripts = []; | 24 final List<ScriptInfo> scripts = []; |
| 25 } |
| 84 | 26 |
| 85 /// Whether we've seen a type="application/dart" script tag, since we expect | 27 /// Holds information about a Dart script tag. |
| 86 /// to have only one of those. | 28 class ScriptInfo { |
| 87 bool scriptSeen = false; | 29 /// The original URL seen in the tag fully resolved. |
| 30 final String resolvedUrl; |
| 31 |
| 32 /// Whether there was no URL, but code inlined instead. |
| 33 bool get isInline => text != null; |
| 34 |
| 35 /// Whether it seems to be a 'package:' URL (starts with the package-root). |
| 36 bool get isPackage => packageUrl != null; |
| 37 |
| 38 /// The equivalent 'package:' URL, if any. |
| 39 final String packageUrl; |
| 40 |
| 41 /// The inlined text, if any. |
| 42 final String text; |
| 43 |
| 44 /// Returns a base64 `data:` uri with the contents of [text]. |
| 45 // TODO(sigmund): change back to application/dart: using text/javascript seems |
| 46 // wrong but it hides a warning in Dartium (dartbug.com/18000). |
| 47 String get dataUrl => 'data:text/javascript;base64,${window.btoa(text)}'; |
| 48 |
| 49 /// URL to import the contents of the original script from Dart. This is |
| 50 /// either the source URL if the script tag had a `src` attribute, or a base64 |
| 51 /// encoded `data:` URL if the script contents are inlined, or a `package:` |
| 52 /// URL if the script can be resolved via a package URL. |
| 53 String get importUrl => |
| 54 isInline ? dataUrl : (isPackage ? packageUrl : resolvedUrl); |
| 55 |
| 56 ScriptInfo(this.resolvedUrl, {this.packageUrl, this.text}); |
| 88 } | 57 } |
| 89 | 58 |
| 90 /// Walks the HTML import structure to discover all script tags that are | 59 /// 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 | 60 /// 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 | 61 /// 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 | 62 /// 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). | 63 /// by the linter, and Dartium will otherwise show an error message). |
| 95 List<String> _discoverScripts(Document doc, String baseUri, [_State state]) { | 64 List<ScriptInfo> discoverScripts(Document doc, String baseUri, [State state]) { |
| 96 if (state == null) state = new _State(); | 65 if (state == null) state = new State(); |
| 97 if (doc == null) { | 66 if (doc == null) { |
| 98 print('warning: $baseUri not found.'); | 67 print('warning: $baseUri not found.'); |
| 99 return state.scripts; | 68 return state.scripts; |
| 100 } | 69 } |
| 101 if (!state.seen.add(doc)) return state.scripts; | 70 if (!state.seen.add(doc)) return state.scripts; |
| 102 | 71 |
| 103 for (var node in doc.querySelectorAll('script,link[rel="import"]')) { | 72 for (var node in doc.querySelectorAll('script,link[rel="import"]')) { |
| 104 if (node is LinkElement) { | 73 if (node is LinkElement) { |
| 105 _discoverScripts(node.import, node.href, state); | 74 discoverScripts(node.import, node.href, state); |
| 106 } else if (node is ScriptElement) { | 75 } else if (node is ScriptElement && node.type == 'application/dart') { |
| 107 if (node.type == 'application/dart;component=1') { | 76 state.scripts.add(_scriptInfoFor(node, baseUri)); |
| 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 } | 77 } |
| 125 } | 78 } |
| 126 return state.scripts; | 79 return state.scripts; |
| 127 } | 80 } |
| 128 | 81 |
| 129 // TODO(sigmund): explore other (cheaper) ways to resolve URIs relative to the | 82 // TODO(sigmund): explore other (cheaper) ways to resolve URIs relative to the |
| 130 // root library (see dartbug.com/12612) | 83 // root library (see dartbug.com/12612) |
| 131 final _rootUri = currentMirrorSystem().isolate.rootLibrary.uri; | 84 final _rootUri = currentMirrorSystem().isolate.rootLibrary.uri; |
| 132 | 85 |
| 133 /// Returns a URI that can be used to load the contents of [script] in a Dart | 86 /// Returns [ScriptInfo] for [script] which was seen in [baseUri]. |
| 134 /// import. This is either the source URI if [script] has a `src` attribute, or | 87 ScriptInfo _scriptInfoFor(script, baseUri) { |
| 135 /// a base64 encoded `data:` URI if the [script] contents are inlined. | |
| 136 String _getScriptUrl(script) { | |
| 137 var uriString = script.src; | 88 var uriString = script.src; |
| 138 if (uriString != '') { | 89 if (uriString != '') { |
| 139 var uri = _rootUri.resolve(uriString); | 90 var uri = _rootUri.resolve(uriString); |
| 140 if (!_isHttpStylePackageUrl(uri)) return '$uri'; | 91 if (!_isHttpStylePackageUrl(uri)) return new ScriptInfo('$uri'); |
| 141 // Use package: urls if available. This rule here is more permissive than | 92 // 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 | 93 // 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 | 94 // 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 | 95 // 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 | 96 // 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 | 97 // tree, then Dartium will also complain because it won't find the file seen |
| 147 // in an HTML import. | 98 // in an HTML import. |
| 148 var packagePath = uri.path.substring( | 99 var packagePath = uri.path.substring( |
| 149 uri.path.lastIndexOf('packages/') + 'packages/'.length); | 100 uri.path.lastIndexOf('packages/') + 'packages/'.length); |
| 150 return 'package:$packagePath'; | 101 return new ScriptInfo('$uri', packageUrl: 'package:$packagePath'); |
| 151 } | 102 } |
| 152 | 103 |
| 153 return _asDataUri(script.text); | 104 return new ScriptInfo(baseUri, text: script.text); |
| 154 } | 105 } |
| 155 | 106 |
| 156 /// Whether [uri] is an http URI that contains a 'packages' segment, and | 107 /// Whether [uri] is an http URI that contains a 'packages' segment, and |
| 157 /// therefore could be converted into a 'package:' URI. | 108 /// therefore could be converted into a 'package:' URI. |
| 158 bool _isHttpStylePackageUrl(Uri uri) { | 109 bool _isHttpStylePackageUrl(Uri uri) { |
| 159 var uriPath = uri.path; | 110 var uriPath = uri.path; |
| 160 return uri.scheme == _rootUri.scheme && | 111 return uri.scheme == _rootUri.scheme && |
| 161 // Don't process cross-domain uris. | 112 // Don't process cross-domain uris. |
| 162 uri.authority == _rootUri.authority && | 113 uri.authority == _rootUri.authority && |
| 163 uriPath.endsWith('.dart') && | 114 uriPath.endsWith('.dart') && |
| 164 (uriPath.contains('/packages/') || uriPath.startsWith('packages/')); | 115 (uriPath.contains('/packages/') || uriPath.startsWith('packages/')); |
| 165 } | 116 } |
| 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 |