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

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

Issue 22825012: Introduce transformers for: (Closed) Base URL: git@github.com:dart-lang/web-ui.git@master
Patch Set: Created 7 years, 4 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 /** Transfomer that combines multiple dart script tags into a single one. */
6 library polymer.src.transform.script_compactor;
7
8 import 'dart:async';
9
10 import 'package:barback/barback.dart';
11 import 'package:html5lib/parser.dart' show parseFragment;
12 import 'package:path/path.dart' as path;
13
14 import 'code_extractor.dart'; // import just for documentation.
terry 2013/08/20 17:03:24 If this import isn't used the editor will flag a w
Siggi Cherem (dart-lang) 2013/08/21 20:35:42 the good news is that they also recognize that the
15 import 'common.dart';
16
17 /**
18 * Combines Dart script tags into a single script tag, and creates a new Dart
19 * file that calls the main function of each of the original script tags.
20 *
21 * This transformer assumes that all script tags point to external files. To
22 * support script tags with inlined code, use this transformer after running
23 * [InlineCodeExtractor] on an earlier phase.
24 *
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
27 * invoke the main method on each of these libraries and register any polymer
28 * elements annotated with `@CustomTag`.
29 */
30 class ScriptCompactor extends Transformer {
31 Future<bool> isPrimary(Asset input) =>
32 new Future.value(input.id.extension == ".html");
33
34 Future apply(Transform transform) {
35 var id = transform.primaryId;
36 var logger = transform.logger;
37 return getPrimaryContent(transform).then((content) {
38 var document = parseHtml(content, id.path, logger);
39 var libraries = [];
40 bool changed = false;
41 for (var tag in document.queryAll('script')) {
42 if (tag.attributes['type'] != 'application/dart') continue;
43 tag.remove();
44 changed = true;
45 var src = tag.attributes['src'];
46 if (src == null) {
47 logger.warning('unexpected script without a src url', tag.sourceSpan);
Jennifer Messerly 2013/08/19 22:57:26 Hmm, why the warning here? Are we assuming the scr
Siggi Cherem (dart-lang) 2013/08/21 20:35:42 Good question. The transformers should always be r
48 continue;
49 }
50 var libraryId = resolve(id, src, logger, tag.sourceSpan);
51
52 // TODO(sigmund): should we detect/remove duplicates?
Jennifer Messerly 2013/08/19 22:57:26 i think so. don't want to generate erroneous code
Siggi Cherem (dart-lang) 2013/08/21 20:35:42 But then, wouldn't it be ok to import it twice? (w
Jennifer Messerly 2013/08/21 23:35:29 good point, importing twice would be fine assuming
53 if (libraryId == null) continue;
54 libraries.add(libraryId);
55 }
56
57 if (!changed) {
58 transform.addOutput(new Asset.fromString(id, content));
59 return;
60 }
61
62 var bootstrapId = id.addExtension('_bootstrap.dart');
63 var filename = path.basename(bootstrapId.path);
64 document.body.nodes.add(parseFragment(
65 '<script type="application/dart" src="$filename"></script>'));
66
67 var urls = libraries.map((id) => importUrlFor(id, bootstrapId, logger))
68 .where((url) => url != null).toList();
69 var buffer = new StringBuffer()..write(_header);
70 for (int i = 0; i < urls.length; i++) {
71 buffer.writeln("import '${urls[i]}' as i$i;");
72 }
73 buffer..write(_mainPrefix)
74 ..writeAll(urls.map((url) => " '$url',\n"))
75 ..write(_mainSuffix);
76
77 transform.addOutput(new Asset.fromString(bootstrapId, buffer.toString()));
78 transform.addOutput(new Asset.fromString(id, document.outerHtml));
79 });
80 }
81
82 /**
83 * Generate the import url for a file described by [id], referenced by a file
84 * with [sourceId].
85 */
86 String importUrlFor(AssetId id, AssetId sourceId, TransformLogger logger) {
87 // use package: urls if possible
88 if (id.path.startsWith('lib/')) {
89 return 'package:${id.package}/${id.path.substring(4)}';
90 }
91
92 // Use relative urls only if it's possible.
93 if (id.package != sourceId.package) {
94 logger.error("don't know how to import $id from $sourceId");
95 return null;
96 }
97
98 var upPath = path.joinAll(path.split(sourceId.path).map((_) => '..'));
99 return path.normalize(path.join(sourceId.path, upPath, id.path));
100 }
101 }
102
103 const _header = """
104 library app_bootstrap;
105
106 import 'package:polymer/polymer.dart';
107 import 'dart:mirrors' show currentMirrorSystem;
108
109 """;
110
111 const _mainPrefix = """
112
113 void main() {
114 initPolymer([
115 """;
116
117 const _mainSuffix = """
118 ],
119 currentMirrorSystem().findLibrary(const Symbol('app_bootstrap'))
Jennifer Messerly 2013/08/19 22:57:26 seems unfortunate to need to use mirrors for this.
Siggi Cherem (dart-lang) 2013/08/21 20:35:42 good question. I opened a bug to investigate.
Jennifer Messerly 2013/08/21 23:35:29 btw, I discovered another pattern using mirrors th
120 .first.uri.toString());
121 }
122 """;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698