| 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 11 matching lines...) Expand all Loading... |
| 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 /** Only run on entry point .html files. */ | 31 /** Only run on entry point .html files. */ |
| 32 Future<bool> isPrimary(Asset input) => isPrimaryHtml(input.id); | 32 Future<bool> isPrimary(Asset input) => |
| 33 new Future.value(isPrimaryHtml(input.id)); |
| 33 | 34 |
| 34 Future apply(Transform transform) { | 35 Future apply(Transform transform) { |
| 35 var id = transform.primaryInput.id; | 36 var id = transform.primaryInput.id; |
| 36 var logger = transform.logger; | 37 var logger = transform.logger; |
| 37 return transform.primaryInput.readAsString().then((content) { | 38 return readPrimaryAsHtml(transform).then((document) { |
| 38 var document = parseHtml(content, id.path, logger, | |
| 39 checkDocType: false); | |
| 40 var libraries = []; | 39 var libraries = []; |
| 41 bool changed = false; | 40 bool changed = false; |
| 42 var dartLoaderTag = null; | 41 var dartLoaderTag = null; |
| 43 for (var tag in document.queryAll('script')) { | 42 for (var tag in document.queryAll('script')) { |
| 44 var src = tag.attributes['src']; | 43 var src = tag.attributes['src']; |
| 45 if (src != null) { | 44 if (src != null) { |
| 46 if (src == 'packages/polymer/boot.js') { | 45 if (src == 'packages/polymer/boot.js') { |
| 47 tag.remove(); | 46 tag.remove(); |
| 48 continue; | 47 continue; |
| 49 } | 48 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 62 continue; | 61 continue; |
| 63 } | 62 } |
| 64 var libraryId = resolve(id, src, logger, tag.sourceSpan); | 63 var libraryId = resolve(id, src, logger, tag.sourceSpan); |
| 65 | 64 |
| 66 // TODO(sigmund): should we detect/remove duplicates? | 65 // TODO(sigmund): should we detect/remove duplicates? |
| 67 if (libraryId == null) continue; | 66 if (libraryId == null) continue; |
| 68 libraries.add(libraryId); | 67 libraries.add(libraryId); |
| 69 } | 68 } |
| 70 | 69 |
| 71 if (!changed) { | 70 if (!changed) { |
| 72 transform.addOutput(new Asset.fromString(id, content)); | 71 transform.addOutput(transform.primaryInput); |
| 73 return; | 72 return; |
| 74 } | 73 } |
| 75 | 74 |
| 76 var bootstrapId = id.addExtension('_bootstrap.dart'); | 75 var bootstrapId = id.addExtension('_bootstrap.dart'); |
| 77 var filename = path.url.basename(bootstrapId.path); | 76 var filename = path.url.basename(bootstrapId.path); |
| 78 | 77 |
| 79 var bootstrapScript = parseFragment( | 78 var bootstrapScript = parseFragment( |
| 80 '<script type="application/dart" src="$filename"></script>'); | 79 '<script type="application/dart" src="$filename"></script>'); |
| 81 if (dartLoaderTag == null) { | 80 if (dartLoaderTag == null) { |
| 82 document.body.nodes.add(bootstrapScript); | 81 document.body.nodes.add(bootstrapScript); |
| 83 document.body.nodes.add(parseFragment( | 82 document.body.nodes.add(parseFragment( |
| 84 '<script src="packages/browser/dart.js"></script>')); | 83 '<script src="packages/browser/dart.js"></script>')); |
| 85 } else if (dartLoaderTag.parent != document.body) { | 84 } else if (dartLoaderTag.parent != document.body) { |
| 86 document.body.nodes.add(bootstrapScript); | 85 document.body.nodes.add(bootstrapScript); |
| 87 } else { | 86 } else { |
| 88 document.body.insertBefore(bootstrapScript, dartLoaderTag); | 87 document.body.insertBefore(bootstrapScript, dartLoaderTag); |
| 89 } | 88 } |
| 90 | 89 |
| 91 var urls = libraries.map((id) => importUrlFor(id, bootstrapId, logger)) | 90 var urls = libraries.map((id) => assetUrlFor(id, bootstrapId, logger, |
| 91 allowAssetUrl: false)) |
| 92 .where((url) => url != null).toList(); | 92 .where((url) => url != null).toList(); |
| 93 var buffer = new StringBuffer()..write(_header); | 93 var buffer = new StringBuffer()..write(_header); |
| 94 for (int i = 0; i < urls.length; i++) { | 94 for (int i = 0; i < urls.length; i++) { |
| 95 buffer.writeln("import '${urls[i]}' as i$i;"); | 95 buffer.writeln("import '${urls[i]}' as i$i;"); |
| 96 } | 96 } |
| 97 buffer..write(_mainPrefix) | 97 buffer..write(_mainPrefix) |
| 98 ..writeAll(urls.map((url) => " '$url',\n")) | 98 ..writeAll(urls.map((url) => " '$url',\n")) |
| 99 ..write(_mainSuffix); | 99 ..write(_mainSuffix); |
| 100 | 100 |
| 101 transform.addOutput(new Asset.fromString(bootstrapId, buffer.toString())); | 101 transform.addOutput(new Asset.fromString(bootstrapId, buffer.toString())); |
| 102 transform.addOutput(new Asset.fromString(id, document.outerHtml)); | 102 transform.addOutput(new Asset.fromString(id, document.outerHtml)); |
| 103 }); | 103 }); |
| 104 } | 104 } |
| 105 | |
| 106 /** | |
| 107 * Generate the import url for a file described by [id], referenced by a file | |
| 108 * with [sourceId]. | |
| 109 */ | |
| 110 String importUrlFor(AssetId id, AssetId sourceId, TransformLogger logger) { | |
| 111 // use package: urls if possible | |
| 112 if (id.path.startsWith('lib/')) { | |
| 113 return 'package:${id.package}/${id.path.substring(4)}'; | |
| 114 } | |
| 115 | |
| 116 // Use relative urls only if it's possible. | |
| 117 if (id.package != sourceId.package) { | |
| 118 logger.error("don't know how to import $id from $sourceId"); | |
| 119 return null; | |
| 120 } | |
| 121 | |
| 122 var builder = path.url; | |
| 123 return builder.relative(builder.join('/', id.path), | |
| 124 from: builder.join('/', builder.dirname(sourceId.path))); | |
| 125 } | |
| 126 } | 105 } |
| 127 | 106 |
| 128 const _header = """ | 107 const _header = """ |
| 129 library app_bootstrap; | 108 library app_bootstrap; |
| 130 | 109 |
| 131 import 'package:polymer/polymer.dart'; | 110 import 'package:polymer/polymer.dart'; |
| 132 import 'dart:mirrors' show currentMirrorSystem; | 111 import 'dart:mirrors' show currentMirrorSystem; |
| 133 | 112 |
| 134 """; | 113 """; |
| 135 | 114 |
| 136 const _mainPrefix = """ | 115 const _mainPrefix = """ |
| 137 | 116 |
| 138 void main() { | 117 void main() { |
| 139 initPolymer([ | 118 initPolymer([ |
| 140 """; | 119 """; |
| 141 | 120 |
| 142 // TODO(sigmund): investigate alternative to get the baseUri (dartbug.com/12612) | 121 // TODO(sigmund): investigate alternative to get the baseUri (dartbug.com/12612) |
| 143 const _mainSuffix = """ | 122 const _mainSuffix = """ |
| 144 ], currentMirrorSystem().isolate.rootLibrary.uri.toString()); | 123 ], currentMirrorSystem().isolate.rootLibrary.uri.toString()); |
| 145 } | 124 } |
| 146 """; | 125 """; |
| OLD | NEW |