Chromium Code Reviews| 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 barback.transformer; | 5 library barback.transformer; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'asset.dart'; | 9 import 'asset.dart'; |
| 10 import 'transform.dart'; | 10 import 'transform.dart'; |
| 11 | 11 |
| 12 /// A [Transformer] represents a processor that takes in one or more input | 12 /// A [Transformer] represents a processor that takes in one or more input |
| 13 /// assets and uses them to generate one or more output assets. | 13 /// assets and uses them to generate one or more output assets. |
| 14 /// | 14 /// |
| 15 /// Dart2js, a SASS->CSS processor, a CSS spriter, and a tool to concatenate | 15 /// Dart2js, a SASS->CSS processor, a CSS spriter, and a tool to concatenate |
| 16 /// files are all examples of transformers. To define your own transformation | 16 /// files are all examples of transformers. To define your own transformation |
| 17 /// step, extend (or implement) this class. | 17 /// step, extend (or implement) this class. |
| 18 abstract class Transformer { | 18 abstract class Transformer { |
| 19 /// Override this to return a space-separated list of file extensions | |
| 20 /// (without leading `.`) that are allowed for the primary inputs to this | |
|
nweiz
2013/08/20 00:35:43
It's confusing that this doesn't use the leading "
Bob Nystrom
2013/08/20 16:15:37
Changed to require the leading ".".
| |
| 21 /// transformer. | |
| 22 /// | |
| 23 /// If you don't override [isPrimary] yourself, it defaults to allowing any | |
| 24 /// asset whose extension matches one of the ones returned by this. If you | |
| 25 /// don't override [isPrimary] *or* this, it allows all files. | |
| 26 String get allowedExtensions => null; | |
|
nweiz
2013/08/20 00:35:43
It's weird that this uses a space-separated list w
Bob Nystrom
2013/08/20 16:15:37
I thought about that, but then I wouldn't be able
nweiz
2013/08/20 19:20:54
If the way you specify a list of extensions is as
| |
| 27 | |
| 19 /// Returns `true` if [input] can be a primary input for this transformer. | 28 /// Returns `true` if [input] can be a primary input for this transformer. |
| 20 /// | 29 /// |
| 21 /// While a transformer can read from multiple input files, one must be the | 30 /// While a transformer can read from multiple input files, one must be the |
| 22 /// "primary" input. This asset determines whether the transformation should | 31 /// "primary" input. This asset determines whether the transformation should |
| 23 /// be run at all. If the primary input is removed, the transformer will no | 32 /// be run at all. If the primary input is removed, the transformer will no |
| 24 /// longer be run. | 33 /// longer be run. |
| 25 /// | 34 /// |
| 26 /// A concrete example is dart2js. When you run dart2js, it will traverse | 35 /// A concrete example is dart2js. When you run dart2js, it will traverse |
| 27 /// all of the imports in your Dart source files and use the contents of all | 36 /// all of the imports in your Dart source files and use the contents of all |
| 28 /// of those to generate the final JS. However you still run dart2js "on" a | 37 /// of those to generate the final JS. However you still run dart2js "on" a |
| 29 /// single file: the entrypoint Dart file that has your `main()` method. | 38 /// single file: the entrypoint Dart file that has your `main()` method. |
| 30 /// This entrypoint file would be the primary input. | 39 /// This entrypoint file would be the primary input. |
| 31 Future<bool> isPrimary(Asset input); | 40 /// |
| 41 /// If this is not overridden, defaults to allow any asset whose extension | |
| 42 /// matches one of the ones returned by [allowedExtensions]. If *that* is | |
| 43 /// not overridden, allows all assets. | |
| 44 Future<bool> isPrimary(Asset input) { | |
| 45 // Allow all files if [primaryExtensions] is not overridden. | |
| 46 if (allowedExtensions == null) return new Future.value(true); | |
| 47 | |
| 48 for (var extension in allowedExtensions.split(" ")) { | |
| 49 if (input.id.extension == ".$extension") return new Future.value(true); | |
| 50 } | |
| 51 | |
| 52 return new Future.value(false); | |
| 53 } | |
| 32 | 54 |
| 33 /// Run this transformer on on the primary input specified by [transform]. | 55 /// Run this transformer on on the primary input specified by [transform]. |
| 34 /// | 56 /// |
| 35 /// The [transform] is used by the [Transformer] for two purposes (in | 57 /// The [transform] is used by the [Transformer] for two purposes (in |
| 36 /// addition to accessing the primary input). It can call `getInput()` to | 58 /// addition to accessing the primary input). It can call `getInput()` to |
| 37 /// request additional input assets. It also calls `addOutput()` to provide | 59 /// request additional input assets. It also calls `addOutput()` to provide |
| 38 /// generated assets back to the system. Either can be called multiple times, | 60 /// generated assets back to the system. Either can be called multiple times, |
| 39 /// in any order. | 61 /// in any order. |
| 40 /// | 62 /// |
| 41 /// In other words, a Transformer's job is to find all inputs for a | 63 /// In other words, a Transformer's job is to find all inputs for a |
| 42 /// transform, starting at the primary input, then generate all output assets | 64 /// transform, starting at the primary input, then generate all output assets |
| 43 /// and yield them back to the transform. | 65 /// and yield them back to the transform. |
| 44 Future apply(Transform transform); | 66 Future apply(Transform transform); |
| 45 | 67 |
| 46 String toString() => runtimeType.toString().replaceAll("Transformer", ""); | 68 String toString() => runtimeType.toString().replaceAll("Transformer", ""); |
| 47 } | 69 } |
| OLD | NEW |