OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 support for Dart scripts on the page as this script. | 5 // Bootstrap support for Dart scripts on the page as this script. |
6 | |
7 if (navigator.webkitStartDart) { | 6 if (navigator.webkitStartDart) { |
8 navigator.webkitStartDart(); | 7 navigator.webkitStartDart(); |
9 } else { | 8 } else { |
9 // TODO: | |
10 // - Support in-browser compilation. | |
11 // - Handle inline Dart scripts. | |
10 window.addEventListener("DOMContentLoaded", function (e) { | 12 window.addEventListener("DOMContentLoaded", function (e) { |
11 // Fall back to compiled JS. | 13 // Fall back to compiled JS. We try to figure out which |
12 var scripts = document.getElementsByTagName("script"); | 14 // Dart-to-JavaScript compiler to use by looking for |
13 var length = scripts.length; | 15 // meta information in the current document. |
14 for (var i = 0; i < length; ++i) { | 16 // |
15 if (scripts[i].type == "application/dart") { | 17 // <meta name="dart/use-dart2js"> |
16 // Remap foo.dart to foo.js. | 18 // |
17 // TODO: | 19 // If we don't find such a tag, we default to frog. |
18 // - Support in-browser compilation. | 20 var metas = document.getElementsByTagName("meta"); |
19 // - Handle inline Dart scripts. | 21 var length = metas.length; |
20 if (scripts[i].src && scripts[i].src != '') { | 22 var compiler = "frog"; |
asandholm
2012/05/07 13:13:00
You could consider using var jssuffix = ".js" or j
| |
21 var script = document.createElement('script'); | 23 for (var i = 0; i < length; i++) { |
24 if (metas[i].name == "dart/use-dart2js") { | |
25 compiler = "dart2js"; | |
26 break; | |
27 } | |
28 } | |
29 | |
30 // Run through all the scripts and replace them if they have a | |
31 // type that indicate that they source in Dart code. | |
32 var scripts = document.getElementsByTagName("script"); | |
33 var length = scripts.length; | |
34 for (var i = 0; i < length; ++i) { | |
35 if (scripts[i].type == "application/dart") { | |
36 // Remap foo.dart to foo.js or foo.js_ depending | |
37 // on the chosen compiler (frog or dart2js). | |
38 if (scripts[i].src && scripts[i].src != '') { | |
39 var script = document.createElement('script'); | |
40 if (compiler == "dart2js") { | |
41 script.src = scripts[i].src + '.js_'; | |
42 } else { | |
22 script.src = scripts[i].src + '.js'; | 43 script.src = scripts[i].src + '.js'; |
23 var parent = scripts[i].parentNode; | |
24 parent.replaceChild(script, scripts[i]); | |
25 } | 44 } |
45 var parent = scripts[i].parentNode; | |
46 parent.replaceChild(script, scripts[i]); | |
26 } | 47 } |
27 } | 48 } |
28 }, false); | 49 } |
50 }, false); | |
29 } | 51 } |
OLD | NEW |