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

Side by Side Diff: observatory_pub_packages/polymer/src/build/polyfill_injector.dart

Issue 816693004: Add observatory_pub_packages snapshot to third_party (Closed) Base URL: http://dart.googlecode.com/svn/third_party/
Patch Set: Created 6 years 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) 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
3 // BSD-style license that can be found in the LICENSE file.
4
5 /// Includes any additional polyfills that may needed by the deployed app.
6 library polymer.src.build.polyfill_injector;
7
8 import 'dart:async';
9
10 import 'package:barback/barback.dart';
11 import 'package:html5lib/dom.dart' show
12 Document, DocumentFragment, Element, Node;
13 import 'package:html5lib/parser.dart' show parseFragment;
14 import 'package:code_transformers/messages/build_logger.dart';
15 import 'common.dart';
16
17 /// Ensures that any scripts and polyfills needed to run a polymer application
18 /// are included.
19 ///
20 /// 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.
22 class PolyfillInjector extends Transformer with PolymerTransformer {
23 final TransformOptions options;
24
25 PolyfillInjector(this.options);
26
27 /// Only run on entry point .html files.
28 // TODO(nweiz): This should just take an AssetId when barback <0.13.0 support
29 // is dropped.
30 Future<bool> isPrimary(idOrAsset) {
31 var id = idOrAsset is AssetId ? idOrAsset : idOrAsset.id;
32 return new Future.value(options.isHtmlEntryPoint(id));
33 }
34
35 Future apply(Transform transform) {
36 var logger = new BuildLogger(transform,
37 convertErrorsToWarnings: !options.releaseMode,
38 detailsUri: 'http://goo.gl/5HPeuP');
39 return readPrimaryAsHtml(transform, logger).then((document) {
40 bool dartSupportFound = false;
41 Element webComponentsJs;
42 Element dartJs;
43 final dartScripts = <Element>[];
44
45 for (var tag in document.querySelectorAll('script')) {
46 var src = tag.attributes['src'];
47 if (src != null) {
48 var last = src.split('/').last;
49 if (_webComponentsJS.hasMatch(last)) {
50 webComponentsJs = tag;
51 } else if (_platformJS.hasMatch(last)) {
52 tag.attributes['src'] = src.replaceFirst(
53 _platformJS, 'webcomponents.min.js');
54 webComponentsJs = tag;
55 } else if (_dartSupportJS.hasMatch(last)) {
56 dartSupportFound = true;
57 } else if (last == 'dart.js') {
58 dartJs = tag;
59 }
60 }
61
62 if (tag.attributes['type'] == 'application/dart') {
63 dartScripts.add(tag);
64 }
65 }
66
67 if (dartScripts.isEmpty) {
68 // This HTML has no Dart code, there is nothing to do here.
69 transform.addOutput(transform.primaryInput);
70 return;
71 }
72
73 // Remove "packages/browser/dart.js". It is not needed in release mode,
74 // and in debug mode we want to ensure it is the last script on the page.
75 if (dartJs != null) dartJs.remove();
76
77 // TODO(jmesserly): ideally we would generate an HTML that loads
78 // dart2dart too. But for now dart2dart is not a supported deployment
79 // target, so just inline the JS script. This has the nice side effect of
80 // fixing our tests: even if content_shell supports Dart VM, we'll still
81 // test the compiled JS code.
82 if (options.directlyIncludeJS) {
83 // Replace all other Dart script tags with JavaScript versions.
84 for (var script in dartScripts) {
85 final src = script.attributes['src'];
86 if (src.endsWith('.dart')) {
87 script.attributes.remove('type');
88 script.attributes['src'] = '$src.js';
89 // TODO(sigmund): we shouldn't need 'async' here. Remove this
90 // workaround for dartbug.com/19653.
91 script.attributes['async'] = '';
92 }
93 }
94 } else {
95 document.body.nodes.add(parseFragment(
96 '<script src="packages/browser/dart.js"></script>'));
97 }
98
99 _addScriptFirst(urlSegment) {
100 document.head.nodes.insert(0, parseFragment(
101 '<script src="packages/$urlSegment"></script>\n'));
102 }
103
104 // Inserts dart_support.js either at the top of the document or directly
105 // after webcomponents.js if it exists.
106 if (!dartSupportFound) {
107 if (webComponentsJs == null) {
108 _addScriptFirst('web_components/dart_support.js');
109 } else {
110 var parentsNodes = webComponentsJs.parentNode.nodes;
111 parentsNodes.insert(
112 parentsNodes.indexOf(webComponentsJs) + 1,
113 parseFragment(
114 '\n<script src="packages/web_components/dart_support.js">'
115 '</script>'));
116 }
117 }
118
119 // By default webcomponents.js should come before all other scripts.
120 if (webComponentsJs == null && options.injectWebComponentsJs) {
121 var suffix = options.releaseMode ? '.min.js' : '.js';
122 _addScriptFirst('web_components/webcomponents$suffix');
123 }
124
125 transform.addOutput(
126 new Asset.fromString(transform.primaryInput.id, document.outerHtml));
127 });
128 }
129 }
130
131 final _platformJS = new RegExp(r'platform.*\.js', caseSensitive: false);
132 final _webComponentsJS =
133 new RegExp(r'webcomponents.*\.js', caseSensitive: false);
134 final _dartSupportJS = new RegExp(r'dart_support.js', caseSensitive: false);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698