| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** Transfomer that extracts inlined script code into separate assets. */ | 5 /// Transfomer that extracts inlined script code into separate assets. |
| 6 library polymer.src.build.code_extractor; | 6 library polymer.src.build.code_extractor; |
| 7 | 7 |
| 8 import 'dart:async'; | 8 import 'dart:async'; |
| 9 | 9 |
| 10 import 'package:analyzer/src/generated/ast.dart'; | 10 import 'package:analyzer/src/generated/ast.dart'; |
| 11 import 'package:analyzer/src/generated/error.dart'; | 11 import 'package:analyzer/src/generated/error.dart'; |
| 12 import 'package:analyzer/src/generated/parser.dart'; | 12 import 'package:analyzer/src/generated/parser.dart'; |
| 13 import 'package:analyzer/src/generated/scanner.dart'; | 13 import 'package:analyzer/src/generated/scanner.dart'; |
| 14 import 'package:barback/barback.dart'; | 14 import 'package:barback/barback.dart'; |
| 15 import 'package:path/path.dart' as path; | 15 import 'package:path/path.dart' as path; |
| 16 | 16 |
| 17 import 'common.dart'; | 17 import 'common.dart'; |
| 18 | 18 |
| 19 /** | 19 /// Transformer that extracts Dart code inlined in HTML script tags and outputs |
| 20 * Transformer that extracts Dart code inlined in HTML script tags and outputs a | 20 /// a separate file for each. |
| 21 * separate file for each. | |
| 22 */ | |
| 23 class InlineCodeExtractor extends Transformer with PolymerTransformer { | 21 class InlineCodeExtractor extends Transformer with PolymerTransformer { |
| 24 final TransformOptions options; | 22 final TransformOptions options; |
| 25 | 23 |
| 26 InlineCodeExtractor(this.options); | 24 InlineCodeExtractor(this.options); |
| 27 | 25 |
| 28 /** Only run this transformer on .html files. */ | 26 /// Only run this transformer on .html files. |
| 29 final String allowedExtensions = ".html"; | 27 final String allowedExtensions = ".html"; |
| 30 | 28 |
| 31 Future apply(Transform transform) { | 29 Future apply(Transform transform) { |
| 32 var input = transform.primaryInput; | 30 var input = transform.primaryInput; |
| 33 var id = transform.primaryInput.id; | 31 var id = transform.primaryInput.id; |
| 34 return readPrimaryAsHtml(transform).then((document) { | 32 return readPrimaryAsHtml(transform).then((document) { |
| 35 int count = 0; | 33 int count = 0; |
| 36 bool htmlChanged = false; | 34 bool htmlChanged = false; |
| 37 for (var tag in document.querySelectorAll('script')) { | 35 for (var tag in document.querySelectorAll('script')) { |
| 38 // Only process tags that have inline Dart code | 36 // Only process tags that have inline Dart code |
| (...skipping 23 matching lines...) Expand all Loading... |
| 62 transform.addOutput(new Asset.fromString(newId, code)); | 60 transform.addOutput(new Asset.fromString(newId, code)); |
| 63 textContent.remove(); | 61 textContent.remove(); |
| 64 count++; | 62 count++; |
| 65 } | 63 } |
| 66 transform.addOutput( | 64 transform.addOutput( |
| 67 htmlChanged ? new Asset.fromString(id, document.outerHtml) : input); | 65 htmlChanged ? new Asset.fromString(id, document.outerHtml) : input); |
| 68 }); | 66 }); |
| 69 } | 67 } |
| 70 } | 68 } |
| 71 | 69 |
| 72 /** Parse [code] and determine whether it has a library directive. */ | 70 /// Parse [code] and determine whether it has a library directive. |
| 73 bool _hasLibraryDirective(String code) { | 71 bool _hasLibraryDirective(String code) { |
| 74 var errorListener = new _ErrorCollector(); | 72 var errorListener = new _ErrorCollector(); |
| 75 var reader = new CharSequenceReader(code); | 73 var reader = new CharSequenceReader(code); |
| 76 var scanner = new Scanner(null, reader, errorListener); | 74 var scanner = new Scanner(null, reader, errorListener); |
| 77 var token = scanner.tokenize(); | 75 var token = scanner.tokenize(); |
| 78 var unit = new Parser(null, errorListener).parseCompilationUnit(token); | 76 var unit = new Parser(null, errorListener).parseCompilationUnit(token); |
| 79 return unit.directives.any((d) => d is LibraryDirective); | 77 return unit.directives.any((d) => d is LibraryDirective); |
| 80 } | 78 } |
| 81 | 79 |
| 82 class _ErrorCollector extends AnalysisErrorListener { | 80 class _ErrorCollector extends AnalysisErrorListener { |
| 83 final errors = <AnalysisError>[]; | 81 final errors = <AnalysisError>[]; |
| 84 onError(error) => errors.add(error); | 82 onError(error) => errors.add(error); |
| 85 } | 83 } |
| OLD | NEW |