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 library pub.dart2js_transformer; | 5 library pub.dart2js_transformer; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import 'package:analyzer/analyzer.dart'; | 10 import 'package:analyzer/analyzer.dart'; |
(...skipping 16 matching lines...) Expand all Loading... |
27 final PackageGraph _graph; | 27 final PackageGraph _graph; |
28 | 28 |
29 /// Whether the JavaScript output should be minified. | 29 /// Whether the JavaScript output should be minified. |
30 final bool _minify; | 30 final bool _minify; |
31 | 31 |
32 /// The [AssetId]s the transformer has discovered so far. Used by pub build | 32 /// The [AssetId]s the transformer has discovered so far. Used by pub build |
33 /// to determine where to copy the JS bootstrap files. | 33 /// to determine where to copy the JS bootstrap files. |
34 // TODO(rnystrom): Do something cleaner for this, or eliminate those files. | 34 // TODO(rnystrom): Do something cleaner for this, or eliminate those files. |
35 final entrypoints = new Set<AssetId>(); | 35 final entrypoints = new Set<AssetId>(); |
36 | 36 |
| 37 /// If this is non-null, then the transformer is currently being applied, so |
| 38 /// subsequent calls to [apply] will wait for this to finish before |
| 39 /// proceeding. |
| 40 /// |
| 41 /// Dart2js uses lots of memory, so if we try to actually run compiles in |
| 42 /// parallel, it takes down the VM. Instead, the transformer will force |
| 43 /// all applies to be sequential. The tracking bug to do something better |
| 44 /// is here: https://code.google.com/p/dart/issues/detail?id=14730. |
| 45 Future _running; |
| 46 |
37 Dart2JSTransformer(this._graph, {bool minify}) | 47 Dart2JSTransformer(this._graph, {bool minify}) |
38 : _minify = minify == true ? true : false; | 48 : _minify = minify == true ? true : false; |
39 | 49 |
40 /// Only ".dart" files within "web/" are processed. | 50 /// Only ".dart" files within "web/" are processed. |
41 Future<bool> isPrimary(Asset asset) { | 51 Future<bool> isPrimary(Asset asset) { |
42 return new Future.value( | 52 return new Future.value( |
43 asset.id.extension == ".dart" && | 53 asset.id.extension == ".dart" && |
44 asset.id.path.startsWith("web/")); | 54 asset.id.path.startsWith("web/")); |
45 } | 55 } |
46 | 56 |
47 Future apply(Transform transform) { | 57 Future apply(Transform transform) { |
| 58 // Wait for any ongoing apply to finish first. |
| 59 // TODO(rnystrom): If there are multiple simultaneous compiles, this will |
| 60 // resume and pause them repeatedly. It still serializes them correctly, |
| 61 // but it might be cleaner to use a real queue. |
| 62 // TODO(rnystrom): Add a test that this is functionality is helpful. |
| 63 if (_running != null) { |
| 64 return _running.then((_) => apply(transform)); |
| 65 } |
| 66 |
| 67 var completer = new Completer(); |
| 68 _running = completer.future; |
| 69 |
48 var stopwatch = new Stopwatch(); | 70 var stopwatch = new Stopwatch(); |
49 stopwatch.start(); | 71 stopwatch.start(); |
50 | 72 |
51 return transform.primaryInput.readAsString().then((code) { | 73 return transform.primaryInput.readAsString().then((code) { |
52 try { | 74 try { |
53 var id = transform.primaryInput.id; | 75 var id = transform.primaryInput.id; |
54 var name = id.path; | 76 var name = id.path; |
55 if (id.package != _graph.entrypoint.root.name) { | 77 if (id.package != _graph.entrypoint.root.name) { |
56 name += " in ${id.package}"; | 78 name += " in ${id.package}"; |
57 } | 79 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 | 111 |
90 stopwatch.stop(); | 112 stopwatch.stop(); |
91 transform.logger.info("Generated $id (${js.length} characters) in " | 113 transform.logger.info("Generated $id (${js.length} characters) in " |
92 "${stopwatch.elapsed}"); | 114 "${stopwatch.elapsed}"); |
93 }).catchError((error) { | 115 }).catchError((error) { |
94 // The compile failed and errors have been reported through the | 116 // The compile failed and errors have been reported through the |
95 // diagnostic handler, so just do nothing here. | 117 // diagnostic handler, so just do nothing here. |
96 if (error is CompilerException) return; | 118 if (error is CompilerException) return; |
97 throw error; | 119 throw error; |
98 }); | 120 }); |
| 121 }).whenComplete(() { |
| 122 completer.complete(); |
| 123 _running = null; |
99 }); | 124 }); |
100 } | 125 } |
101 } | 126 } |
102 | 127 |
103 /// Defines methods implementig [CompilerInputProvider] and [DiagnosticHandler] | 128 /// Defines methods implementig [CompilerInputProvider] and [DiagnosticHandler] |
104 /// for dart2js to use to load files from Barback and report errors. | 129 /// for dart2js to use to load files from Barback and report errors. |
105 /// | 130 /// |
106 /// Note that most of the implementation of diagnostic handling here was | 131 /// Note that most of the implementation of diagnostic handling here was |
107 /// copied from [FormattingDiagnosticHandler] in dart2js. The primary | 132 /// copied from [FormattingDiagnosticHandler] in dart2js. The primary |
108 /// difference is that it uses barback's logging code and, more importantly, it | 133 /// difference is that it uses barback's logging code and, more importantly, it |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 var rootDir = _graph.entrypoint.root.dir; | 267 var rootDir = _graph.entrypoint.root.dir; |
243 var sourcePath = path.fromUri(url); | 268 var sourcePath = path.fromUri(url); |
244 if (isBeneath(sourcePath, rootDir)) { | 269 if (isBeneath(sourcePath, rootDir)) { |
245 var relative = path.relative(sourcePath, from: rootDir); | 270 var relative = path.relative(sourcePath, from: rootDir); |
246 return new AssetId(_graph.entrypoint.root.name, relative); | 271 return new AssetId(_graph.entrypoint.root.name, relative); |
247 } | 272 } |
248 | 273 |
249 return null; | 274 return null; |
250 } | 275 } |
251 } | 276 } |
OLD | NEW |