-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Description
Update. This feature is fully-implemented and available in the master branch.
Pending official documentation: flutter/website#10471
Pending sample project, which you can use to play with this feature: flutter/samples#2267
This tracks the implementation of the feature requested in #101077. The rest of this issue assumes the reader has a basic understanding of application assets in Flutter; see https://docs.flutter.dev/ui/assets/assets-and-images for more.
This feature allows users to configure their assets to be transformed by Dart CLI programs.
When declaring an asset in their pubspec, Flutter app developers can specify a transformer along with it. When building the app, the flutter CLI tool will run the transformer against the original asset file, with the output routed to the appropriate directory.
Example use case. Consider a scenario where a user wants to use the vector_graphics package to render SVG images in their app. To make an SVG file renderable by vector_graphics the user would normally have to manually run the vector_graphics_compiler package against it. The user would then need to have this transformed file bundled into their app (not the original). However, they can now instead do this:
# pubspec.yaml
...
flutter:
...
assets:
- path: assets/icon.svg
transformers:
- package: vector_graphics_compilerThen, in their app code, the developer can the vector_graphics package to render the asset.
import 'package:vector_graphics/vector_graphics.dart';
// ...
final Widget icon = VectorGraphic(loader: AssetBytesLoader('assets/icon.svg'))This makes adding SVGs to a Flutter app simpler. While the flutter_svg package could already render SVGs without requiring precompilation, it is significantly slower since it needs to decode and parse XML (amongst other reasons). With this feature, developers can enjoy convenience similar to that of using flutter_svg while also enjoying the superior rendering performance of vector_graphics.
vector_graphics_compiler can be used as an asset transformer because it is a CLI Dart application that accepts an --input argument (the path to the original SVG file) and an --output argument (where the output the file that can be rendered by the vector_graphics package. Any Dart CLI application that accepts these arguments and writes an output file to the provided --output path can be used as an asset transformer.
Tasks:
- Decide on the scheme that users use to declare transformations in the pubspec, and augment tool code to validate and parse this
- Enable asset transformation for workflows that use
copyAssetsto do bundle and write assets. If I'm not mistaken, this includesflutter buildfor Android, iOS/macOS, Windows, Linux, and macOS. - Enable asset transformation for non-web
flutter runscenarios. - Enable asset transformation for
flutter runfor web. - Enable asset transformation for
flutter_tester. - Make hot reload reflect changes to asset transformers or configurations
Cleanup tasks:
- For
flutter build, see if we can make caching work in the presence of transformers that are path dependencies (see Enable asset transformation forflutter buildfor iOS, Android, Windows, MacOS, Linux, and web (alsoflutter runwithout hot reload support) #143815 (comment)) - Consider getting this under integration test
- Consider having the flutter tool, when invoking a transformer, expose relevant information via environment variables. This would include information like what mode we are building in (debug, profile, release).