| 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 /** Includes any additional polyfills that may needed by the deployed app. */ | 5 /** Includes any additional polyfills that may needed by the deployed app. */ |
| 6 library polymer.src.build.polyfill_injector; | 6 library polymer.src.build.polyfill_injector; |
| 7 | 7 |
| 8 import 'dart:async'; | 8 import 'dart:async'; |
| 9 | 9 |
| 10 import 'package:barback/barback.dart'; | 10 import 'package:barback/barback.dart'; |
| 11 import 'package:html5lib/dom.dart' show | 11 import 'package:html5lib/dom.dart' show |
| 12 Document, DocumentFragment, Element, Node; | 12 Document, DocumentFragment, Element, Node; |
| 13 import 'package:html5lib/parser.dart' show parseFragment; | 13 import 'package:html5lib/parser.dart' show parseFragment; |
| 14 import 'common.dart'; | 14 import 'common.dart'; |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * Ensures that any scripts and polyfills needed to run a polymer application | 17 * Ensures that any scripts and polyfills needed to run a polymer application |
| 18 * are included. For example, this transformer will ensure that there is a | 18 * are included. For example, this transformer will ensure that there is a |
| 19 * script tag that loads the shadow_dom polyfill and interop.js (used for the | 19 * script tag that loads the polyfills and interop.js (used for css shimming). |
| 20 * css shimming). | |
| 21 * | 20 * |
| 22 * This step also replaces "packages/browser/dart.js" and the Dart script tag | 21 * This step also replaces "packages/browser/dart.js" and the Dart script tag |
| 23 * with a script tag that loads the dart2js compiled code directly. | 22 * with a script tag that loads the dart2js compiled code directly. |
| 24 */ | 23 */ |
| 25 class PolyfillInjector extends Transformer with PolymerTransformer { | 24 class PolyfillInjector extends Transformer with PolymerTransformer { |
| 26 final TransformOptions options; | 25 final TransformOptions options; |
| 27 | 26 |
| 28 PolyfillInjector(this.options); | 27 PolyfillInjector(this.options); |
| 29 | 28 |
| 30 /** Only run on entry point .html files. */ | 29 /** Only run on entry point .html files. */ |
| 31 Future<bool> isPrimary(Asset input) => | 30 Future<bool> isPrimary(Asset input) => |
| 32 new Future.value(options.isHtmlEntryPoint(input.id)); | 31 new Future.value(options.isHtmlEntryPoint(input.id)); |
| 33 | 32 |
| 34 Future apply(Transform transform) { | 33 Future apply(Transform transform) { |
| 35 return readPrimaryAsHtml(transform).then((document) { | 34 return readPrimaryAsHtml(transform).then((document) { |
| 36 bool shadowDomFound = false; | 35 bool webComponentsFound = false; |
| 37 bool jsInteropFound = false; | 36 bool jsInteropFound = false; |
| 38 bool customElementFound = false; | |
| 39 Element dartJs; | 37 Element dartJs; |
| 40 final dartScripts = <Element>[]; | 38 final dartScripts = <Element>[]; |
| 41 | 39 |
| 42 for (var tag in document.queryAll('script')) { | 40 for (var tag in document.querySelectorAll('script')) { |
| 43 var src = tag.attributes['src']; | 41 var src = tag.attributes['src']; |
| 44 if (src != null) { | 42 if (src != null) { |
| 45 var last = src.split('/').last; | 43 var last = src.split('/').last; |
| 46 if (last == 'interop.js') { | 44 if (last == 'interop.js') { |
| 47 jsInteropFound = true; | 45 jsInteropFound = true; |
| 48 } else if (_shadowDomJS.hasMatch(last)) { | 46 } else if (_webComponentsJS.hasMatch(last)) { |
| 49 shadowDomFound = true; | 47 webComponentsFound = true; |
| 50 } else if (_customElementJS.hasMatch(last)) { | |
| 51 customElementFound = true; | |
| 52 } else if (last == 'dart.js') { | 48 } else if (last == 'dart.js') { |
| 53 dartJs = tag; | 49 dartJs = tag; |
| 54 } | 50 } |
| 55 } | 51 } |
| 56 | 52 |
| 57 if (tag.attributes['type'] == 'application/dart') { | 53 if (tag.attributes['type'] == 'application/dart') { |
| 58 dartScripts.add(tag); | 54 dartScripts.add(tag); |
| 59 } | 55 } |
| 60 } | 56 } |
| 61 | 57 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 82 script.attributes['src'] = '$src$csp.js'; | 78 script.attributes['src'] = '$src$csp.js'; |
| 83 } | 79 } |
| 84 } | 80 } |
| 85 // Remove "packages/browser/dart.js" | 81 // Remove "packages/browser/dart.js" |
| 86 if (dartJs != null) dartJs.remove(); | 82 if (dartJs != null) dartJs.remove(); |
| 87 } else if (dartJs == null) { | 83 } else if (dartJs == null) { |
| 88 document.body.nodes.add(parseFragment( | 84 document.body.nodes.add(parseFragment( |
| 89 '<script src="packages/browser/dart.js"></script>')); | 85 '<script src="packages/browser/dart.js"></script>')); |
| 90 } | 86 } |
| 91 | 87 |
| 92 _addScript(urlSegment) { | 88 _addScriptFirst(urlSegment) { |
| 93 document.head.nodes.insert(0, parseFragment( | 89 document.head.nodes.insert(0, parseFragment( |
| 94 '<script src="packages/$urlSegment"></script>\n')); | 90 '<script src="packages/$urlSegment"></script>\n')); |
| 95 } | 91 } |
| 96 | 92 |
| 97 // JS interop code is required for Polymer CSS shimming. | 93 // JS interop code is required for Polymer CSS shimming. |
| 98 if (!jsInteropFound) _addScript('browser/interop.js'); | 94 if (!jsInteropFound) _addScriptFirst('browser/interop.js'); |
| 99 | 95 |
| 100 // TODO(sigmund): enable using .min.js. This currently fails in checked | 96 var suffix = options.releaseMode ? '.js' : '.concat.js'; |
| 101 // mode because of bugs in dart2js mirrors (dartbug.com/14720). | 97 if (!webComponentsFound) { |
| 102 // var suffix = options.releaseMode ? '.min.js' : '.debug.js'; | 98 _addScriptFirst('web_components/dart_support.js'); |
| 103 var suffix = '.debug.js'; | 99 |
| 104 if (!customElementFound) { | 100 // platform.js should come before all other scripts. |
| 105 _addScript('custom_element/custom-elements$suffix'); | 101 _addScriptFirst('web_components/platform$suffix'); |
| 106 } | 102 } |
| 107 | 103 |
| 108 // This polyfill needs to be the first one on the head | |
| 109 if (!shadowDomFound) _addScript('shadow_dom/shadow_dom$suffix'); | |
| 110 | |
| 111 transform.addOutput( | 104 transform.addOutput( |
| 112 new Asset.fromString(transform.primaryInput.id, document.outerHtml)); | 105 new Asset.fromString(transform.primaryInput.id, document.outerHtml)); |
| 113 }); | 106 }); |
| 114 } | 107 } |
| 115 } | 108 } |
| 116 | 109 |
| 117 final _shadowDomJS = new RegExp(r'shadow_dom\..*\.js', caseSensitive: false); | 110 final _webComponentsJS = new RegExp(r'platform.*\.js', |
| 118 final _customElementJS = new RegExp(r'custom-elements\..*\.js', | |
| 119 caseSensitive: false); | 111 caseSensitive: false); |
| OLD | NEW |