| 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.build.script_compactor; | 6 library polymer.src.build.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 28 matching lines...) Expand all Loading... |
| 39 Future apply(Transform transform) { | 39 Future apply(Transform transform) { |
| 40 var id = transform.primaryInput.id; | 40 var id = transform.primaryInput.id; |
| 41 var logger = transform.logger; | 41 var logger = transform.logger; |
| 42 return readPrimaryAsHtml(transform).then((document) { | 42 return readPrimaryAsHtml(transform).then((document) { |
| 43 var libraries = []; | 43 var libraries = []; |
| 44 bool changed = false; | 44 bool changed = false; |
| 45 var dartLoaderTag = null; | 45 var dartLoaderTag = null; |
| 46 for (var tag in document.queryAll('script')) { | 46 for (var tag in document.queryAll('script')) { |
| 47 var src = tag.attributes['src']; | 47 var src = tag.attributes['src']; |
| 48 if (src != null) { | 48 if (src != null) { |
| 49 if (src == 'packages/polymer/boot.js') { | 49 if (src == 'packages/polymer/boot.js' || |
| 50 src == 'packages/polymer/init.dart') { |
| 50 tag.remove(); | 51 tag.remove(); |
| 51 continue; | 52 continue; |
| 52 } | 53 } |
| 53 var last = src.split('/').last; | 54 var last = src.split('/').last; |
| 54 if (last == 'dart.js') { | 55 if (last == 'dart.js') { |
| 55 dartLoaderTag = tag; | 56 dartLoaderTag = tag; |
| 56 } | 57 } |
| 57 } | 58 } |
| 58 if (tag.attributes['type'] != 'application/dart') continue; | 59 if (tag.attributes['type'] != 'application/dart') continue; |
| 59 tag.remove(); | 60 tag.remove(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 78 | 79 |
| 79 var bootstrapId = id.addExtension('_bootstrap.dart'); | 80 var bootstrapId = id.addExtension('_bootstrap.dart'); |
| 80 var filename = path.url.basename(bootstrapId.path); | 81 var filename = path.url.basename(bootstrapId.path); |
| 81 | 82 |
| 82 var bootstrapScript = parseFragment( | 83 var bootstrapScript = parseFragment( |
| 83 '<script type="application/dart" src="$filename"></script>'); | 84 '<script type="application/dart" src="$filename"></script>'); |
| 84 if (dartLoaderTag == null) { | 85 if (dartLoaderTag == null) { |
| 85 document.body.nodes.add(bootstrapScript); | 86 document.body.nodes.add(bootstrapScript); |
| 86 document.body.nodes.add(parseFragment( | 87 document.body.nodes.add(parseFragment( |
| 87 '<script src="packages/browser/dart.js"></script>')); | 88 '<script src="packages/browser/dart.js"></script>')); |
| 88 } else if (dartLoaderTag.parent != document.body) { | |
| 89 document.body.nodes.add(bootstrapScript); | |
| 90 } else { | 89 } else { |
| 91 document.body.insertBefore(bootstrapScript, dartLoaderTag); | 90 dartLoaderTag.parent.insertBefore(bootstrapScript, dartLoaderTag); |
| 92 } | 91 } |
| 93 | 92 |
| 94 var urls = libraries.map((id) => assetUrlFor(id, bootstrapId, logger)) | 93 var urls = libraries.map((id) => assetUrlFor(id, bootstrapId, logger)) |
| 95 .where((url) => url != null).toList(); | 94 .where((url) => url != null).toList(); |
| 96 var buffer = new StringBuffer()..writeln(MAIN_HEADER); | 95 var buffer = new StringBuffer()..writeln(MAIN_HEADER); |
| 97 for (int i = 0; i < urls.length; i++) { | 96 for (int i = 0; i < urls.length; i++) { |
| 98 buffer.writeln("import '${urls[i]}' as i$i;"); | 97 buffer.writeln("import '${urls[i]}' as i$i;"); |
| 99 } | 98 } |
| 100 buffer..write(_mainPrefix) | 99 buffer..write(_mainPrefix) |
| 101 ..writeAll(urls.map((url) => " '$url',\n")) | 100 ..writeAll(urls.map((url) => " '$url',\n")) |
| 102 ..write(_mainSuffix); | 101 ..write(_mainSuffix); |
| 103 | 102 |
| 104 transform.addOutput(new Asset.fromString(bootstrapId, buffer.toString())); | 103 transform.addOutput(new Asset.fromString(bootstrapId, buffer.toString())); |
| 105 transform.addOutput(new Asset.fromString(id, document.outerHtml)); | 104 transform.addOutput(new Asset.fromString(id, document.outerHtml)); |
| 106 }); | 105 }); |
| 107 } | 106 } |
| 108 } | 107 } |
| 109 | 108 |
| 110 const MAIN_HEADER = """ | 109 const MAIN_HEADER = """ |
| 111 library app_bootstrap; | 110 library app_bootstrap; |
| 112 | 111 |
| 113 import 'package:polymer/polymer.dart'; | 112 import 'package:polymer/polymer.dart'; |
| 114 @MirrorsUsed(symbols: 'main', override: 'app_bootstrap') | |
| 115 import 'dart:mirrors' show currentMirrorSystem, MirrorsUsed; | |
| 116 """; | 113 """; |
| 117 | 114 |
| 118 const _mainPrefix = """ | 115 const _mainPrefix = """ |
| 119 | 116 |
| 120 void main() { | 117 void main() { |
| 121 initPolymer([ | 118 initPolymer([ |
| 122 """; | 119 """; |
| 123 | 120 |
| 124 // TODO(sigmund): investigate alternative to get the baseUri (dartbug.com/12612) | |
| 125 const _mainSuffix = """ | 121 const _mainSuffix = """ |
| 126 ], currentMirrorSystem().isolate.rootLibrary.uri.toString()); | 122 ]); |
| 127 } | 123 } |
| 128 """; | 124 """; |
| OLD | NEW |