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

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

Issue 23757034: Rearranges the polymer package now that the old compiler is gone. (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/lib/transformer.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 with PolymerTransformer {
31 final TransformOptions options;
32
33 ScriptCompactor(this.options);
34
35 /** Only run on entry point .html files. */
36 Future<bool> isPrimary(Asset input) =>
37 new Future.value(options.isHtmlEntryPoint(input.id));
38
39 Future apply(Transform transform) {
40 var id = transform.primaryInput.id;
41 var logger = transform.logger;
42 return readPrimaryAsHtml(transform).then((document) {
43 var libraries = [];
44 bool changed = false;
45 var dartLoaderTag = null;
46 for (var tag in document.queryAll('script')) {
47 var src = tag.attributes['src'];
48 if (src != null) {
49 if (src == 'packages/polymer/boot.js') {
50 tag.remove();
51 continue;
52 }
53 var last = src.split('/').last;
54 if (last == 'dart.js') {
55 dartLoaderTag = tag;
56 }
57 }
58 if (tag.attributes['type'] != 'application/dart') continue;
59 tag.remove();
60 changed = true;
61 if (src == null) {
62 logger.warning('unexpected script without a src url. The '
63 'ScriptCompactor transformer should run after running the '
64 'InlineCodeExtractor', tag.sourceSpan);
65 continue;
66 }
67 var libraryId = resolve(id, src, logger, tag.sourceSpan);
68
69 // TODO(sigmund): should we detect/remove duplicates?
70 if (libraryId == null) continue;
71 libraries.add(libraryId);
72 }
73
74 if (!changed) {
75 transform.addOutput(transform.primaryInput);
76 return;
77 }
78
79 var bootstrapId = id.addExtension('_bootstrap.dart');
80 var filename = path.url.basename(bootstrapId.path);
81
82 var bootstrapScript = parseFragment(
83 '<script type="application/dart" src="$filename"></script>');
84 if (dartLoaderTag == null) {
85 document.body.nodes.add(bootstrapScript);
86 document.body.nodes.add(parseFragment(
87 '<script src="packages/browser/dart.js"></script>'));
88 } else if (dartLoaderTag.parent != document.body) {
89 document.body.nodes.add(bootstrapScript);
90 } else {
91 document.body.insertBefore(bootstrapScript, dartLoaderTag);
92 }
93
94 var urls = libraries.map((id) => assetUrlFor(id, bootstrapId, logger))
95 .where((url) => url != null).toList();
96 var buffer = new StringBuffer()..write(_header);
97 for (int i = 0; i < urls.length; i++) {
98 buffer.writeln("import '${urls[i]}' as i$i;");
99 }
100 buffer..write(_mainPrefix)
101 ..writeAll(urls.map((url) => " '$url',\n"))
102 ..write(_mainSuffix);
103
104 transform.addOutput(new Asset.fromString(bootstrapId, buffer.toString()));
105 transform.addOutput(new Asset.fromString(id, document.outerHtml));
106 });
107 }
108 }
109
110 const _header = """
111 library app_bootstrap;
112
113 import 'package:polymer/polymer.dart';
114 import 'dart:mirrors' show currentMirrorSystem;
115
116 """;
117
118 const _mainPrefix = """
119
120 void main() {
121 initPolymer([
122 """;
123
124 // TODO(sigmund): investigate alternative to get the baseUri (dartbug.com/12612)
125 const _mainSuffix = """
126 ], currentMirrorSystem().isolate.rootLibrary.uri.toString());
127 }
128 """;
OLDNEW
« no previous file with comments | « pkg/polymer/lib/src/transform/polyfill_injector.dart ('k') | pkg/polymer/lib/transformer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698