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

Side by Side Diff: pkg/polymer/lib/src/transform/script_compactor.dart

Issue 23452010: Add a polymer validator: a linter/analysis that will replace the old (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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 | Annotate | Revision Log
« no previous file with comments | « pkg/polymer/lib/src/transform/polyfill_injector.dart ('k') | pkg/polymer/test/linter_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 /** Transfomer that combines multiple dart script tags into a single one. */ 5 /** Transfomer that combines multiple dart script tags into a single one. */
6 library polymer.src.transform.script_compactor; 6 library polymer.src.transform.script_compactor;
7 7
8 import 'dart:async'; 8 import 'dart:async';
9 9
10 import 'package:barback/barback.dart'; 10 import 'package:barback/barback.dart';
(...skipping 11 matching lines...) Expand all
22 * support script tags with inlined code, use this transformer after running 22 * support script tags with inlined code, use this transformer after running
23 * [InlineCodeExtractor] on an earlier phase. 23 * [InlineCodeExtractor] on an earlier phase.
24 * 24 *
25 * Internally, this transformer will convert each script tag into an import 25 * Internally, this transformer will convert each script tag into an import
26 * statement to a library, and then uses `initPolymer` (see polymer.dart) to 26 * statement to a library, and then uses `initPolymer` (see polymer.dart) to
27 * invoke the main method on each of these libraries and register any polymer 27 * invoke the main method on each of these libraries and register any polymer
28 * elements annotated with `@CustomTag`. 28 * elements annotated with `@CustomTag`.
29 */ 29 */
30 class ScriptCompactor extends Transformer { 30 class ScriptCompactor extends Transformer {
31 /** Only run on entry point .html files. */ 31 /** Only run on entry point .html files. */
32 Future<bool> isPrimary(Asset input) => isPrimaryHtml(input.id); 32 Future<bool> isPrimary(Asset input) =>
33 new Future.value(isPrimaryHtml(input.id));
33 34
34 Future apply(Transform transform) { 35 Future apply(Transform transform) {
35 var id = transform.primaryInput.id; 36 var id = transform.primaryInput.id;
36 var logger = transform.logger; 37 var logger = transform.logger;
37 return transform.primaryInput.readAsString().then((content) { 38 return readPrimaryAsHtml(transform).then((document) {
38 var document = parseHtml(content, id.path, logger,
39 checkDocType: false);
40 var libraries = []; 39 var libraries = [];
41 bool changed = false; 40 bool changed = false;
42 var dartLoaderTag = null; 41 var dartLoaderTag = null;
43 for (var tag in document.queryAll('script')) { 42 for (var tag in document.queryAll('script')) {
44 var src = tag.attributes['src']; 43 var src = tag.attributes['src'];
45 if (src != null) { 44 if (src != null) {
46 if (src == 'packages/polymer/boot.js') { 45 if (src == 'packages/polymer/boot.js') {
47 tag.remove(); 46 tag.remove();
48 continue; 47 continue;
49 } 48 }
(...skipping 12 matching lines...) Expand all
62 continue; 61 continue;
63 } 62 }
64 var libraryId = resolve(id, src, logger, tag.sourceSpan); 63 var libraryId = resolve(id, src, logger, tag.sourceSpan);
65 64
66 // TODO(sigmund): should we detect/remove duplicates? 65 // TODO(sigmund): should we detect/remove duplicates?
67 if (libraryId == null) continue; 66 if (libraryId == null) continue;
68 libraries.add(libraryId); 67 libraries.add(libraryId);
69 } 68 }
70 69
71 if (!changed) { 70 if (!changed) {
72 transform.addOutput(new Asset.fromString(id, content)); 71 transform.addOutput(transform.primaryInput);
73 return; 72 return;
74 } 73 }
75 74
76 var bootstrapId = id.addExtension('_bootstrap.dart'); 75 var bootstrapId = id.addExtension('_bootstrap.dart');
77 var filename = path.url.basename(bootstrapId.path); 76 var filename = path.url.basename(bootstrapId.path);
78 77
79 var bootstrapScript = parseFragment( 78 var bootstrapScript = parseFragment(
80 '<script type="application/dart" src="$filename"></script>'); 79 '<script type="application/dart" src="$filename"></script>');
81 if (dartLoaderTag == null) { 80 if (dartLoaderTag == null) {
82 document.body.nodes.add(bootstrapScript); 81 document.body.nodes.add(bootstrapScript);
83 document.body.nodes.add(parseFragment( 82 document.body.nodes.add(parseFragment(
84 '<script src="packages/browser/dart.js"></script>')); 83 '<script src="packages/browser/dart.js"></script>'));
85 } else if (dartLoaderTag.parent != document.body) { 84 } else if (dartLoaderTag.parent != document.body) {
86 document.body.nodes.add(bootstrapScript); 85 document.body.nodes.add(bootstrapScript);
87 } else { 86 } else {
88 document.body.insertBefore(bootstrapScript, dartLoaderTag); 87 document.body.insertBefore(bootstrapScript, dartLoaderTag);
89 } 88 }
90 89
91 var urls = libraries.map((id) => importUrlFor(id, bootstrapId, logger)) 90 var urls = libraries.map((id) => assetUrlFor(id, bootstrapId, logger))
92 .where((url) => url != null).toList(); 91 .where((url) => url != null).toList();
93 var buffer = new StringBuffer()..write(_header); 92 var buffer = new StringBuffer()..write(_header);
94 for (int i = 0; i < urls.length; i++) { 93 for (int i = 0; i < urls.length; i++) {
95 buffer.writeln("import '${urls[i]}' as i$i;"); 94 buffer.writeln("import '${urls[i]}' as i$i;");
96 } 95 }
97 buffer..write(_mainPrefix) 96 buffer..write(_mainPrefix)
98 ..writeAll(urls.map((url) => " '$url',\n")) 97 ..writeAll(urls.map((url) => " '$url',\n"))
99 ..write(_mainSuffix); 98 ..write(_mainSuffix);
100 99
101 transform.addOutput(new Asset.fromString(bootstrapId, buffer.toString())); 100 transform.addOutput(new Asset.fromString(bootstrapId, buffer.toString()));
102 transform.addOutput(new Asset.fromString(id, document.outerHtml)); 101 transform.addOutput(new Asset.fromString(id, document.outerHtml));
103 }); 102 });
104 } 103 }
105
106 /**
107 * Generate the import url for a file described by [id], referenced by a file
108 * with [sourceId].
109 */
110 String importUrlFor(AssetId id, AssetId sourceId, TransformLogger logger) {
111 // use package: urls if possible
112 if (id.path.startsWith('lib/')) {
113 return 'package:${id.package}/${id.path.substring(4)}';
114 }
115
116 // Use relative urls only if it's possible.
117 if (id.package != sourceId.package) {
118 logger.error("don't know how to import $id from $sourceId");
119 return null;
120 }
121
122 var builder = path.url;
123 return builder.relative(builder.join('/', id.path),
124 from: builder.join('/', builder.dirname(sourceId.path)));
125 }
126 } 104 }
127 105
128 const _header = """ 106 const _header = """
129 library app_bootstrap; 107 library app_bootstrap;
130 108
131 import 'package:polymer/polymer.dart'; 109 import 'package:polymer/polymer.dart';
132 import 'dart:mirrors' show currentMirrorSystem; 110 import 'dart:mirrors' show currentMirrorSystem;
133 111
134 """; 112 """;
135 113
136 const _mainPrefix = """ 114 const _mainPrefix = """
137 115
138 void main() { 116 void main() {
139 initPolymer([ 117 initPolymer([
140 """; 118 """;
141 119
142 // TODO(sigmund): investigate alternative to get the baseUri (dartbug.com/12612) 120 // TODO(sigmund): investigate alternative to get the baseUri (dartbug.com/12612)
143 const _mainSuffix = """ 121 const _mainSuffix = """
144 ], currentMirrorSystem().isolate.rootLibrary.uri.toString()); 122 ], currentMirrorSystem().isolate.rootLibrary.uri.toString());
145 } 123 }
146 """; 124 """;
OLDNEW
« no previous file with comments | « pkg/polymer/lib/src/transform/polyfill_injector.dart ('k') | pkg/polymer/test/linter_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698