New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
☂️ Implement pluggable transformation (via packages) of assets at build time #143348
Comments
In service of #143348. **Issue.** The `equals` implementation of `AssetsEntry` is incorrect. It compares `flavors` lists using reference equality. This PR addresses this. This also adds a test to make sure valid asset `flavors` declarations are parsed correctly. While we are here, this PR also includes a couple of refactorings: * `flutter_manifest_test.dart` is a bit large. To better match our style guide, I've factored out some related tests into their own file. * A couple of changes to the `_validateListType` function in `flutter_manifest.dart`: * The function now returns a list of errors instead of accepting a list to append onto. This is more readable and also allows callers to know which errors were found by the call. * The function is renamed to `_validateList` and now accepts an `Object?` instead of an `YamlList`. If the argument is null, an appropriate error message is contained in the output. This saves callers that are only interested in validation from having to write their own null-check, which they all did before. * Some error strings were tweaked for increased readability and/or grammatical correctness.
One way we could integration test this is by adding https://github.com/andrewkolos/asset_transformers_test to the github.com/flutter/tests registry (this solves needing to check in test assets to flutter/flutter). If it turns out to be more trouble than its worth, we can easily delete it. |
In service of #143348 This will make testing of asset transformation hot reload behavior easier (specifically in that we can avoid having to write a test that looks like this: https://github.com/flutter/flutter/blob/5d02c27248d9ebb0db79547f3ed0fce8060ff041/packages/flutter_tools/test/general.shard/build_system/targets/assets_test.dart#L167-L249
Drive by comment. We might be trying to solve similar use cases in the work on "native assets" (#129757), data assets (dart-lang/native#154), and Some of our uses cases:
The transformers in our use cases ( Note that both of these use cases need to work in Dart standalone and Flutter. So unfortunately a Flutter-specific solution for resource transformation doesn't work. We should maybe sync up on what work overlaps so we can leverage each others' work. cc @mosuem Detailed comments:
@andrewkolos Where do the entry points for these transformation scripts live?
@mosuem This sound rather similar to in our proposal having
This is precisely what flutter_tools does with the dylibs output by (Side note: |
…er test` (#144734) Partial implementation of #143348. The title says it all. Feel free to experiment with the feature using this project: https://github.com/andrewkolos/asset_transformers_test.
Yes, they live in
Yes, the file output by the |
For the native assets feature,
Cool! The "data assets" for Dart standalone packages such as |
How are the dependencies of the package resolved then? For example, if I
I will get compiler errors:
|
The (We've had a bunch of conversations whether we should allow hooks of different packages to resolve their dependencies to different versions, but decided that that can have negative consequences. Especially if the code in
flutter/packages/flutter_tools/lib/src/build_system/targets/native_assets.dart Lines 71 to 81 in d98d842
|
) In service of #143348 When invoking a package to transform an asset, we set `FLUTTER_BUILD_MODE` to the CLI name of the build mode being used. Inspired by #101077 (comment): > Do transformers know whether they get executed in debug or release mode? I kinda imagine that being useful. Ex: There's a transformer that optimizes the file size of images. Depending on the amount and size of the images, that could take a significant amount of time. Therefore, I might want to only execute it in release builds. Note for the reviewer: the interesting part of this change can be found in the commit [set environment variable to build mode when running asset transformer�](579912d). The rest of the change is updating call sites with a new argument.
…ses (#144752)" (#144957) Reverts: #144752 Initiated by: andrewkolos Reason for reverting: compilation issue has turned the tree red Original PR Author: andrewkolos Reviewed By: {christopherfujino} This change reverts the following previous change: In service of #143348 When invoking a package to transform an asset, we set `FLUTTER_BUILD_MODE` to the CLI name of the build mode being used. Inspired by #101077 (comment): > Do transformers know whether they get executed in debug or release mode? I kinda imagine that being useful. Ex: There's a transformer that optimizes the file size of images. Depending on the amount and size of the images, that could take a significant amount of time. Therefore, I might want to only execute it in release builds. Note for the reviewer: the interesting part of this change can be found in the commit [set environment variable to build mode when running asset transformer…](579912d). The rest of the change is updating call sites with a new argument.
A few thoughts:
|
…144660) In service of #143348 This PR makes hot reloads reflect changes to transformer configurations under the `assets` section in pubspec.yaml. This PR is optimized for ease of implementation, and should not be merged as-is. If it is merged as-is, seriously consider creating a tech debt issue and assigning it to someone to make sure it gets resolved.
From #143348 (comment): > before we ship, we should add a printTrace to the tool about each asset transformer we're invoking and the path/arguments we called it with I think this is a good idea since asset transformers can be arbitrary Dart programs�meaning that a lot can go wrong when running them. For example, they can hang indefinitely or perform some sort of I/O that later results in a tool crash. Knowing that asset transformation was involved when debugging a crash (or a slow/stuck `flutter build`) could be useful, so I think adding a `printTrace` or two is a good idea (or at least not a bad one).
From flutter#143348 (comment): > before we ship, we should add a printTrace to the tool about each asset transformer we're invoking and the path/arguments we called it with I think this is a good idea since asset transformers can be arbitrary Dart programs�meaning that a lot can go wrong when running them. For example, they can hang indefinitely or perform some sort of I/O that later results in a tool crash. Knowing that asset transformation was involved when debugging a crash (or a slow/stuck `flutter build`) could be useful, so I think adding a `printTrace` or two is a good idea (or at least not a bad one).
In service of #143348 This adds a simple integration test for the new asset transformation feature.
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 byvector_graphics
the user would normally have to manually run thevector_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:Then, in their app code, the developer can the
vector_graphics
package to render the asset.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 usingflutter_svg
while also enjoying the superior rendering performance ofvector_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 thevector_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:
copyAssets
to do bundle and write assets. If I'm not mistaken, this includesflutter build
for Android, iOS/macOS, Windows, Linux, and macOS.flutter build
for iOS, Android, Windows, MacOS, Linux, and web (alsoflutter run
without hot reload support) #143815flutter run
scenarios.flutter run
for web.flutter run -d <browser>
andflutter test
#144734flutter_tester
.flutter run -d <browser>
andflutter test
#144734dart run <package>
can fail if adart pub get
operation is running concurrently dart-lang/pub#4161Cleanup tasks:
flutter build
, see if we can make caching work in the presence of transformers that are path dependencies (see Enable asset transformation forflutter build
for iOS, Android, Windows, MacOS, Linux, and web (alsoflutter run
without hot reload support) #143815 (comment))The text was updated successfully, but these errors were encountered: