Index: lib/src/transform/code_extractor.dart |
diff --git a/lib/src/transform/code_extractor.dart b/lib/src/transform/code_extractor.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ec06e32b005b69fbce873a6779b49c84b5d60ccd |
--- /dev/null |
+++ b/lib/src/transform/code_extractor.dart |
@@ -0,0 +1,45 @@ |
+// 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:path/path.dart' as path; |
Jennifer Messerly
2013/08/19 22:57:26
not sure if you want to sort these, noticed a few
Siggi Cherem (dart-lang)
2013/08/21 20:35:42
Done.
|
+import 'package:barback/barback.dart'; |
+import 'common.dart'; |
+ |
+/** |
+ * Transformer that extracts Dart code inlined in HTML script tags and outputs a |
+ * separate file for each. |
+ */ |
+class InlineCodeExtractor extends Transformer { |
terry
2013/08/20 17:03:24
Does Barback has the ability to pass options for t
Bob Nystrom
2013/08/20 17:20:05
No, it doesn't currently have a way to handle conf
|
+ 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) { |
Bob Nystrom
2013/08/19 22:28:59
Add a TODO mentioning #12515 or #12516 here since
Siggi Cherem (dart-lang)
2013/08/21 20:35:42
awesome. Done. moved the TODOs to the definition o
|
+ 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'); |
Jennifer Messerly
2013/08/19 22:57:26
do we need to worry about this name conflicting wi
Siggi Cherem (dart-lang)
2013/08/21 20:35:42
It's mainly on our hands to worry about finding a
Bob Nystrom
2013/08/21 21:18:21
Similarly, you need to choose something that is no
|
+ transform.addOutput(new Asset.fromString(id, textContent.value)); |
terry
2013/08/20 17:03:24
Can barback produce an output stream. Might want
Bob Nystrom
2013/08/20 17:20:05
Yes, a barback Asset exposes a stream interface yo
|
+ textContent.remove(); |
+ count++; |
+ } |
+ } |
+ transform.addOutput(new Asset.fromString(inputId, |
+ count == 0 ? content : document.outerHtml)); |
Bob Nystrom
2013/08/19 22:28:59
Just indent +4 here or all the way to the "(".
Siggi Cherem (dart-lang)
2013/08/21 20:35:42
Done. Thanks, vi-mode for Dart is not perfect yet
|
+ }); |
+ } |
+} |