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

Side by Side Diff: packages/polymer/lib/src/build/polyfill_injector.dart

Issue 2312183003: Removed Polymer from Observatory deps (Closed)
Patch Set: Created 4 years, 3 months 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
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:html/dom.dart'
12 show Document, DocumentFragment, Element, Node;
13 import 'package:html/parser.dart' show parseFragment;
14 import 'package:code_transformers/messages/build_logger.dart';
15 import 'package:path/path.dart' as path;
16 import 'package:web_components/transformer.dart' show testAttribute;
17 import 'common.dart';
18
19 /// Ensures that any scripts and polyfills needed to run a polymer application
20 /// are included.
21 ///
22 /// 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.
24 class PolyfillInjector extends Transformer with PolymerTransformer {
25 final TransformOptions options;
26
27 PolyfillInjector(this.options);
28
29 /// Only run on entry point .html files.
30 bool isPrimary(AssetId id) => options.isHtmlEntryPoint(id);
31
32 Future apply(Transform transform) {
33 var logger = new BuildLogger(transform,
34 convertErrorsToWarnings: !options.releaseMode,
35 detailsUri: 'http://goo.gl/5HPeuP');
36 return readPrimaryAsHtml(transform, logger).then((document) {
37 bool dartSupportFound = false;
38 Element webComponentsJs;
39 Element dartJs;
40 final dartScripts = <Element>[];
41
42 for (var tag in document.querySelectorAll('script')) {
43 var src = tag.attributes['src'];
44 if (src != null) {
45 var last = src.split('/').last;
46 if (_webComponentsJS.hasMatch(last)) {
47 webComponentsJs = tag;
48 } else if (_platformJS.hasMatch(last)) {
49 tag.attributes['src'] =
50 src.replaceFirst(_platformJS, 'webcomponents.min.js');
51 webComponentsJs = tag;
52 } else if (_dartSupportJS.hasMatch(last)) {
53 dartSupportFound = true;
54 } else if (_browserDartJS.hasMatch(src)) {
55 dartJs = tag;
56 }
57 }
58
59 if (tag.attributes['type'] == 'application/dart') {
60 dartScripts.add(tag);
61 }
62 }
63
64 if (dartScripts.isEmpty) {
65 // This HTML has no Dart code, there is nothing to do here.
66 transform.addOutput(transform.primaryInput);
67 return;
68 }
69
70 // Remove "packages/browser/dart.js". It is not needed in release mode,
71 // and in debug mode we want to ensure it is the last script on the page.
72 if (dartJs != null) dartJs.remove();
73
74 // TODO(jmesserly): ideally we would generate an HTML that loads
75 // dart2dart too. But for now dart2dart is not a supported deployment
76 // target, so just inline the JS script. This has the nice side effect of
77 // fixing our tests: even if content_shell supports Dart VM, we'll still
78 // test the compiled JS code.
79 if (options.directlyIncludeJS) {
80 // Replace all other Dart script tags with JavaScript versions.
81 for (var script in dartScripts) {
82 /// Don't modify scripts that were originally <link rel="x-test-dart">
83 /// tags.
84 if (script.attributes.containsKey(testAttribute)) continue;
85
86 final src = script.attributes['src'];
87 if (src.endsWith('.dart')) {
88 script.attributes.remove('type');
89 script.attributes['src'] = '$src.js';
90 // TODO(sigmund): we shouldn't need 'async' here. Remove this
91 // workaround for dartbug.com/19653.
92 script.attributes['async'] = '';
93 }
94 }
95 } else {
96 document.body.nodes.add(
97 parseFragment('<script src="packages/browser/dart.js"></script>'));
98 }
99
100 _addScript(urlSegment, [Element parent, int position]) {
101 if (parent == null) parent = document.head;
102 // Default to either the top of `parent` or right after the <base> tag.
103 if (position == null) {
104 var base = parent.querySelector('base');
105 if (base != null) {
106 position = parent.nodes.indexOf(base) + 1;
107 } else {
108 position = 0;
109 }
110 }
111 var pathToPackages =
112 '../' * (path.url.split(transform.primaryInput.id.path).length - 2);
113 parent.nodes.insert(position, parseFragment(
114 '<script src="${pathToPackages}packages/$urlSegment"></script>'));
115 }
116
117 // Inserts dart_support.js either at the top of the document or directly
118 // after webcomponents.js if it exists.
119 if (!dartSupportFound) {
120 if (webComponentsJs == null) {
121 _addScript('web_components/dart_support.js');
122 } else {
123 var parentNode = webComponentsJs.parentNode;
124 _addScript('web_components/dart_support.js', parentNode,
125 parentNode.nodes.indexOf(webComponentsJs) + 1);
126 }
127 }
128
129 // By default webcomponents.js should come before all other scripts.
130 if (webComponentsJs == null && options.injectWebComponentsJs) {
131 var suffix = options.releaseMode ? '.min.js' : '.js';
132 _addScript('web_components/webcomponents$suffix');
133 }
134
135 transform.addOutput(
136 new Asset.fromString(transform.primaryInput.id, document.outerHtml));
137 });
138 }
139 }
140
141 final _platformJS = new RegExp(r'platform.*\.js', caseSensitive: false);
142 final _webComponentsJS =
143 new RegExp(r'webcomponents.*\.js', caseSensitive: false);
144 final _dartSupportJS = new RegExp(r'dart_support.js', caseSensitive: false);
145 final _browserDartJS =
146 new RegExp(r'packages/browser/dart.js', caseSensitive: false);
OLDNEW
« no previous file with comments | « packages/polymer/lib/src/build/messages.dart ('k') | packages/polymer/lib/src/build/polymer_bootstrap.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698