OLD | NEW |
| (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 extracts inlined script code into separate assets. */ | |
6 library polymer.src.transformers; | |
7 | |
8 import 'dart:async'; | |
9 | |
10 import 'package:analyzer_experimental/src/generated/ast.dart'; | |
11 import 'package:analyzer_experimental/src/generated/error.dart'; | |
12 import 'package:analyzer_experimental/src/generated/parser.dart'; | |
13 import 'package:analyzer_experimental/src/generated/scanner.dart'; | |
14 import 'package:barback/barback.dart'; | |
15 import 'package:path/path.dart' as path; | |
16 | |
17 import 'common.dart'; | |
18 | |
19 /** | |
20 * Transformer that extracts Dart code inlined in HTML script tags and outputs a | |
21 * separate file for each. | |
22 */ | |
23 class InlineCodeExtractor extends Transformer with PolymerTransformer { | |
24 final TransformOptions options; | |
25 | |
26 InlineCodeExtractor(this.options); | |
27 | |
28 /** Only run this transformer on .html files. */ | |
29 final String allowedExtensions = ".html"; | |
30 | |
31 Future apply(Transform transform) { | |
32 var input = transform.primaryInput; | |
33 var id = transform.primaryInput.id; | |
34 return readPrimaryAsHtml(transform).then((document) { | |
35 int count = 0; | |
36 bool htmlChanged = false; | |
37 for (var tag in document.queryAll('script')) { | |
38 // Only process tags that have inline Dart code | |
39 if (tag.attributes['type'] != 'application/dart' || | |
40 tag.attributes.containsKey('src')) { | |
41 continue; | |
42 } | |
43 htmlChanged = true; | |
44 | |
45 // Remove empty tags | |
46 if (tag.nodes.length == 0) { | |
47 tag.remove(); | |
48 continue; | |
49 } | |
50 | |
51 var filename = path.url.basename(id.path); | |
52 // TODO(sigmund): ensure this filename is unique (dartbug.com/12618). | |
53 tag.attributes['src'] = '$filename.$count.dart'; | |
54 var textContent = tag.nodes.first; | |
55 var code = textContent.value; | |
56 var newId = id.addExtension('.$count.dart'); | |
57 if (!_hasLibraryDirective(code)) { | |
58 var libname = path.withoutExtension(newId.path) | |
59 .replaceAll(new RegExp('[-./]'), '_'); | |
60 code = "library $libname;\n$code"; | |
61 } | |
62 transform.addOutput(new Asset.fromString(newId, code)); | |
63 textContent.remove(); | |
64 count++; | |
65 } | |
66 transform.addOutput( | |
67 htmlChanged ? new Asset.fromString(id, document.outerHtml) : input); | |
68 }); | |
69 } | |
70 } | |
71 | |
72 /** Parse [code] and determine whether it has a library directive. */ | |
73 bool _hasLibraryDirective(String code) { | |
74 var errorListener = new _ErrorCollector(); | |
75 var token = new StringScanner(null, code, errorListener).tokenize(); | |
76 var unit = new Parser(null, errorListener).parseCompilationUnit(token); | |
77 return unit.directives.any((d) => d is LibraryDirective); | |
78 } | |
79 | |
80 class _ErrorCollector extends AnalysisErrorListener { | |
81 final errors = <AnalysisError>[]; | |
82 onError(error) => errors.add(error); | |
83 } | |
OLD | NEW |