Skip to content

☂️ Implement pluggable transformation (via packages) of assets at build time #143348

@andrewkolos

Description

@andrewkolos

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_compiler

Then, 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:

Cleanup tasks:

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listteam-toolOwned by Flutter Tool teamtriaged-toolTriaged by Flutter Tool team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions