OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /** | 5 /** |
6 * Final phase of the polymer transformation: includes any additional polyfills | 6 * Final phase of the polymer transformation: includes any additional polyfills |
7 * that may needed by the deployed app. | 7 * that may needed by the deployed app. |
8 */ | 8 */ |
9 library polymer.src.build.polyfill_injector; | 9 library polymer.src.build.polyfill_injector; |
10 | 10 |
11 import 'dart:async'; | 11 import 'dart:async'; |
12 | 12 |
13 import 'package:barback/barback.dart'; | 13 import 'package:barback/barback.dart'; |
14 import 'package:html5lib/dom.dart' show Document, Node, DocumentFragment; | 14 import 'package:html5lib/dom.dart' show Document, Node, DocumentFragment; |
15 import 'package:html5lib/parser.dart' show parseFragment; | 15 import 'package:html5lib/parser.dart' show parseFragment; |
16 import 'common.dart'; | 16 import 'common.dart'; |
17 | 17 |
18 /** | 18 /** |
19 * Ensures that any scripts and polyfills needed to run a polymer application | 19 * Ensures that any scripts and polyfills needed to run a polymer application |
20 * are included. For example, this transformer will ensure that there is a | 20 * are included. For example, this transformer will ensure that there is a |
21 * script tag that loads the shadow_dom polyfill and interop.js (used for the | 21 * script tag that loads the shadow_dom polyfill and interop.js (used for the |
22 * css shimming). | 22 * css shimming). |
23 * | |
24 * This step also replaces "packages/browser/dart.js" and the Dart script tag | |
25 * with a script tag that loads the dart2js compiled code3 directly. | |
Siggi Cherem (dart-lang)
2013/10/21 21:07:42
code3 -> code
Jennifer Messerly
2013/10/21 21:42:56
oops. fixed
| |
23 */ | 26 */ |
24 class PolyfillInjector extends Transformer with PolymerTransformer { | 27 class PolyfillInjector extends Transformer with PolymerTransformer { |
25 final TransformOptions options; | 28 final TransformOptions options; |
26 | 29 |
27 PolyfillInjector(this.options); | 30 PolyfillInjector(this.options); |
28 | 31 |
29 /** Only run on entry point .html files. */ | 32 /** Only run on entry point .html files. */ |
30 Future<bool> isPrimary(Asset input) => | 33 Future<bool> isPrimary(Asset input) => |
31 new Future.value(options.isHtmlEntryPoint(input.id)); | 34 new Future.value(options.isHtmlEntryPoint(input.id)); |
32 | 35 |
33 Future apply(Transform transform) { | 36 Future apply(Transform transform) { |
34 return readPrimaryAsHtml(transform).then((document) { | 37 return readPrimaryAsHtml(transform).then((document) { |
35 bool shadowDomFound = false; | 38 bool shadowDomFound = false; |
36 bool jsInteropFound = false; | 39 bool jsInteropFound = false; |
37 bool customElementFound = false; | 40 bool customElementFound = false; |
38 bool dartScriptTags = false; | 41 Element dartJs; |
42 final dartScripts = <Element>[]; | |
39 | 43 |
40 for (var tag in document.queryAll('script')) { | 44 for (var tag in document.queryAll('script')) { |
41 var src = tag.attributes['src']; | 45 var src = tag.attributes['src']; |
42 if (src != null) { | 46 if (src != null) { |
43 var last = src.split('/').last; | 47 var last = src.split('/').last; |
44 if (last == 'interop.js') { | 48 if (last == 'interop.js') { |
45 jsInteropFound = true; | 49 jsInteropFound = true; |
46 } else if (_shadowDomJS.hasMatch(last)) { | 50 } else if (_shadowDomJS.hasMatch(last)) { |
47 shadowDomFound = true; | 51 shadowDomFound = true; |
48 } else if (_customElementJS.hasMatch(last)) { | 52 } else if (_customElementJS.hasMatch(last)) { |
49 customElementFound = true; | 53 customElementFound = true; |
54 } else if (last == 'dart.js') { | |
55 dartJs = tag; | |
50 } | 56 } |
51 } | 57 } |
52 | 58 |
53 if (tag.attributes['type'] == 'application/dart') { | 59 if (tag.attributes['type'] == 'application/dart') { |
54 dartScriptTags = true; | 60 dartScripts.add(tag); |
55 } | 61 } |
56 } | 62 } |
57 | 63 |
58 if (!dartScriptTags) { | 64 if (dartScripts.isEmpty) { |
59 // This HTML has no Dart code, there is nothing to do here. | 65 // This HTML has no Dart code, there is nothing to do here. |
60 transform.addOutput(transform.primaryInput); | 66 transform.addOutput(transform.primaryInput); |
61 return; | 67 return; |
62 } | 68 } |
63 | 69 |
70 // TODO(jmesserly): ideally we would generate an HTML that loads | |
71 // dart2dart too. But for now dart2dart is not a supported deployment | |
72 // target, so just inline the JS script. This has the nice side effect of | |
73 // fixing our tests: even if content_shell supports Dart VM, we'll still | |
74 // test the compiled JS code. | |
75 if (options.directlyIncludeJS) { | |
76 // If using CSP add the "precompiled" extension | |
77 final csp = options.contentSecurityPolicy ? '.precompiled' : ''; | |
78 | |
79 // Replace all other Dart script tags with JavaScript versions. | |
80 for (var script in dartScripts) { | |
81 final src = script.attributes['src']; | |
82 if (src.endsWith('.dart')) { | |
83 script.attributes.remove('type'); | |
84 script.attributes['src'] = '$src$csp.js'; | |
85 } | |
86 } | |
87 // Remove "packages/browser/dart.js" | |
88 if (dartJs != null) dartJs.remove(); | |
89 } | |
90 | |
64 _addScript(urlSegment) { | 91 _addScript(urlSegment) { |
65 document.body.nodes.insert(0, parseFragment( | 92 document.body.nodes.insert(0, parseFragment( |
66 '<script src="packages/$urlSegment"></script>\n')); | 93 '<script src="packages/$urlSegment"></script>\n')); |
67 } | 94 } |
68 | 95 |
69 // JS interop code is required for Polymer CSS shimming. | 96 // JS interop code is required for Polymer CSS shimming. |
70 if (!jsInteropFound) _addScript('browser/interop.js'); | 97 if (!jsInteropFound) _addScript('browser/interop.js'); |
71 if (!customElementFound) { | 98 if (!customElementFound) { |
72 _addScript('custom_element/custom-elements.debug.js'); | 99 _addScript('custom_element/custom-elements.debug.js'); |
73 } | 100 } |
74 | 101 |
75 // This polyfill needs to be the first one on the body | 102 // This polyfill needs to be the first one on the body |
76 // TODO(jmesserly): this is .debug to workaround issue 13046. | 103 // TODO(jmesserly): this is .debug to workaround issue 13046. |
77 if (!shadowDomFound) _addScript('shadow_dom/shadow_dom.debug.js'); | 104 if (!shadowDomFound) _addScript('shadow_dom/shadow_dom.debug.js'); |
78 | 105 |
79 transform.addOutput( | 106 transform.addOutput( |
80 new Asset.fromString(transform.primaryInput.id, document.outerHtml)); | 107 new Asset.fromString(transform.primaryInput.id, document.outerHtml)); |
81 }); | 108 }); |
82 } | 109 } |
83 } | 110 } |
84 | 111 |
85 final _shadowDomJS = new RegExp(r'shadow_dom\..*\.js', caseSensitive: false); | 112 final _shadowDomJS = new RegExp(r'shadow_dom\..*\.js', caseSensitive: false); |
86 final _customElementJS = new RegExp(r'custom-elements\..*\.js', | 113 final _customElementJS = new RegExp(r'custom-elements\..*\.js', |
87 caseSensitive: false); | 114 caseSensitive: false); |
OLD | NEW |