| 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 import 'dart:convert'; | 9 import 'dart:convert'; |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 import 'package:code_transformers/resolver.dart'; | 22 import 'package:code_transformers/resolver.dart'; |
| 23 import 'package:code_transformers/src/dart_sdk.dart'; | 23 import 'package:code_transformers/src/dart_sdk.dart'; |
| 24 import 'package:template_binding/src/mustache_tokens.dart' show MustacheTokens; | 24 import 'package:template_binding/src/mustache_tokens.dart' show MustacheTokens; |
| 25 | 25 |
| 26 import 'package:polymer_expressions/expression.dart' as pe; | 26 import 'package:polymer_expressions/expression.dart' as pe; |
| 27 import 'package:polymer_expressions/parser.dart' as pe; | 27 import 'package:polymer_expressions/parser.dart' as pe; |
| 28 import 'package:polymer_expressions/visitor.dart' as pe; | 28 import 'package:polymer_expressions/visitor.dart' as pe; |
| 29 | 29 |
| 30 import 'import_inliner.dart' show ImportInliner; // just for docs. | 30 import 'import_inliner.dart' show ImportInliner; // just for docs. |
| 31 import 'common.dart'; | 31 import 'common.dart'; |
| 32 import 'wrapped_logger.dart'; |
| 32 | 33 |
| 33 /// Combines Dart script tags into a single script tag, and creates a new Dart | 34 /// Combines Dart script tags into a single script tag, and creates a new Dart |
| 34 /// file that calls the main function of each of the original script tags. | 35 /// file that calls the main function of each of the original script tags. |
| 35 /// | 36 /// |
| 36 /// This transformer assumes that all script tags point to external files. To | 37 /// This transformer assumes that all script tags point to external files. To |
| 37 /// support script tags with inlined code, use this transformer after running | 38 /// support script tags with inlined code, use this transformer after running |
| 38 /// [ImportInliner] on an earlier phase. | 39 /// [ImportInliner] on an earlier phase. |
| 39 /// | 40 /// |
| 40 /// Internally, this transformer will convert each script tag into an import | 41 /// Internally, this transformer will convert each script tag into an import |
| 41 /// statement to a library, and then uses `initPolymer` (see polymer.dart) to | 42 /// statement to a library, and then uses `initPolymer` (see polymer.dart) to |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 _ResolvedTypes types; | 136 _ResolvedTypes types; |
| 136 | 137 |
| 137 /// The resolver instance associated with a single run of this transformer. | 138 /// The resolver instance associated with a single run of this transformer. |
| 138 Resolver resolver; | 139 Resolver resolver; |
| 139 | 140 |
| 140 /// Code generator used to create the static initialization for smoke. | 141 /// Code generator used to create the static initialization for smoke. |
| 141 final generator = new SmokeCodeGenerator(); | 142 final generator = new SmokeCodeGenerator(); |
| 142 | 143 |
| 143 _SubExpressionVisitor expressionVisitor; | 144 _SubExpressionVisitor expressionVisitor; |
| 144 | 145 |
| 145 _ScriptCompactor(Transform transform, this.options, this.resolvers) | 146 _ScriptCompactor(Transform transform, options, this.resolvers) |
| 146 : transform = transform, | 147 : transform = transform, |
| 147 logger = transform.logger, | 148 options = options, |
| 149 logger = options.releaseMode ? transform.logger : |
| 150 new WrappedLogger(transform, convertErrorsToWarnings: true), |
| 148 docId = transform.primaryInput.id, | 151 docId = transform.primaryInput.id, |
| 149 bootstrapId = transform.primaryInput.id.addExtension('_bootstrap.dart'); | 152 bootstrapId = transform.primaryInput.id.addExtension('_bootstrap.dart'); |
| 150 | 153 |
| 151 Future apply() => | 154 Future apply() => |
| 152 _loadDocument() | 155 _loadDocument() |
| 153 .then(_loadEntryLibraries) | 156 .then(_loadEntryLibraries) |
| 154 .then(_processHtml) | 157 .then(_processHtml) |
| 155 .then(_emitNewEntrypoint); | 158 .then(_emitNewEntrypoint) |
| 159 .then((_) { |
| 160 // Write out the logs collected by our [WrappedLogger]. |
| 161 if (options.injectBuildLogsInOutput && logger is WrappedLogger) { |
| 162 return logger.writeOutput(); |
| 163 } |
| 164 }); |
| 156 | 165 |
| 157 /// Loads the primary input as an html document. | 166 /// Loads the primary input as an html document. |
| 158 Future _loadDocument() => | 167 Future _loadDocument() => |
| 159 readPrimaryAsHtml(transform).then((doc) { document = doc; }); | 168 readPrimaryAsHtml(transform).then((doc) { document = doc; }); |
| 160 | 169 |
| 161 /// Populates [entryLibraries] as a list containing the asset ids of each | 170 /// Populates [entryLibraries] as a list containing the asset ids of each |
| 162 /// library loaded on a script tag. The actual work of computing this is done | 171 /// library loaded on a script tag. The actual work of computing this is done |
| 163 /// in an earlier phase and emited in the `entrypoint._data` asset. | 172 /// in an earlier phase and emited in the `entrypoint._data` asset. |
| 164 Future _loadEntryLibraries(_) => | 173 Future _loadEntryLibraries(_) => |
| 165 transform.readInputAsString(docId.addExtension('._data')).then((data) { | 174 transform.readInputAsString(docId.addExtension('._data')).then((data) { |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 /// Writes the final output for the bootstrap Dart file and entrypoint HTML | 429 /// Writes the final output for the bootstrap Dart file and entrypoint HTML |
| 421 /// file. | 430 /// file. |
| 422 void _emitFiles(_) { | 431 void _emitFiles(_) { |
| 423 StringBuffer code = new StringBuffer()..writeln(MAIN_HEADER); | 432 StringBuffer code = new StringBuffer()..writeln(MAIN_HEADER); |
| 424 Map<AssetId, String> prefixes = {}; | 433 Map<AssetId, String> prefixes = {}; |
| 425 int i = 0; | 434 int i = 0; |
| 426 for (var id in entryLibraries) { | 435 for (var id in entryLibraries) { |
| 427 var url = assetUrlFor(id, bootstrapId, logger); | 436 var url = assetUrlFor(id, bootstrapId, logger); |
| 428 if (url == null) continue; | 437 if (url == null) continue; |
| 429 code.writeln("import '$url' as i$i;"); | 438 code.writeln("import '$url' as i$i;"); |
| 439 if (options.injectBuildLogsInOutput) { |
| 440 code.writeln("import 'package:polymer/src/build/log_injector.dart';"); |
| 441 } |
| 430 prefixes[id] = 'i$i'; | 442 prefixes[id] = 'i$i'; |
| 431 i++; | 443 i++; |
| 432 } | 444 } |
| 433 | 445 |
| 434 // Include smoke initialization. | 446 // Include smoke initialization. |
| 435 generator.writeImports(code); | 447 generator.writeImports(code); |
| 436 generator.writeTopLevelDeclarations(code); | 448 generator.writeTopLevelDeclarations(code); |
| 437 code.writeln('\nvoid main() {'); | 449 code.writeln('\nvoid main() {'); |
| 438 code.write(' useGeneratedCode('); | 450 code.write(' useGeneratedCode('); |
| 439 generator.writeStaticConfiguration(code); | 451 generator.writeStaticConfiguration(code); |
| 440 code.writeln(');'); | 452 code.writeln(');'); |
| 453 |
| 454 if (options.injectBuildLogsInOutput) { |
| 455 code.writeln(' new LogInjector().injectLogsFromUrl();'); |
| 456 } |
| 457 |
| 441 if (experimentalBootstrap) { | 458 if (experimentalBootstrap) { |
| 442 code.write(' startPolymer(['); | 459 code.write(' startPolymer(['); |
| 443 } else { | 460 } else { |
| 444 code.write(' configureForDeployment(['); | 461 code.write(' configureForDeployment(['); |
| 445 } | 462 } |
| 446 | 463 |
| 447 // Include initializers to switch from mirrors_loader to static_loader. | 464 // Include initializers to switch from mirrors_loader to static_loader. |
| 448 if (!initializers.isEmpty) { | 465 if (!initializers.isEmpty) { |
| 449 code.writeln(); | 466 code.writeln(); |
| 450 for (var init in initializers) { | 467 for (var init in initializers) { |
| 451 var initCode = init.asCode(prefixes[init.assetId]); | 468 var initCode = init.asCode(prefixes[init.assetId]); |
| 452 code.write(" $initCode,\n"); | 469 code.write(" $initCode,\n"); |
| 453 } | 470 } |
| 454 code.writeln(' ]);'); | 471 code.writeln(' ]);'); |
| 455 } else { | 472 } else { |
| 456 if (experimentalBootstrap) logger.warning(NO_INITIALIZERS_ERROR); | 473 if (experimentalBootstrap) logger.warning(NO_INITIALIZERS_ERROR); |
| 457 code.writeln(']);'); | 474 code.writeln(']);'); |
| 458 } | 475 } |
| 459 if (!experimentalBootstrap) { | 476 if (!experimentalBootstrap) { |
| 460 code.writeln(' i${entryLibraries.length - 1}.main();'); | 477 code.writeln(' i${entryLibraries.length - 1}.main();'); |
| 461 } | 478 } |
| 479 |
| 480 // End of main(). |
| 462 code.writeln('}'); | 481 code.writeln('}'); |
| 463 transform.addOutput(new Asset.fromString(bootstrapId, code.toString())); | 482 transform.addOutput(new Asset.fromString(bootstrapId, code.toString())); |
| 464 | 483 |
| 465 | 484 |
| 466 // Emit the bootstrap .dart file | 485 // Emit the bootstrap .dart file |
| 467 var srcUrl = path.url.basename(bootstrapId.path); | 486 var srcUrl = path.url.basename(bootstrapId.path); |
| 468 document.body.nodes.add(parseFragment( | 487 document.body.nodes.add(parseFragment( |
| 469 '<script type="application/dart" src="$srcUrl"></script>')); | 488 '<script type="application/dart" src="$srcUrl"></script>')); |
| 489 |
| 490 // Add the styles for the logger widget. |
| 491 if (options.injectBuildLogsInOutput) { |
| 492 document.head.append(parseFragment( |
| 493 '<link rel="stylesheet" type="text/css"' |
| 494 'href="packages/polymer/src/build/log_injector.css">')); |
| 495 } |
| 496 |
| 470 transform.addOutput(new Asset.fromString(docId, document.outerHtml)); | 497 transform.addOutput(new Asset.fromString(docId, document.outerHtml)); |
| 471 } | 498 } |
| 472 | 499 |
| 473 _spanForNode(analyzer.Element context, AstNode node) { | 500 _spanForNode(analyzer.Element context, AstNode node) { |
| 474 var file = resolver.getSourceFile(context); | 501 var file = resolver.getSourceFile(context); |
| 475 return file.span(node.offset, node.end); | 502 return file.span(node.offset, node.end); |
| 476 } | 503 } |
| 477 } | 504 } |
| 478 | 505 |
| 479 abstract class _Initializer { | 506 abstract class _Initializer { |
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 823 for (var c in combinators) { | 850 for (var c in combinators) { |
| 824 if (c is ShowElementCombinator) { | 851 if (c is ShowElementCombinator) { |
| 825 var show = c.shownNames.toSet(); | 852 var show = c.shownNames.toSet(); |
| 826 elements.retainWhere((e) => show.contains(e.displayName)); | 853 elements.retainWhere((e) => show.contains(e.displayName)); |
| 827 } else if (c is HideElementCombinator) { | 854 } else if (c is HideElementCombinator) { |
| 828 var hide = c.hiddenNames.toSet(); | 855 var hide = c.hiddenNames.toSet(); |
| 829 elements.removeWhere((e) => hide.contains(e.displayName)); | 856 elements.removeWhere((e) => hide.contains(e.displayName)); |
| 830 } | 857 } |
| 831 } | 858 } |
| 832 } | 859 } |
| OLD | NEW |