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

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

Issue 22935016: Introduce polymer transformers (inlined code extraction, inlining html imports, (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
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 | Annotate | Revision Log
« no previous file with comments | « pkg/polymer/lib/src/transform/import_inliner.dart ('k') | pkg/polymer/test/run_all.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.
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. The '
48 'ScriptCompactor transformer should run after running the '
49 'InlineCodeExtractor', tag.sourceSpan);
50 continue;
51 }
52 var libraryId = resolve(id, src, logger, tag.sourceSpan);
53
54 // TODO(sigmund): should we detect/remove duplicates?
55 if (libraryId == null) continue;
56 libraries.add(libraryId);
57 }
58
59 if (!changed) {
60 transform.addOutput(new Asset.fromString(id, content));
61 return;
62 }
63
64 var bootstrapId = id.addExtension('_bootstrap.dart');
65 var filename = path.basename(bootstrapId.path);
66 document.body.nodes.add(parseFragment(
67 '<script type="application/dart" src="$filename"></script>'));
68
69 var urls = libraries.map((id) => importUrlFor(id, bootstrapId, logger))
70 .where((url) => url != null).toList();
71 var buffer = new StringBuffer()..write(_header);
72 for (int i = 0; i < urls.length; i++) {
73 buffer.writeln("import '${urls[i]}' as i$i;");
74 }
75 buffer..write(_mainPrefix)
76 ..writeAll(urls.map((url) => " '$url',\n"))
77 ..write(_mainSuffix);
78
79 transform.addOutput(new Asset.fromString(bootstrapId, buffer.toString()));
80 transform.addOutput(new Asset.fromString(id, document.outerHtml));
81 });
82 }
83
84 /**
85 * Generate the import url for a file described by [id], referenced by a file
86 * with [sourceId].
87 */
88 String importUrlFor(AssetId id, AssetId sourceId, TransformLogger logger) {
89 // use package: urls if possible
90 if (id.path.startsWith('lib/')) {
91 return 'package:${id.package}/${id.path.substring(4)}';
92 }
93
94 // Use relative urls only if it's possible.
95 if (id.package != sourceId.package) {
96 logger.error("don't know how to import $id from $sourceId");
97 return null;
98 }
99
100 var upPath = path.joinAll(path.split(sourceId.path).map((_) => '..'));
101 return path.normalize(path.join(sourceId.path, upPath, id.path));
102 }
103 }
104
105 const _header = """
106 library app_bootstrap;
107
108 import 'package:polymer/polymer.dart';
109 import 'dart:mirrors' show currentMirrorSystem;
110
111 """;
112
113 const _mainPrefix = """
114
115 void main() {
116 initPolymer([
117 """;
118
119 // TODO(sigmund): investigate alternative to get the baseUri (dartbug.com/12612)
120 const _mainSuffix = """
121 ],
122 currentMirrorSystem().findLibrary(const Symbol('app_bootstrap'))
123 .first.uri.toString());
124 }
125 """;
OLDNEW
« no previous file with comments | « pkg/polymer/lib/src/transform/import_inliner.dart ('k') | pkg/polymer/test/run_all.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698