Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(353)

Side by Side Diff: pkg/polymer/lib/boot.dart

Issue 225043004: Replace bootstrap logic with 'boot.js', use 'component/dart' mime-type and add (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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.contains(doc)) return state.scripts;
Jennifer Messerly 2014/04/04 21:26:39 small thing, Set.add returns true if it added, so
Siggi Cherem (dart-lang) 2014/04/04 23:10:07 Done.
102 state.seen.add(doc);
103
104 for (var node in doc.querySelectorAll('script,link[rel="import"]')) {
105 if (node is LinkElement) {
106 _discoverScripts(node.import, node.href, state);
107 } else if (node is ScriptElement) {
108 if (node.type == 'application/dart;component=1') {
109 if (node.src != '' ||
110 node.text != "export 'package:polymer/boot.dart';") {
111 state.scripts.add(_getScriptUrl(node));
112 }
113 }
114
115 if (node.type == 'application/dart') {
116 if (state.scriptSeen) {
117 print('Dartium currently only allows a single Dart script tag '
118 'per application, and in the future it will run them in '
119 'separtate isolates. To prepare for this Dart script '
120 'tags need to be updated to use the mime-type '
121 '"application/dart;component=1" instead of "application/dart":');
122 }
123 state.scriptSeen = true;
124 }
125 }
126 }
127 return state.scripts;
128 }
129
130 // TODO(sigmund): explore other (cheaper) ways to resolve URIs relative to the
131 // root library (see dartbug.com/12612)
132 final _rootUri = currentMirrorSystem().isolate.rootLibrary.uri;
133
134 /// Returns a URI that can be used to load the contents of [script] in a Dart
135 /// import. This is either the source URI if [script] has a `src` attribute, or
136 /// a base64 encoded `data:` URI if the [script] contents are inlined.
137 String _getScriptUrl(script) {
138 var uriString = script.src;
139 if (uriString != '') {
140 var uri = _rootUri.resolve(uriString);
141 if (!_isHttpStylePackageUrl(uri)) return '$uri';
142 // Use package: urls if available. This rule here is more permissive than
143 // how we translate urls in polymer-build, but we expect Dartium to limit
144 // the cases where there are differences. The polymer-build issues an error
145 // when using packages/ inside lib without properly stepping out all the way
146 // to the packages folder. If users don't create symlinks in the source
147 // tree, then Dartium will also complain because it won't find the file seen
148 // in an HTML import.
149 var packagePath = uri.path.substring(
150 uri.path.lastIndexOf('packages/') + 'packages/'.length);
151 return 'package:$packagePath';
152 }
153
154 return _asDataUri(script.text);
155 }
156
157 /// Whether [uri] is an http URI that contains a 'packages' segment, and
158 /// therefore could be converted into a 'package:' URI.
159 bool _isHttpStylePackageUrl(Uri uri) {
160 var uriPath = uri.path;
161 return uri.scheme == _rootUri.scheme &&
162 // Don't process cross-domain uris.
163 uri.authority == _rootUri.authority &&
164 uriPath.endsWith('.dart') &&
165 (uriPath.contains('/packages/') || uriPath.startsWith('packages/'));
166 }
167
168 /// Returns a base64 `data:` uri with the contents of [s].
169 // TODO(sigmund): change back to application/dart: using text/javascript seems
170 // wrong but it hides a warning in Dartium (dartbug.com/18000).
171 _asDataUri(s) => 'data:text/javascript;base64,${window.btoa(s)}';
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698