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 (logger is WrappedLogger) return logger.writeOutput(); | |
162 }); | |
156 | 163 |
157 /// Loads the primary input as an html document. | 164 /// Loads the primary input as an html document. |
158 Future _loadDocument() => | 165 Future _loadDocument() => |
159 readPrimaryAsHtml(transform).then((doc) { document = doc; }); | 166 readPrimaryAsHtml(transform).then((doc) { document = doc; }); |
160 | 167 |
161 /// Populates [entryLibraries] as a list containing the asset ids of each | 168 /// 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 | 169 /// 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. | 170 /// in an earlier phase and emited in the `entrypoint._data` asset. |
164 Future _loadEntryLibraries(_) => | 171 Future _loadEntryLibraries(_) => |
165 transform.readInputAsString(docId.addExtension('._data')).then((data) { | 172 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 | 427 /// Writes the final output for the bootstrap Dart file and entrypoint HTML |
421 /// file. | 428 /// file. |
422 void _emitFiles(_) { | 429 void _emitFiles(_) { |
423 StringBuffer code = new StringBuffer()..writeln(MAIN_HEADER); | 430 StringBuffer code = new StringBuffer()..writeln(MAIN_HEADER); |
424 Map<AssetId, String> prefixes = {}; | 431 Map<AssetId, String> prefixes = {}; |
425 int i = 0; | 432 int i = 0; |
426 for (var id in entryLibraries) { | 433 for (var id in entryLibraries) { |
427 var url = assetUrlFor(id, bootstrapId, logger); | 434 var url = assetUrlFor(id, bootstrapId, logger); |
428 if (url == null) continue; | 435 if (url == null) continue; |
429 code.writeln("import '$url' as i$i;"); | 436 code.writeln("import '$url' as i$i;"); |
437 code.writeln( | |
Siggi Cherem (dart-lang)
2014/08/01 21:31:52
guard this also with !options.releaseMode?
jakemac
2014/08/04 19:49:59
Done.
| |
438 "import 'package:polymer/src/build/log_injector.dart'" | |
439 " as log_injector;"); | |
430 prefixes[id] = 'i$i'; | 440 prefixes[id] = 'i$i'; |
431 i++; | 441 i++; |
432 } | 442 } |
433 | 443 |
434 // Include smoke initialization. | 444 // Include smoke initialization. |
435 generator.writeImports(code); | 445 generator.writeImports(code); |
436 generator.writeTopLevelDeclarations(code); | 446 generator.writeTopLevelDeclarations(code); |
437 code.writeln('\nvoid main() {'); | 447 code.writeln('\nvoid main() {'); |
438 code.write(' useGeneratedCode('); | 448 code.write(' useGeneratedCode('); |
439 generator.writeStaticConfiguration(code); | 449 generator.writeStaticConfiguration(code); |
440 code.writeln(');'); | 450 code.writeln(');'); |
451 | |
452 if (!options.releaseMode) { | |
453 code.write(' log_injector.injectLogs();'); | |
454 } | |
455 | |
441 if (experimentalBootstrap) { | 456 if (experimentalBootstrap) { |
442 code.write(' startPolymer(['); | 457 code.write(' startPolymer(['); |
443 } else { | 458 } else { |
444 code.write(' configureForDeployment(['); | 459 code.write(' configureForDeployment(['); |
445 } | 460 } |
446 | 461 |
447 // Include initializers to switch from mirrors_loader to static_loader. | 462 // Include initializers to switch from mirrors_loader to static_loader. |
448 if (!initializers.isEmpty) { | 463 if (!initializers.isEmpty) { |
449 code.writeln(); | 464 code.writeln(); |
450 for (var init in initializers) { | 465 for (var init in initializers) { |
451 var initCode = init.asCode(prefixes[init.assetId]); | 466 var initCode = init.asCode(prefixes[init.assetId]); |
452 code.write(" $initCode,\n"); | 467 code.write(" $initCode,\n"); |
453 } | 468 } |
454 code.writeln(' ]);'); | 469 code.writeln(' ]);'); |
455 } else { | 470 } else { |
456 if (experimentalBootstrap) logger.warning(NO_INITIALIZERS_ERROR); | 471 if (experimentalBootstrap) logger.warning(NO_INITIALIZERS_ERROR); |
457 code.writeln(']);'); | 472 code.writeln(']);'); |
458 } | 473 } |
459 if (!experimentalBootstrap) { | 474 if (!experimentalBootstrap) { |
460 code.writeln(' i${entryLibraries.length - 1}.main();'); | 475 code.writeln(' i${entryLibraries.length - 1}.main();'); |
461 } | 476 } |
477 | |
478 // End of main(). | |
462 code.writeln('}'); | 479 code.writeln('}'); |
463 transform.addOutput(new Asset.fromString(bootstrapId, code.toString())); | 480 transform.addOutput(new Asset.fromString(bootstrapId, code.toString())); |
464 | 481 |
465 | 482 |
466 // Emit the bootstrap .dart file | 483 // Emit the bootstrap .dart file |
467 var srcUrl = path.url.basename(bootstrapId.path); | 484 var srcUrl = path.url.basename(bootstrapId.path); |
468 document.body.nodes.add(parseFragment( | 485 document.body.nodes.add(parseFragment( |
469 '<script type="application/dart" src="$srcUrl"></script>')); | 486 '<script type="application/dart" src="$srcUrl"></script>')); |
Siggi Cherem (dart-lang)
2014/08/01 21:31:52
if we get rid of the 'add' phase, here we could ad
| |
470 transform.addOutput(new Asset.fromString(docId, document.outerHtml)); | 487 transform.addOutput(new Asset.fromString(docId, document.outerHtml)); |
471 } | 488 } |
472 | 489 |
473 _spanForNode(analyzer.Element context, AstNode node) { | 490 _spanForNode(analyzer.Element context, AstNode node) { |
474 var file = resolver.getSourceFile(context); | 491 var file = resolver.getSourceFile(context); |
475 return file.span(node.offset, node.end); | 492 return file.span(node.offset, node.end); |
476 } | 493 } |
477 } | 494 } |
478 | 495 |
479 abstract class _Initializer { | 496 abstract class _Initializer { |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
825 for (var c in combinators) { | 842 for (var c in combinators) { |
826 if (c is ShowElementCombinator) { | 843 if (c is ShowElementCombinator) { |
827 var show = c.shownNames.toSet(); | 844 var show = c.shownNames.toSet(); |
828 elements.retainWhere((e) => show.contains(e.displayName)); | 845 elements.retainWhere((e) => show.contains(e.displayName)); |
829 } else if (c is HideElementCombinator) { | 846 } else if (c is HideElementCombinator) { |
830 var hide = c.hiddenNames.toSet(); | 847 var hide = c.hiddenNames.toSet(); |
831 elements.removeWhere((e) => hide.contains(e.displayName)); | 848 elements.removeWhere((e) => hide.contains(e.displayName)); |
832 } | 849 } |
833 } | 850 } |
834 } | 851 } |
OLD | NEW |