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:convert'; | 8 import 'dart:convert'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 | 10 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 | 50 |
51 throw new FormatException("Unrecognized dart2js " | 51 throw new FormatException("Unrecognized dart2js " |
52 "${pluralize('option', invalidOptions.length)} " | 52 "${pluralize('option', invalidOptions.length)} " |
53 "${toSentence(invalidOptions.map((option) => '"$option"'))}."); | 53 "${toSentence(invalidOptions.map((option) => '"$option"'))}."); |
54 } | 54 } |
55 | 55 |
56 Dart2JSTransformer(BuildEnvironment environment, BarbackMode mode) | 56 Dart2JSTransformer(BuildEnvironment environment, BarbackMode mode) |
57 : this.withSettings(environment, new BarbackSettings({}, mode)); | 57 : this.withSettings(environment, new BarbackSettings({}, mode)); |
58 | 58 |
59 /// Only ".dart" entrypoint files within a buildable directory are processed. | 59 /// Only ".dart" entrypoint files within a buildable directory are processed. |
60 Future<bool> isPrimary(Asset asset) { | 60 Future<bool> isPrimary(AssetId id) { |
61 if (asset.id.extension != ".dart") return new Future.value(false); | 61 if (id.extension != ".dart") return new Future.value(false); |
62 | 62 |
63 // These should only contain libraries. For efficiency's sake, we don't | 63 // These should only contain libraries. For efficiency's sake, we don't |
64 // look for entrypoints in there. | 64 // look for entrypoints in there. |
65 if (["asset/", "lib/"].any(asset.id.path.startsWith)) { | 65 return new Future.value(!["asset/", "lib/"].any(id.path.startsWith)); |
66 return new Future.value(false); | 66 } |
67 } | |
68 | 67 |
| 68 Future apply(Transform transform) { |
| 69 // TODO(nweiz): If/when barback starts reporting what assets were modified, |
| 70 // don't re-run the entrypoint detection logic unless the primary input was |
| 71 // actually modified. See issue 16817. |
| 72 return _isEntrypoint(transform.primaryInput).then((isEntrypoint) { |
| 73 if (!isEntrypoint) return null; |
| 74 |
| 75 // Wait for any ongoing apply to finish first. |
| 76 return _pool.withResource(() { |
| 77 transform.logger.info("Compiling ${transform.primaryInput.id}..."); |
| 78 var stopwatch = new Stopwatch()..start(); |
| 79 return _doCompilation(transform).then((_) { |
| 80 stopwatch.stop(); |
| 81 transform.logger.info("Took ${stopwatch.elapsed} to compile " |
| 82 "${transform.primaryInput.id}."); |
| 83 }); |
| 84 }); |
| 85 }); |
| 86 } |
| 87 |
| 88 Future declareOutputs(DeclaringTransform transform) { |
| 89 var primaryId = transform.primaryId; |
| 90 transform.declareOutput(primaryId.addExtension(".js")); |
| 91 transform.declareOutput(primaryId.addExtension(".js.map")); |
| 92 transform.declareOutput(primaryId.addExtension(".precompiled.js")); |
| 93 return new Future.value(); |
| 94 } |
| 95 |
| 96 /// Returns whether or not [asset] might be an entrypoint. |
| 97 Future<bool> _isEntrypoint(Asset asset) { |
69 return asset.readAsString().then((code) { | 98 return asset.readAsString().then((code) { |
70 try { | 99 try { |
71 var name = asset.id.path; | 100 var name = asset.id.path; |
72 if (asset.id.package != _environment.rootPackage.name) { | 101 if (asset.id.package != _environment.rootPackage.name) { |
73 name += " in ${asset.id.package}"; | 102 name += " in ${asset.id.package}"; |
74 } | 103 } |
75 | 104 |
76 var parsed = parseCompilationUnit(code, name: name); | 105 var parsed = parseCompilationUnit(code, name: name); |
77 return dart.isEntrypoint(parsed); | 106 return dart.isEntrypoint(parsed); |
78 } on AnalyzerErrorGroup catch (e) { | 107 } on AnalyzerErrorGroup catch (e) { |
79 // If we get a parse error, consider the asset primary so we report | 108 // If we get a parse error, consider the asset primary so we report |
80 // dart2js's more detailed error message instead. | 109 // dart2js's more detailed error message instead. |
81 return true; | 110 return true; |
82 } | 111 } |
83 }); | 112 }); |
84 } | 113 } |
85 | 114 |
86 Future apply(Transform transform) { | 115 /// Run the dart2js compiler. |
87 var stopwatch = new Stopwatch(); | 116 Future _doCompilation(Transform transform) { |
| 117 var provider = new _BarbackCompilerProvider(_environment, transform, |
| 118 generateSourceMaps: _settings.mode != BarbackMode.RELEASE); |
88 | 119 |
89 // Wait for any ongoing apply to finish first. | 120 // Create a "path" to the entrypoint script. The entrypoint may not actually |
90 return _pool.withResource(() { | 121 // be on disk, but this gives dart2js a root to resolve relative paths |
91 transform.logger.info("Compiling ${transform.primaryInput.id}..."); | 122 // against. |
92 stopwatch.start(); | 123 var id = transform.primaryInput.id; |
93 | 124 |
94 var provider = new _BarbackCompilerProvider(_environment, transform, | 125 var entrypoint = path.join(_environment.graph.packages[id.package].dir, |
95 generateSourceMaps: _settings.mode != BarbackMode.RELEASE); | 126 id.path); |
96 | 127 |
97 // Create a "path" to the entrypoint script. The entrypoint may not | 128 // TODO(rnystrom): Should have more sophisticated error-handling here. Need |
98 // actually be on disk, but this gives dart2js a root to resolve relative | 129 // to report compile errors to the user in an easily visible way. Need to |
99 // paths against. | 130 // make sure paths in errors are mapped to the original source path so they |
100 var id = transform.primaryInput.id; | 131 // can understand them. |
101 | 132 return Chain.track(dart.compile( |
102 var entrypoint = path.join(_environment.graph.packages[id.package].dir, | 133 entrypoint, provider, |
103 id.path); | 134 commandLineOptions: _configCommandLineOptions, |
104 | 135 checked: _configBool('checked'), |
105 // TODO(rnystrom): Should have more sophisticated error-handling here. | 136 minify: _configBool( |
106 // Need to report compile errors to the user in an easily visible way. | 137 'minify', defaultsTo: _settings.mode == BarbackMode.RELEASE), |
107 // Need to make sure paths in errors are mapped to the original source | 138 verbose: _configBool('verbose'), |
108 // path so they can understand them. | 139 environment: _configEnvironment, |
109 return Chain.track(dart.compile( | 140 packageRoot: path.join(_environment.rootPackage.dir, "packages"), |
110 entrypoint, provider, | 141 analyzeAll: _configBool('analyzeAll'), |
111 commandLineOptions: _configCommandLineOptions, | 142 suppressWarnings: _configBool('suppressWarnings'), |
112 checked: _configBool('checked'), | 143 suppressHints: _configBool('suppressHints'), |
113 minify: _configBool( | 144 suppressPackageWarnings: _configBool( |
114 'minify', defaultsTo: _settings.mode == BarbackMode.RELEASE), | 145 'suppressPackageWarnings', defaultsTo: true), |
115 verbose: _configBool('verbose'), | 146 terse: _configBool('terse'), |
116 environment: _configEnvironment, | 147 includeSourceMapUrls: _settings.mode != BarbackMode.RELEASE)); |
117 packageRoot: path.join(_environment.rootPackage.dir, | |
118 "packages"), | |
119 analyzeAll: _configBool('analyzeAll'), | |
120 suppressWarnings: _configBool('suppressWarnings'), | |
121 suppressHints: _configBool('suppressHints'), | |
122 suppressPackageWarnings: _configBool( | |
123 'suppressPackageWarnings', defaultsTo: true), | |
124 terse: _configBool('terse'), | |
125 includeSourceMapUrls: _settings.mode != BarbackMode.RELEASE)) | |
126 .then((_) { | |
127 stopwatch.stop(); | |
128 transform.logger.info("Took ${stopwatch.elapsed} to compile $id."); | |
129 }); | |
130 }); | |
131 } | |
132 | |
133 Future declareOutputs(DeclaringTransform transform) { | |
134 var primaryId = transform.primaryInput.id; | |
135 transform.declareOutput(primaryId.addExtension(".js")); | |
136 transform.declareOutput(primaryId.addExtension(".js.map")); | |
137 transform.declareOutput(primaryId.addExtension(".precompiled.js")); | |
138 return new Future.value(); | |
139 } | 148 } |
140 | 149 |
141 /// Parses and returns the "commandLineOptions" configuration option. | 150 /// Parses and returns the "commandLineOptions" configuration option. |
142 List<String> get _configCommandLineOptions { | 151 List<String> get _configCommandLineOptions { |
143 if (!_settings.configuration.containsKey('commandLineOptions')) return null; | 152 if (!_settings.configuration.containsKey('commandLineOptions')) return null; |
144 | 153 |
145 var options = _settings.configuration['commandLineOptions']; | 154 var options = _settings.configuration['commandLineOptions']; |
146 if (options is List && options.every((option) => option is String)) { | 155 if (options is List && options.every((option) => option is String)) { |
147 return options; | 156 return options; |
148 } | 157 } |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 } | 396 } |
388 } | 397 } |
389 | 398 |
390 /// An [EventSink] that discards all data. Provided to dart2js when we don't | 399 /// An [EventSink] that discards all data. Provided to dart2js when we don't |
391 /// want an actual output. | 400 /// want an actual output. |
392 class NullSink<T> implements EventSink<T> { | 401 class NullSink<T> implements EventSink<T> { |
393 void add(T event) {} | 402 void add(T event) {} |
394 void addError(errorEvent, [StackTrace stackTrace]) {} | 403 void addError(errorEvent, [StackTrace stackTrace]) {} |
395 void close() {} | 404 void close() {} |
396 } | 405 } |
OLD | NEW |