| 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 20 matching lines...) Expand all Loading... |
| 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.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 for (var tag in document.queryAll('script')) { | 42 for (var tag in document.queryAll('script')) { |
| 43 var src = tag.attributes['src']; |
| 44 if (src != null) { |
| 45 if (src == 'packages/polymer/boot.js') { |
| 46 tag.remove(); |
| 47 continue; |
| 48 } |
| 49 var last = src.split('/').last; |
| 50 if (last == 'dart.js' || last == 'testing.js') { |
| 51 dartLoaderTag = tag; |
| 52 } |
| 53 } |
| 42 if (tag.attributes['type'] != 'application/dart') continue; | 54 if (tag.attributes['type'] != 'application/dart') continue; |
| 43 tag.remove(); | 55 tag.remove(); |
| 44 changed = true; | 56 changed = true; |
| 45 var src = tag.attributes['src']; | |
| 46 if (src == null) { | 57 if (src == null) { |
| 47 logger.warning('unexpected script without a src url. The ' | 58 logger.warning('unexpected script without a src url. The ' |
| 48 'ScriptCompactor transformer should run after running the ' | 59 'ScriptCompactor transformer should run after running the ' |
| 49 'InlineCodeExtractor', tag.sourceSpan); | 60 'InlineCodeExtractor', tag.sourceSpan); |
| 50 continue; | 61 continue; |
| 51 } | 62 } |
| 52 var libraryId = resolve(id, src, logger, tag.sourceSpan); | 63 var libraryId = resolve(id, src, logger, tag.sourceSpan); |
| 53 | 64 |
| 54 // TODO(sigmund): should we detect/remove duplicates? | 65 // TODO(sigmund): should we detect/remove duplicates? |
| 55 if (libraryId == null) continue; | 66 if (libraryId == null) continue; |
| 56 libraries.add(libraryId); | 67 libraries.add(libraryId); |
| 57 } | 68 } |
| 58 | 69 |
| 59 if (!changed) { | 70 if (!changed) { |
| 60 transform.addOutput(new Asset.fromString(id, content)); | 71 transform.addOutput(new Asset.fromString(id, content)); |
| 61 return; | 72 return; |
| 62 } | 73 } |
| 63 | 74 |
| 64 var bootstrapId = id.addExtension('_bootstrap.dart'); | 75 var bootstrapId = id.addExtension('_bootstrap.dart'); |
| 65 var filename = path.url.basename(bootstrapId.path); | 76 var filename = path.url.basename(bootstrapId.path); |
| 66 document.body.nodes.add(parseFragment( | 77 |
| 67 '<script type="application/dart" src="$filename"></script>')); | 78 var bootstrapScript = parseFragment( |
| 79 '<script type="application/dart" src="$filename"></script>'); |
| 80 if (dartLoaderTag == null) { |
| 81 document.body.nodes.add(bootstrapScript); |
| 82 document.body.nodes.add(parseFragment('<script type="text/javascript" ' |
| 83 'src="packages/browser/dart.js"></script>')); |
| 84 } else if (dartLoaderTag.parent != document.body) { |
| 85 document.body.nodes.add(bootstrapScript); |
| 86 } else { |
| 87 document.body.insertBefore(bootstrapScript, dartLoaderTag); |
| 88 } |
| 68 | 89 |
| 69 var urls = libraries.map((id) => importUrlFor(id, bootstrapId, logger)) | 90 var urls = libraries.map((id) => importUrlFor(id, bootstrapId, logger)) |
| 70 .where((url) => url != null).toList(); | 91 .where((url) => url != null).toList(); |
| 71 var buffer = new StringBuffer()..write(_header); | 92 var buffer = new StringBuffer()..write(_header); |
| 72 for (int i = 0; i < urls.length; i++) { | 93 for (int i = 0; i < urls.length; i++) { |
| 73 buffer.writeln("import '${urls[i]}' as i$i;"); | 94 buffer.writeln("import '${urls[i]}' as i$i;"); |
| 74 } | 95 } |
| 75 buffer..write(_mainPrefix) | 96 buffer..write(_mainPrefix) |
| 76 ..writeAll(urls.map((url) => " '$url',\n")) | 97 ..writeAll(urls.map((url) => " '$url',\n")) |
| 77 ..write(_mainSuffix); | 98 ..write(_mainSuffix); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 90 if (id.path.startsWith('lib/')) { | 111 if (id.path.startsWith('lib/')) { |
| 91 return 'package:${id.package}/${id.path.substring(4)}'; | 112 return 'package:${id.package}/${id.path.substring(4)}'; |
| 92 } | 113 } |
| 93 | 114 |
| 94 // Use relative urls only if it's possible. | 115 // Use relative urls only if it's possible. |
| 95 if (id.package != sourceId.package) { | 116 if (id.package != sourceId.package) { |
| 96 logger.error("don't know how to import $id from $sourceId"); | 117 logger.error("don't know how to import $id from $sourceId"); |
| 97 return null; | 118 return null; |
| 98 } | 119 } |
| 99 | 120 |
| 100 var urlBuilder = path.url; | 121 var builder = path.url; |
| 101 var upPath = urlBuilder.joinAll( | 122 return builder.relative(builder.join('/', id.path), |
| 102 urlBuilder.split(sourceId.path).map((_) => '..')); | 123 from: builder.join('/', builder.dirname(sourceId.path))); |
| 103 return urlBuilder.normalize( | |
| 104 urlBuilder.join(sourceId.path, upPath, id.path)); | |
| 105 } | 124 } |
| 106 } | 125 } |
| 107 | 126 |
| 108 const _header = """ | 127 const _header = """ |
| 109 library app_bootstrap; | 128 library app_bootstrap; |
| 110 | 129 |
| 111 import 'package:polymer/polymer.dart'; | 130 import 'package:polymer/polymer.dart'; |
| 112 import 'dart:mirrors' show currentMirrorSystem; | 131 import 'dart:mirrors' show currentMirrorSystem; |
| 113 | 132 |
| 114 """; | 133 """; |
| 115 | 134 |
| 116 const _mainPrefix = """ | 135 const _mainPrefix = """ |
| 117 | 136 |
| 118 void main() { | 137 void main() { |
| 119 initPolymer([ | 138 initPolymer([ |
| 120 """; | 139 """; |
| 121 | 140 |
| 122 // TODO(sigmund): investigate alternative to get the baseUri (dartbug.com/12612) | 141 // TODO(sigmund): investigate alternative to get the baseUri (dartbug.com/12612) |
| 123 const _mainSuffix = """ | 142 const _mainSuffix = """ |
| 124 ], currentMirrorSystem().isolate.rootLibrary.uri.toString()); | 143 ], currentMirrorSystem().isolate.rootLibrary.uri.toString()); |
| 125 } | 144 } |
| 126 """; | 145 """; |
| OLD | NEW |