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 = new WrappedLogger(transform, convertErrorsToWarnings: true), |
148 docId = transform.primaryInput.id, | 150 docId = transform.primaryInput.id, |
149 bootstrapId = transform.primaryInput.id.addExtension('_bootstrap.dart'); | 151 bootstrapId = transform.primaryInput.id.addExtension('_bootstrap.dart'); |
150 | 152 |
151 Future apply() => | 153 Future apply() => |
152 _loadDocument() | 154 _loadDocument() |
153 .then(_loadEntryLibraries) | 155 .then(_loadEntryLibraries) |
154 .then(_processHtml) | 156 .then(_processHtml) |
155 .then(_emitNewEntrypoint); | 157 .then(_emitNewEntrypoint) |
| 158 .then((_) { |
| 159 // Write out the logs collected by our [WrappedLogger]. |
| 160 if (options.injectBuildLogsInOutput) return logger.writeOutput(); |
| 161 }); |
156 | 162 |
157 /// Loads the primary input as an html document. | 163 /// Loads the primary input as an html document. |
158 Future _loadDocument() => | 164 Future _loadDocument() => |
159 readPrimaryAsHtml(transform).then((doc) { document = doc; }); | 165 readPrimaryAsHtml(transform).then((doc) { document = doc; }); |
160 | 166 |
161 /// Populates [entryLibraries] as a list containing the asset ids of each | 167 /// 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 | 168 /// 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. | 169 /// in an earlier phase and emited in the `entrypoint._data` asset. |
164 Future _loadEntryLibraries(_) => | 170 Future _loadEntryLibraries(_) => |
165 transform.readInputAsString(docId.addExtension('._data')).then((data) { | 171 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 | 426 /// Writes the final output for the bootstrap Dart file and entrypoint HTML |
421 /// file. | 427 /// file. |
422 void _emitFiles(_) { | 428 void _emitFiles(_) { |
423 StringBuffer code = new StringBuffer()..writeln(MAIN_HEADER); | 429 StringBuffer code = new StringBuffer()..writeln(MAIN_HEADER); |
424 Map<AssetId, String> prefixes = {}; | 430 Map<AssetId, String> prefixes = {}; |
425 int i = 0; | 431 int i = 0; |
426 for (var id in entryLibraries) { | 432 for (var id in entryLibraries) { |
427 var url = assetUrlFor(id, bootstrapId, logger); | 433 var url = assetUrlFor(id, bootstrapId, logger); |
428 if (url == null) continue; | 434 if (url == null) continue; |
429 code.writeln("import '$url' as i$i;"); | 435 code.writeln("import '$url' as i$i;"); |
| 436 if (options.injectBuildLogsInOutput) { |
| 437 code.writeln("import 'package:polymer/src/build/log_injector.dart';"); |
| 438 } |
430 prefixes[id] = 'i$i'; | 439 prefixes[id] = 'i$i'; |
431 i++; | 440 i++; |
432 } | 441 } |
433 | 442 |
434 // Include smoke initialization. | 443 // Include smoke initialization. |
435 generator.writeImports(code); | 444 generator.writeImports(code); |
436 generator.writeTopLevelDeclarations(code); | 445 generator.writeTopLevelDeclarations(code); |
437 code.writeln('\nvoid main() {'); | 446 code.writeln('\nvoid main() {'); |
438 code.write(' useGeneratedCode('); | 447 code.write(' useGeneratedCode('); |
439 generator.writeStaticConfiguration(code); | 448 generator.writeStaticConfiguration(code); |
440 code.writeln(');'); | 449 code.writeln(');'); |
| 450 |
| 451 if (options.injectBuildLogsInOutput) { |
| 452 code.writeln(' new LogInjector().injectLogs();'); |
| 453 } |
| 454 |
441 if (experimentalBootstrap) { | 455 if (experimentalBootstrap) { |
442 code.write(' startPolymer(['); | 456 code.write(' startPolymer(['); |
443 } else { | 457 } else { |
444 code.write(' configureForDeployment(['); | 458 code.write(' configureForDeployment(['); |
445 } | 459 } |
446 | 460 |
447 // Include initializers to switch from mirrors_loader to static_loader. | 461 // Include initializers to switch from mirrors_loader to static_loader. |
448 if (!initializers.isEmpty) { | 462 if (!initializers.isEmpty) { |
449 code.writeln(); | 463 code.writeln(); |
450 for (var init in initializers) { | 464 for (var init in initializers) { |
451 var initCode = init.asCode(prefixes[init.assetId]); | 465 var initCode = init.asCode(prefixes[init.assetId]); |
452 code.write(" $initCode,\n"); | 466 code.write(" $initCode,\n"); |
453 } | 467 } |
454 code.writeln(' ]);'); | 468 code.writeln(' ]);'); |
455 } else { | 469 } else { |
456 if (experimentalBootstrap) logger.warning(NO_INITIALIZERS_ERROR); | 470 if (experimentalBootstrap) logger.warning(NO_INITIALIZERS_ERROR); |
457 code.writeln(']);'); | 471 code.writeln(']);'); |
458 } | 472 } |
459 if (!experimentalBootstrap) { | 473 if (!experimentalBootstrap) { |
460 code.writeln(' i${entryLibraries.length - 1}.main();'); | 474 code.writeln(' i${entryLibraries.length - 1}.main();'); |
461 } | 475 } |
| 476 |
| 477 // End of main(). |
462 code.writeln('}'); | 478 code.writeln('}'); |
463 transform.addOutput(new Asset.fromString(bootstrapId, code.toString())); | 479 transform.addOutput(new Asset.fromString(bootstrapId, code.toString())); |
464 | 480 |
465 | 481 |
466 // Emit the bootstrap .dart file | 482 // Emit the bootstrap .dart file |
467 var srcUrl = path.url.basename(bootstrapId.path); | 483 var srcUrl = path.url.basename(bootstrapId.path); |
468 document.body.nodes.add(parseFragment( | 484 document.body.nodes.add(parseFragment( |
469 '<script type="application/dart" src="$srcUrl"></script>')); | 485 '<script type="application/dart" src="$srcUrl"></script>')); |
| 486 |
| 487 // Add the styles for the logger widget. |
| 488 if (options.injectBuildLogsInOutput) { |
| 489 document.head.append(parseFragment( |
| 490 '<link rel="stylesheet" type="text/css"' |
| 491 'href="packages/polymer/src/build/log_injector.css">')); |
| 492 } |
| 493 |
470 transform.addOutput(new Asset.fromString(docId, document.outerHtml)); | 494 transform.addOutput(new Asset.fromString(docId, document.outerHtml)); |
471 } | 495 } |
472 | 496 |
473 _spanForNode(analyzer.Element context, AstNode node) { | 497 _spanForNode(analyzer.Element context, AstNode node) { |
474 var file = resolver.getSourceFile(context); | 498 var file = resolver.getSourceFile(context); |
475 return file.span(node.offset, node.end); | 499 return file.span(node.offset, node.end); |
476 } | 500 } |
477 } | 501 } |
478 | 502 |
479 abstract class _Initializer { | 503 abstract class _Initializer { |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
823 for (var c in combinators) { | 847 for (var c in combinators) { |
824 if (c is ShowElementCombinator) { | 848 if (c is ShowElementCombinator) { |
825 var show = c.shownNames.toSet(); | 849 var show = c.shownNames.toSet(); |
826 elements.retainWhere((e) => show.contains(e.displayName)); | 850 elements.retainWhere((e) => show.contains(e.displayName)); |
827 } else if (c is HideElementCombinator) { | 851 } else if (c is HideElementCombinator) { |
828 var hide = c.hiddenNames.toSet(); | 852 var hide = c.hiddenNames.toSet(); |
829 elements.removeWhere((e) => hide.contains(e.displayName)); | 853 elements.removeWhere((e) => hide.contains(e.displayName)); |
830 } | 854 } |
831 } | 855 } |
832 } | 856 } |
OLD | NEW |