Chromium Code Reviews| 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 entrypoint .html files under web or test. */ | 31 /** Only run on entrypoint .html files under web or test. */ |
| 32 Future<bool> isPrimary(Asset input) => isHtmlInWebOrTest(input.id); | 32 Future<bool> isPrimary(Asset input) => |
| 33 new Future.value(isHtmlInWebOrTest(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 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; |
| 48 } | 48 } |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 61 continue; | 61 continue; |
| 62 } | 62 } |
| 63 var libraryId = resolve(id, src, logger, tag.sourceSpan); | 63 var libraryId = resolve(id, src, logger, tag.sourceSpan); |
| 64 | 64 |
| 65 // TODO(sigmund): should we detect/remove duplicates? | 65 // TODO(sigmund): should we detect/remove duplicates? |
| 66 if (libraryId == null) continue; | 66 if (libraryId == null) continue; |
| 67 libraries.add(libraryId); | 67 libraries.add(libraryId); |
| 68 } | 68 } |
| 69 | 69 |
| 70 if (!changed) { | 70 if (!changed) { |
| 71 transform.addOutput(new Asset.fromString(id, content)); | 71 transform.addOutput(transform.primaryInput); |
| 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( | 82 document.body.nodes.add(parseFragment( |
| 83 '<script 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) => assetUrlFor(id, bootstrapId, logger)) |
|
Jennifer Messerly
2013/09/04 03:13:27
as noted earlier, I don't think we want Dart files
Siggi Cherem (dart-lang)
2013/09/04 17:57:27
good point. added a check for that here.
| |
| 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++) { |
| 94 buffer.writeln("import '${urls[i]}' as i$i;"); | 94 buffer.writeln("import '${urls[i]}' as i$i;"); |
| 95 } | 95 } |
| 96 buffer..write(_mainPrefix) | 96 buffer..write(_mainPrefix) |
| 97 ..writeAll(urls.map((url) => " '$url',\n")) | 97 ..writeAll(urls.map((url) => " '$url',\n")) |
| 98 ..write(_mainSuffix); | 98 ..write(_mainSuffix); |
| 99 | 99 |
| 100 transform.addOutput(new Asset.fromString(bootstrapId, buffer.toString())); | 100 transform.addOutput(new Asset.fromString(bootstrapId, buffer.toString())); |
| 101 transform.addOutput(new Asset.fromString(id, document.outerHtml)); | 101 transform.addOutput(new Asset.fromString(id, document.outerHtml)); |
| 102 }); | 102 }); |
| 103 } | 103 } |
| 104 | |
| 105 /** | |
| 106 * Generate the import url for a file described by [id], referenced by a file | |
| 107 * with [sourceId]. | |
| 108 */ | |
| 109 String importUrlFor(AssetId id, AssetId sourceId, TransformLogger logger) { | |
| 110 // use package: urls if possible | |
| 111 if (id.path.startsWith('lib/')) { | |
| 112 return 'package:${id.package}/${id.path.substring(4)}'; | |
| 113 } | |
| 114 | |
| 115 // Use relative urls only if it's possible. | |
| 116 if (id.package != sourceId.package) { | |
| 117 logger.error("don't know how to import $id from $sourceId"); | |
| 118 return null; | |
| 119 } | |
| 120 | |
| 121 var builder = path.url; | |
| 122 return builder.relative(builder.join('/', id.path), | |
| 123 from: builder.join('/', builder.dirname(sourceId.path))); | |
| 124 } | |
| 125 } | 104 } |
| 126 | 105 |
| 127 const _header = """ | 106 const _header = """ |
| 128 library app_bootstrap; | 107 library app_bootstrap; |
| 129 | 108 |
| 130 import 'package:polymer/polymer.dart'; | 109 import 'package:polymer/polymer.dart'; |
| 131 import 'dart:mirrors' show currentMirrorSystem; | 110 import 'dart:mirrors' show currentMirrorSystem; |
| 132 | 111 |
| 133 """; | 112 """; |
| 134 | 113 |
| 135 const _mainPrefix = """ | 114 const _mainPrefix = """ |
| 136 | 115 |
| 137 void main() { | 116 void main() { |
| 138 initPolymer([ | 117 initPolymer([ |
| 139 """; | 118 """; |
| 140 | 119 |
| 141 // TODO(sigmund): investigate alternative to get the baseUri (dartbug.com/12612) | 120 // TODO(sigmund): investigate alternative to get the baseUri (dartbug.com/12612) |
| 142 const _mainSuffix = """ | 121 const _mainSuffix = """ |
| 143 ], currentMirrorSystem().isolate.rootLibrary.uri.toString()); | 122 ], currentMirrorSystem().isolate.rootLibrary.uri.toString()); |
| 144 } | 123 } |
| 145 """; | 124 """; |
| OLD | NEW |