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

Unified Diff: pkg/polymer/lib/src/transform/code_extractor.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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/polymer/lib/src/transform.dart ('k') | pkg/polymer/lib/src/transform/common.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/polymer/lib/src/transform/code_extractor.dart
diff --git a/pkg/polymer/lib/src/transform/code_extractor.dart b/pkg/polymer/lib/src/transform/code_extractor.dart
new file mode 100644
index 0000000000000000000000000000000000000000..14ff6522c24422209b96626f8ac36687a6c1ce97
--- /dev/null
+++ b/pkg/polymer/lib/src/transform/code_extractor.dart
@@ -0,0 +1,46 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+/** Transfomer that extracts inlined script code into separate assets. */
+library polymer.src.transformers;
+
+import 'dart:async';
+
+import 'package:barback/barback.dart';
+import 'package:path/path.dart' as path;
+
+import 'common.dart';
+
+/**
+ * Transformer that extracts Dart code inlined in HTML script tags and outputs a
+ * separate file for each.
+ */
+class InlineCodeExtractor extends Transformer {
+ Future<bool> isPrimary(Asset input) =>
+ new Future.value(input.id.extension == ".html");
+
+ Future apply(Transform transform) {
+ var inputId = transform.primaryId;
+ return getPrimaryContent(transform).then((content) {
+ var document = parseHtml(content, inputId.path, transform.logger);
+ int count = 0;
+ for (var tag in document.queryAll('script')) {
+ if (tag.attributes['type'] == 'application/dart' &&
+ !tag.attributes.containsKey('src')) {
+ // TODO(sigmund): should we automatically include a library directive
+ // if it doesn't have one?
+ var filename = path.basename(inputId.path);
+ tag.attributes['src'] = '$filename.$count.dart';
+ var textContent = tag.nodes.first;
+ var id = inputId.addExtension('.$count.dart');
+ transform.addOutput(new Asset.fromString(id, textContent.value));
+ textContent.remove();
+ count++;
+ }
+ }
+ transform.addOutput(new Asset.fromString(inputId,
+ count == 0 ? content : document.outerHtml));
+ });
+ }
+}
« no previous file with comments | « pkg/polymer/lib/src/transform.dart ('k') | pkg/polymer/lib/src/transform/common.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698