| 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 /// Ensures that any scripts and polyfills needed to run a polymer application | 16 /// Ensures that any scripts and polyfills needed to run a polymer application |
| 17 /// are included. For example, this transformer will ensure that there is a | 17 /// are included. |
| 18 /// script tag that loads the polyfills and interop.js (used for css shimming). | |
| 19 /// | 18 /// |
| 20 /// This step also replaces "packages/browser/dart.js" and the Dart script tag | 19 /// This step also replaces "packages/browser/dart.js" and the Dart script tag |
| 21 /// with a script tag that loads the dart2js compiled code directly. | 20 /// with a script tag that loads the dart2js compiled code directly. |
| 22 class PolyfillInjector extends Transformer with PolymerTransformer { | 21 class PolyfillInjector extends Transformer with PolymerTransformer { |
| 23 final TransformOptions options; | 22 final TransformOptions options; |
| 24 | 23 |
| 25 PolyfillInjector(this.options); | 24 PolyfillInjector(this.options); |
| 26 | 25 |
| 27 /// Only run on entry point .html files. | 26 /// Only run on entry point .html files. |
| 28 Future<bool> isPrimary(Asset input) => | 27 Future<bool> isPrimary(Asset input) => |
| 29 new Future.value(options.isHtmlEntryPoint(input.id)); | 28 new Future.value(options.isHtmlEntryPoint(input.id)); |
| 30 | 29 |
| 31 Future apply(Transform transform) { | 30 Future apply(Transform transform) { |
| 32 return readPrimaryAsHtml(transform).then((document) { | 31 return readPrimaryAsHtml(transform).then((document) { |
| 33 bool webComponentsFound = false; | 32 bool webComponentsFound = false; |
| 34 bool jsInteropFound = false; | |
| 35 Element dartJs; | 33 Element dartJs; |
| 36 final dartScripts = <Element>[]; | 34 final dartScripts = <Element>[]; |
| 37 | 35 |
| 38 for (var tag in document.querySelectorAll('script')) { | 36 for (var tag in document.querySelectorAll('script')) { |
| 39 var src = tag.attributes['src']; | 37 var src = tag.attributes['src']; |
| 40 if (src != null) { | 38 if (src != null) { |
| 41 var last = src.split('/').last; | 39 var last = src.split('/').last; |
| 42 if (last == 'interop.js') { | 40 if (_webComponentsJS.hasMatch(last)) { |
| 43 jsInteropFound = true; | |
| 44 } else if (_webComponentsJS.hasMatch(last)) { | |
| 45 webComponentsFound = true; | 41 webComponentsFound = true; |
| 46 } else if (last == 'dart.js') { | 42 } else if (last == 'dart.js') { |
| 47 dartJs = tag; | 43 dartJs = tag; |
| 48 } | 44 } |
| 49 } | 45 } |
| 50 | 46 |
| 51 if (tag.attributes['type'] == 'application/dart') { | 47 if (tag.attributes['type'] == 'application/dart') { |
| 52 dartScripts.add(tag); | 48 dartScripts.add(tag); |
| 53 } | 49 } |
| 54 } | 50 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 81 } else if (dartJs == null) { | 77 } else if (dartJs == null) { |
| 82 document.body.nodes.add(parseFragment( | 78 document.body.nodes.add(parseFragment( |
| 83 '<script src="packages/browser/dart.js"></script>')); | 79 '<script src="packages/browser/dart.js"></script>')); |
| 84 } | 80 } |
| 85 | 81 |
| 86 _addScriptFirst(urlSegment) { | 82 _addScriptFirst(urlSegment) { |
| 87 document.head.nodes.insert(0, parseFragment( | 83 document.head.nodes.insert(0, parseFragment( |
| 88 '<script src="packages/$urlSegment"></script>\n')); | 84 '<script src="packages/$urlSegment"></script>\n')); |
| 89 } | 85 } |
| 90 | 86 |
| 91 // JS interop code is required for Polymer CSS shimming. | |
| 92 if (!jsInteropFound) _addScriptFirst('browser/interop.js'); | |
| 93 | |
| 94 var suffix = options.releaseMode ? '.js' : '.concat.js'; | 87 var suffix = options.releaseMode ? '.js' : '.concat.js'; |
| 95 if (!webComponentsFound) { | 88 if (!webComponentsFound) { |
| 96 _addScriptFirst('web_components/dart_support.js'); | 89 _addScriptFirst('web_components/dart_support.js'); |
| 97 | 90 |
| 98 // platform.js should come before all other scripts. | 91 // platform.js should come before all other scripts. |
| 99 _addScriptFirst('web_components/platform$suffix'); | 92 _addScriptFirst('web_components/platform$suffix'); |
| 100 } | 93 } |
| 101 | 94 |
| 102 transform.addOutput( | 95 transform.addOutput( |
| 103 new Asset.fromString(transform.primaryInput.id, document.outerHtml)); | 96 new Asset.fromString(transform.primaryInput.id, document.outerHtml)); |
| 104 }); | 97 }); |
| 105 } | 98 } |
| 106 } | 99 } |
| 107 | 100 |
| 108 final _webComponentsJS = new RegExp(r'platform.*\.js', | 101 final _webComponentsJS = new RegExp(r'platform.*\.js', |
| 109 caseSensitive: false); | 102 caseSensitive: false); |
| OLD | NEW |