| 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.transform.script_compactor; | 6 library polymer.src.transform.script_compactor; |
| 7 | 7 |
| 8 import 'dart:async'; | 8 import 'dart:async'; |
| 9 | 9 |
| 10 import 'package:barback/barback.dart'; | 10 import 'package:barback/barback.dart'; |
| 11 import 'package:html5lib/parser.dart' show parseFragment; | 11 import 'package:html5lib/parser.dart' show parseFragment; |
| 12 import 'package:path/path.dart' as path; | 12 import 'package:path/path.dart' as path; |
| 13 | 13 |
| 14 import 'code_extractor.dart'; // import just for documentation. | 14 import 'code_extractor.dart'; // import just for documentation. |
| 15 import 'common.dart'; | 15 import 'common.dart'; |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * Combines Dart script tags into a single script tag, and creates a new Dart | 18 * Combines Dart script tags into a single script tag, and creates a new Dart |
| 19 * file that calls the main function of each of the original script tags. | 19 * file that calls the main function of each of the original script tags. |
| 20 * | 20 * |
| 21 * This transformer assumes that all script tags point to external files. To | 21 * This transformer assumes that all script tags point to external files. To |
| 22 * support script tags with inlined code, use this transformer after running | 22 * support script tags with inlined code, use this transformer after running |
| 23 * [InlineCodeExtractor] on an earlier phase. | 23 * [InlineCodeExtractor] on an earlier phase. |
| 24 * | 24 * |
| 25 * Internally, this transformer will convert each script tag into an import | 25 * Internally, this transformer will convert each script tag into an import |
| 26 * statement to a library, and then uses `initPolymer` (see polymer.dart) to | 26 * statement to a library, and then uses `initPolymer` (see polymer.dart) to |
| 27 * invoke the main method on each of these libraries and register any polymer | 27 * invoke the main method on each of these libraries and register any polymer |
| 28 * elements annotated with `@CustomTag`. | 28 * elements annotated with `@CustomTag`. |
| 29 */ | 29 */ |
| 30 class ScriptCompactor extends Transformer { | 30 class ScriptCompactor extends Transformer with PolymerTransformer { |
| 31 final TransformOptions options; |
| 32 |
| 33 ScriptCompactor(this.options); |
| 34 |
| 31 /** Only run on entry point .html files. */ | 35 /** Only run on entry point .html files. */ |
| 32 Future<bool> isPrimary(Asset input) => | 36 Future<bool> isPrimary(Asset input) => |
| 33 new Future.value(isPrimaryHtml(input.id)); | 37 new Future.value(options.isHtmlEntryPoint(input.id)); |
| 34 | 38 |
| 35 Future apply(Transform transform) { | 39 Future apply(Transform transform) { |
| 36 var id = transform.primaryInput.id; | 40 var id = transform.primaryInput.id; |
| 37 var logger = transform.logger; | 41 var logger = transform.logger; |
| 38 return readPrimaryAsHtml(transform).then((document) { | 42 return readPrimaryAsHtml(transform).then((document) { |
| 39 var libraries = []; | 43 var libraries = []; |
| 40 bool changed = false; | 44 bool changed = false; |
| 41 var dartLoaderTag = null; | 45 var dartLoaderTag = null; |
| 42 for (var tag in document.queryAll('script')) { | 46 for (var tag in document.queryAll('script')) { |
| 43 var src = tag.attributes['src']; | 47 var src = tag.attributes['src']; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 | 119 |
| 116 void main() { | 120 void main() { |
| 117 initPolymer([ | 121 initPolymer([ |
| 118 """; | 122 """; |
| 119 | 123 |
| 120 // TODO(sigmund): investigate alternative to get the baseUri (dartbug.com/12612) | 124 // TODO(sigmund): investigate alternative to get the baseUri (dartbug.com/12612) |
| 121 const _mainSuffix = """ | 125 const _mainSuffix = """ |
| 122 ], currentMirrorSystem().isolate.rootLibrary.uri.toString()); | 126 ], currentMirrorSystem().isolate.rootLibrary.uri.toString()); |
| 123 } | 127 } |
| 124 """; | 128 """; |
| OLD | NEW |