OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 /// Transformer that removes uses of mirrors from the polymer runtime, so that | 5 /// Transformer that removes uses of mirrors from the polymer runtime, so that |
6 /// deployed applications are thin and small. | 6 /// deployed applications are thin and small. |
7 library polymer.src.build.mirrors_remover; | 7 library polymer.src.build.mirrors_remover; |
8 | 8 |
9 import 'dart:async'; | 9 import 'dart:async'; |
10 import 'package:barback/barback.dart'; | 10 import 'package:barback/barback.dart'; |
11 | 11 |
12 /// Removes the code-initialization logic based on mirrors. | 12 /// Removes the code-initialization logic based on mirrors. |
13 class MirrorsRemover extends Transformer { | 13 class MirrorsRemover extends Transformer { |
14 MirrorsRemover.asPlugin(); | 14 MirrorsRemover.asPlugin(); |
15 | 15 |
16 /// Only apply to `lib/polymer.dart`. | 16 /// Only apply to `lib/polymer.dart`. |
17 Future<bool> isPrimary(Asset input) => new Future.value( | 17 Future<bool> isPrimary(AssetId id) => new Future.value( |
18 input.id.package == 'polymer' && | 18 id.package == 'polymer' && id.path == 'lib/polymer.dart'); |
19 input.id.path == 'lib/polymer.dart'); | |
20 | 19 |
21 Future apply(Transform transform) { | 20 Future apply(Transform transform) { |
22 var id = transform.primaryInput.id; | 21 var id = transform.primaryInput.id; |
23 return transform.primaryInput.readAsString().then((code) { | 22 return transform.primaryInput.readAsString().then((code) { |
24 // Note: this rewrite is highly-coupled with how polymer.dart is | 23 // Note: this rewrite is highly-coupled with how polymer.dart is |
25 // written. Make sure both are updated in sync. | 24 // written. Make sure both are updated in sync. |
26 var start = code.indexOf('@MirrorsUsed('); | 25 var start = code.indexOf('@MirrorsUsed('); |
27 if (start == -1) _error(); | 26 if (start == -1) _error(); |
28 var end = code.indexOf('show MirrorsUsed;', start); | 27 var end = code.indexOf('show MirrorsUsed;', start); |
29 if (end == -1) _error(); | 28 if (end == -1) _error(); |
30 end = code.indexOf('\n', end); | 29 end = code.indexOf('\n', end); |
31 var sb = new StringBuffer() | 30 var sb = new StringBuffer() |
32 ..write(code.substring(0, start)) | 31 ..write(code.substring(0, start)) |
33 ..write(code.substring(end)); | 32 ..write(code.substring(end)); |
34 | 33 |
35 transform.addOutput(new Asset.fromString(id, sb.toString())); | 34 transform.addOutput(new Asset.fromString(id, sb.toString())); |
36 }); | 35 }); |
37 } | 36 } |
38 } | 37 } |
39 | 38 |
40 /** Transformer phases which should be applied to the smoke package. */ | 39 /** Transformer phases which should be applied to the smoke package. */ |
41 List<List<Transformer>> get phasesForSmoke => | 40 List<List<Transformer>> get phasesForSmoke => |
42 [[new MirrorsRemover.asPlugin()]]; | 41 [[new MirrorsRemover.asPlugin()]]; |
43 | 42 |
44 _error() => throw new StateError("Couldn't remove imports to mirrors, maybe " | 43 _error() => throw new StateError("Couldn't remove imports to mirrors, maybe " |
45 "polymer.dart was modified, but mirrors_remover.dart wasn't."); | 44 "polymer.dart was modified, but mirrors_remover.dart wasn't."); |
OLD | NEW |