| 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 combines multiple dart script tags into a single one. | 5 /// Transfomer that combines multiple dart script tags into a single one. |
| 6 library polymer.src.build.script_compactor; | 6 library polymer.src.build.script_compactor; |
| 7 | 7 |
| 8 import 'dart:async'; | 8 import 'dart:async'; |
| 9 import 'dart:convert'; | 9 import 'dart:convert'; |
| 10 | 10 |
| 11 import 'package:analyzer/src/generated/ast.dart'; | 11 import 'package:analyzer/src/generated/ast.dart'; |
| 12 import 'package:analyzer/src/generated/error.dart'; | |
| 13 import 'package:analyzer/src/generated/parser.dart'; | |
| 14 import 'package:analyzer/src/generated/scanner.dart'; | |
| 15 import 'package:barback/barback.dart'; | 12 import 'package:barback/barback.dart'; |
| 16 import 'package:path/path.dart' as path; | 13 import 'package:path/path.dart' as path; |
| 17 import 'package:source_maps/span.dart' show SourceFile; | 14 import 'package:source_maps/span.dart' show SourceFile; |
| 18 | 15 |
| 19 import 'code_extractor.dart'; // import just for documentation. | 16 import 'import_inliner.dart' show ImportInliner; // just for docs. |
| 20 import 'common.dart'; | 17 import 'common.dart'; |
| 21 | 18 |
| 22 /// Combines Dart script tags into a single script tag, and creates a new Dart | 19 /// Combines Dart script tags into a single script tag, and creates a new Dart |
| 23 /// file that calls the main function of each of the original script tags. | 20 /// file that calls the main function of each of the original script tags. |
| 24 /// | 21 /// |
| 25 /// This transformer assumes that all script tags point to external files. To | 22 /// This transformer assumes that all script tags point to external files. To |
| 26 /// support script tags with inlined code, use this transformer after running | 23 /// support script tags with inlined code, use this transformer after running |
| 27 /// [InlineCodeExtractor] on an earlier phase. | 24 /// [ImportInliner] on an earlier phase. |
| 28 /// | 25 /// |
| 29 /// Internally, this transformer will convert each script tag into an import | 26 /// Internally, this transformer will convert each script tag into an import |
| 30 /// statement to a library, and then uses `initPolymer` (see polymer.dart) to | 27 /// statement to a library, and then uses `initPolymer` (see polymer.dart) to |
| 31 /// process `@initMethod` and `@CustomTag` annotations in those libraries. | 28 /// process `@initMethod` and `@CustomTag` annotations in those libraries. |
| 32 class ScriptCompactor extends Transformer with PolymerTransformer { | 29 class ScriptCompactor extends Transformer with PolymerTransformer { |
| 33 final TransformOptions options; | 30 final TransformOptions options; |
| 34 | 31 |
| 35 ScriptCompactor(this.options); | 32 ScriptCompactor(this.options); |
| 36 | 33 |
| 37 /// Only run on entry point .html files. | 34 /// Only run on entry point .html files. |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 } | 119 } |
| 123 | 120 |
| 124 /// Computes the initializers of [dartLibrary]. That is, a closure that calls | 121 /// Computes the initializers of [dartLibrary]. That is, a closure that calls |
| 125 /// Polymer.register for each @CustomTag, and any public top-level methods | 122 /// Polymer.register for each @CustomTag, and any public top-level methods |
| 126 /// labeled with @initMethod. | 123 /// labeled with @initMethod. |
| 127 Future<List<_Initializer>> _initializersOf( | 124 Future<List<_Initializer>> _initializersOf( |
| 128 AssetId dartLibrary, Transform transform, TransformLogger logger) { | 125 AssetId dartLibrary, Transform transform, TransformLogger logger) { |
| 129 var initializers = []; | 126 var initializers = []; |
| 130 return transform.readInputAsString(dartLibrary).then((code) { | 127 return transform.readInputAsString(dartLibrary).then((code) { |
| 131 var file = new SourceFile.text(_simpleUriForSource(dartLibrary), code); | 128 var file = new SourceFile.text(_simpleUriForSource(dartLibrary), code); |
| 132 var unit = _parseCompilationUnit(code); | 129 var unit = parseCompilationUnit(code); |
| 133 | 130 |
| 134 return Future.forEach(unit.directives, (directive) { | 131 return Future.forEach(unit.directives, (directive) { |
| 135 // Include anything from parts. | 132 // Include anything from parts. |
| 136 if (directive is PartDirective) { | 133 if (directive is PartDirective) { |
| 137 var targetId = resolve(dartLibrary, directive.uri.stringValue, | 134 var targetId = resolve(dartLibrary, directive.uri.stringValue, |
| 138 logger, _getSpan(file, directive)); | 135 logger, _getSpan(file, directive)); |
| 139 return _initializersOf(targetId, transform, logger) | 136 return _initializersOf(targetId, transform, logger) |
| 140 .then(initializers.addAll); | 137 .then(initializers.addAll); |
| 141 } | 138 } |
| 142 | 139 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 var name = function.name.name; | 214 var name = function.name.name; |
| 218 if (name.startsWith('_')) { | 215 if (name.startsWith('_')) { |
| 219 logger.error('@initMethod is no longer supported on private ' | 216 logger.error('@initMethod is no longer supported on private ' |
| 220 'functions: $name', span: _getSpan(file, function.name)); | 217 'functions: $name', span: _getSpan(file, function.name)); |
| 221 return; | 218 return; |
| 222 } | 219 } |
| 223 initializers.add(new _InitMethodInitializer(name)); | 220 initializers.add(new _InitMethodInitializer(name)); |
| 224 } | 221 } |
| 225 } | 222 } |
| 226 | 223 |
| 227 /// Parse [code] using analyzer. | |
| 228 CompilationUnit _parseCompilationUnit(String code) { | |
| 229 var errorListener = new _ErrorCollector(); | |
| 230 var reader = new CharSequenceReader(code); | |
| 231 var scanner = new Scanner(null, reader, errorListener); | |
| 232 var token = scanner.tokenize(); | |
| 233 var parser = new Parser(null, errorListener); | |
| 234 return parser.parseCompilationUnit(token); | |
| 235 } | |
| 236 | |
| 237 class _ErrorCollector extends AnalysisErrorListener { | |
| 238 final errors = <AnalysisError>[]; | |
| 239 onError(error) => errors.add(error); | |
| 240 } | |
| 241 | |
| 242 // TODO(sigmund): consider support for importing annotations with prefixes. | 224 // TODO(sigmund): consider support for importing annotations with prefixes. |
| 243 bool _isInitMethodAnnotation(Annotation node) => | 225 bool _isInitMethodAnnotation(Annotation node) => |
| 244 node.name.name == 'initMethod' && node.constructorName == null && | 226 node.name.name == 'initMethod' && node.constructorName == null && |
| 245 node.arguments == null; | 227 node.arguments == null; |
| 246 bool _isCustomTagAnnotation(Annotation node) => node.name.name == 'CustomTag'; | 228 bool _isCustomTagAnnotation(Annotation node) => node.name.name == 'CustomTag'; |
| 247 | 229 |
| 248 abstract class _Initializer { | 230 abstract class _Initializer { |
| 249 String get symbolName; | 231 String get symbolName; |
| 250 String asCode(String prefix); | 232 String asCode(String prefix); |
| 251 } | 233 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 268 "() => Polymer.register('$tagName', $prefix.$typeName)"; | 250 "() => Polymer.register('$tagName', $prefix.$typeName)"; |
| 269 } | 251 } |
| 270 | 252 |
| 271 _getSpan(SourceFile file, ASTNode node) => file.span(node.offset, node.end); | 253 _getSpan(SourceFile file, ASTNode node) => file.span(node.offset, node.end); |
| 272 | 254 |
| 273 const MAIN_HEADER = """ | 255 const MAIN_HEADER = """ |
| 274 library app_bootstrap; | 256 library app_bootstrap; |
| 275 | 257 |
| 276 import 'package:polymer/polymer.dart'; | 258 import 'package:polymer/polymer.dart'; |
| 277 """; | 259 """; |
| OLD | NEW |