| 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 14 matching lines...) Expand all Loading... |
| 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 Future<bool> isPrimary(Asset input) => |
| 32 new Future.value(input.id.extension == ".html"); | 32 new Future.value(input.id.extension == ".html"); |
| 33 | 33 |
| 34 Future apply(Transform transform) { | 34 Future apply(Transform transform) { |
| 35 var id = transform.primaryId; | 35 var id = transform.primaryInput.id; |
| 36 var logger = transform.logger; | 36 var logger = transform.logger; |
| 37 return getPrimaryContent(transform).then((content) { | 37 return transform.primaryInput.readAsString().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')) { |
| 43 var src = tag.attributes['src']; | 43 var src = tag.attributes['src']; |
| 44 if (src != null) { | 44 if (src != null) { |
| 45 if (src == 'packages/polymer/boot.js') { | 45 if (src == 'packages/polymer/boot.js') { |
| 46 tag.remove(); | 46 tag.remove(); |
| 47 continue; | 47 continue; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 | 104 |
| 105 /** | 105 /** |
| 106 * Generate the import url for a file described by [id], referenced by a file | 106 * Generate the import url for a file described by [id], referenced by a file |
| 107 * with [sourceId]. | 107 * with [sourceId]. |
| 108 */ | 108 */ |
| 109 String importUrlFor(AssetId id, AssetId sourceId, TransformLogger logger) { | 109 String importUrlFor(AssetId id, AssetId sourceId, TransformLogger logger) { |
| 110 // use package: urls if possible | 110 // use package: urls if possible |
| 111 if (id.path.startsWith('lib/')) { | 111 if (id.path.startsWith('lib/')) { |
| 112 return 'package:${id.package}/${id.path.substring(4)}'; | 112 return 'package:${id.package}/${id.path.substring(4)}'; |
| 113 } | 113 } |
| 114 | 114 |
| 115 // Use relative urls only if it's possible. | 115 // Use relative urls only if it's possible. |
| 116 if (id.package != sourceId.package) { | 116 if (id.package != sourceId.package) { |
| 117 logger.error("don't know how to import $id from $sourceId"); | 117 logger.error("don't know how to import $id from $sourceId"); |
| 118 return null; | 118 return null; |
| 119 } | 119 } |
| 120 | 120 |
| 121 var builder = path.url; | 121 var builder = path.url; |
| 122 return builder.relative(builder.join('/', id.path), | 122 return builder.relative(builder.join('/', id.path), |
| 123 from: builder.join('/', builder.dirname(sourceId.path))); | 123 from: builder.join('/', builder.dirname(sourceId.path))); |
| 124 } | 124 } |
| (...skipping 11 matching lines...) Expand all 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 |