| 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'; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 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 { |
| 31 Future<bool> isPrimary(Asset input) => | 31 /** Only run this transformer on .html files. */ |
| 32 new Future.value(input.id.extension == ".html"); | 32 final String allowedExtensions = ".html"; |
| 33 | 33 |
| 34 Future apply(Transform transform) { | 34 Future apply(Transform transform) { |
| 35 var id = transform.primaryId; | 35 var id = transform.primaryId; |
| 36 var logger = transform.logger; | 36 var logger = transform.logger; |
| 37 return getPrimaryContent(transform).then((content) { | 37 return getPrimaryContent(transform).then((content) { |
| 38 var document = parseHtml(content, id.path, logger); | 38 var document = parseHtml(content, id.path, logger); |
| 39 var libraries = []; | 39 var libraries = []; |
| 40 bool changed = false; | 40 bool changed = false; |
| 41 var dartLoaderTag = null; | 41 var dartLoaderTag = null; |
| 42 for (var tag in document.queryAll('script')) { | 42 for (var tag in document.queryAll('script')) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 72 return; | 72 return; |
| 73 } | 73 } |
| 74 | 74 |
| 75 var bootstrapId = id.addExtension('_bootstrap.dart'); | 75 var bootstrapId = id.addExtension('_bootstrap.dart'); |
| 76 var filename = path.url.basename(bootstrapId.path); | 76 var filename = path.url.basename(bootstrapId.path); |
| 77 | 77 |
| 78 var bootstrapScript = parseFragment( | 78 var bootstrapScript = parseFragment( |
| 79 '<script type="application/dart" src="$filename"></script>'); | 79 '<script type="application/dart" src="$filename"></script>'); |
| 80 if (dartLoaderTag == null) { | 80 if (dartLoaderTag == null) { |
| 81 document.body.nodes.add(bootstrapScript); | 81 document.body.nodes.add(bootstrapScript); |
| 82 document.body.nodes.add(parseFragment('<script type="text/javascript" ' | 82 document.body.nodes.add(parseFragment( |
| 83 'src="packages/browser/dart.js"></script>')); | 83 '<script src="packages/browser/dart.js"></script>')); |
| 84 } else if (dartLoaderTag.parent != document.body) { | 84 } else if (dartLoaderTag.parent != document.body) { |
| 85 document.body.nodes.add(bootstrapScript); | 85 document.body.nodes.add(bootstrapScript); |
| 86 } else { | 86 } else { |
| 87 document.body.insertBefore(bootstrapScript, dartLoaderTag); | 87 document.body.insertBefore(bootstrapScript, dartLoaderTag); |
| 88 } | 88 } |
| 89 | 89 |
| 90 var urls = libraries.map((id) => importUrlFor(id, bootstrapId, logger)) | 90 var urls = libraries.map((id) => importUrlFor(id, bootstrapId, logger)) |
| 91 .where((url) => url != null).toList(); | 91 .where((url) => url != null).toList(); |
| 92 var buffer = new StringBuffer()..write(_header); | 92 var buffer = new StringBuffer()..write(_header); |
| 93 for (int i = 0; i < urls.length; i++) { | 93 for (int i = 0; i < urls.length; i++) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 | 136 |
| 137 void main() { | 137 void main() { |
| 138 initPolymer([ | 138 initPolymer([ |
| 139 """; | 139 """; |
| 140 | 140 |
| 141 // TODO(sigmund): investigate alternative to get the baseUri (dartbug.com/12612) | 141 // TODO(sigmund): investigate alternative to get the baseUri (dartbug.com/12612) |
| 142 const _mainSuffix = """ | 142 const _mainSuffix = """ |
| 143 ], currentMirrorSystem().isolate.rootLibrary.uri.toString()); | 143 ], currentMirrorSystem().isolate.rootLibrary.uri.toString()); |
| 144 } | 144 } |
| 145 """; | 145 """; |
| OLD | NEW |