Skip to content
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

Document asset transformation feature #10471

Merged
merged 16 commits into from
May 12, 2024

Conversation

andrewkolos
Copy link
Contributor

@andrewkolos andrewkolos commented Apr 30, 2024

Documents the new asset transformation feature (if curious, see flutter/flutter#143348; but the documentation included in this PR should be sufficient for learning about and understanding this feature.)

For reviewers. The documentation includes a link to a yet-to-be-merged sample in the flutter/samples repo. When you arrive at this link, I ask that you review flutter/samples#2267, which is the PR that adds the sample being linked to.

Presubmit checklist

@flutter-website-bot
Copy link
Collaborator

flutter-website-bot commented Apr 30, 2024

Visit the preview URL for this PR (updated for commit 48dde01):

https://flutter-docs-prod--pr10471-document-asset-transformers-7yrye4qk.web.app

Comment on lines 63 to 69
### Automatic transformation of asset files at build time

You can have asset files transformed by dart packages as they are bundled into
your app.
To learn how to do this and write your own asset-transforming packages, see
[Transforming assets at build time][].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine this to be a fairly niche (even if highly useful) feature, so I didn't want to fully explain it on this page but rather give it its own page and link to that. However, this section feels a bit sparse (and too easy to gloss over) to me. If anyone has ideas to improve this, please let me know.

@andrewkolos andrewkolos changed the title [WIP] Document asset transformation feature Document asset transformation feature Apr 30, 2024
Copy link
Contributor

@sfshaza2 sfshaza2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had this PR half reviewed yesterday. Sorry for the delay.

src/content/ui/assets/asset-transformers.md Outdated Show resolved Hide resolved
Comment on lines 12 to 13
You can describe which assets should transformed and which package should be
used transform them in your `pubspec.yaml` file.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
You can describe which assets should transformed and which package should be
used transform them in your `pubspec.yaml` file.
List the assets to be transformed, and by which package,
in your `pubspec.yaml` file.
For example, the following example configures Flutter build
to transform an SVG file to an optimized binary file
with the [`vector_graphics_compiler`][] package on pub.dev:
[`vector_]: {{site.pub-pkg}}/vector_graphics_compiler

Comment on lines 23 to 26
With this configuration, `assets/logo.svg` will be transformed by the
`vector_graphics_compiler` package as it is copied to the build output. This
package precompiles SVG files into an optimized binary files that can be
displayed using the `vector_graphics` package, like so:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
With this configuration, `assets/logo.svg` will be transformed by the
`vector_graphics_compiler` package as it is copied to the build output. This
package precompiles SVG files into an optimized binary files that can be
displayed using the `vector_graphics` package, like so:
With this configuration, `assets/logo.svg` will be transformed by the
`vector_graphics_compiler` package as it's copied to the build output.
You don't have to write the following code, but it shows what happens
on your behalf, once configured:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

package precompiles SVG files into an optimized binary files that can be
displayed using the `vector_graphics` package, like so:

<?code-excerpt "ui/assets_and_images/lib/logo.dart (TransformedAsset)"?>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: This Dart code is autogenerated, yes? This is just informational, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this feature does not generate any application code, it only transforms the asset file. Basically, instead of just copying the asset file the app bundle as-is, it runs dart run vector_graphics_compiler --input="assets/logo.svg" --output=<path to assets dir in build output>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected a typo in my previous comment

Comment on lines 39 to 40
You can specify a string of arguments to passed to an asset transformer, just like
a command-line program:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
You can specify a string of arguments to passed to an asset transformer, just like
a command-line program:
To pass a string of arguments to an asset transformer,
also specify that in the pubspec:

Comment on lines 51 to 52
In addition, asset transformers can be chained, with transformations applying in
the order they are declared. Consider this example using made-up packages:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Omigosh, I was wondering that exact thing about now. Excellent!

Suggested change
In addition, asset transformers can be chained, with transformations applying in
the order they are declared. Consider this example using made-up packages:
Asset transformers can be chained and are applied in
the order they are declared.
Consider the following example using imaginary packages:

Comment on lines 63 to 64
Here, `bird.png` is transformed by the `grayscale_filter` package. The output
is then transformed by `png_optimizer`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Here, `bird.png` is transformed by the `grayscale_filter` package. The output
is then transformed by `png_optimizer`.
Here, `bird.png` is transformed by the `grayscale_filter` package.
The output is then transformed by `png_optimizer`.

@sfshaza2
Copy link
Contributor

sfshaza2 commented May 1, 2024

Despite @domesticmouse's approval, please do not land yet.

@sfshaza2
Copy link
Contributor

sfshaza2 commented May 5, 2024

@andrewkolos, can you ping me directly when you feel this is ready to land?

@domesticmouse
Copy link
Contributor

This should land as part of the Flutter 3.22 release with flutter/samples#2267.

Copy link
Contributor

@sfshaza2 sfshaza2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can land this when the linked to sample lands.

Copy link
Contributor

@sfshaza2 sfshaza2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments. Also, can you add this new file to the sidenav? That will definitely increase visibility. Do this by adding it to /src/_data/sidenav.yml

src/content/ui/assets/asset-transformation.md Outdated Show resolved Hide resolved
src/content/ui/assets/asset-transformation.md Outdated Show resolved Hide resolved
src/content/ui/assets/asset-transformation.md Outdated Show resolved Hide resolved
src/content/ui/assets/asset-transformation.md Outdated Show resolved Hide resolved
src/content/ui/assets/assets-and-images.md Outdated Show resolved Hide resolved
@andrewkolos
Copy link
Contributor Author

few comments. Also, can you add this new file to the sidenav? That will definitely increase visibility. Do this by adding it to /src/_data/sidenav.yml

Good idea! Done.

Copy link
Contributor

@sfshaza2 sfshaza2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@sfshaza2 sfshaza2 merged commit 04a0ddb into flutter:main May 12, 2024
9 checks passed
@andrewkolos
Copy link
Contributor Author

Should this be reverted since 3.22 (and thus this feature) is not released yet?

@sfshaza2
Copy link
Contributor

sfshaza2 commented May 13, 2024 via email

ericwindmill added a commit to flutter/samples that referenced this pull request May 14, 2024
This adds a sample Flutter project that demonstrates a
soon-to-be-released feature, asset transformation[^1]. [PR for
flutter.dev
documentation](flutter/website#10471).

This feature isn't the easiest to explain using documentation, so I
think augmenting that documentation with a sample is appropriate.

This sample demonstrates 1) how to use an existing Dart package (that is
compatible with the feature) as an asset transformer and 2) how to write
a Dart package that is compatible with this feature. This should be
clear from the README.md.

**Advice for reviewing this PR.** The goal here is that most users that
read the documentation and follow the link from there to this sample
should be able to figure out what the feature does and how to use it.
Try to imagine yourself in this position and follow this story. If the
feature is still unclear, then there is probably something we can do to
improve this sample or the docs. Said more simply, follow these steps:
1) Start at the new section to be added to Flutter.dev
(https://flutter-docs-prod--pr10471-document-asset-transformers-cc21qf01.web.app/ui/assets/assets-and-images#automatic-transformation-of-asset-files-at-build-time).
It should naturally link you to the sample project. Start with the
README and see if things make sense.

## Pre-launch Checklist

- [X] I read the [Flutter Style Guide] _recently_, and have followed its
advice.
- [X] I signed the [CLA].
- [X] I read the [Contributors Guide].
- [X] I updated/added relevant documentation (doc comments with `///`).
- [X] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-devrel
channel on [Discord].

<!-- Links -->
[Flutter Style Guide]:
https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo
[CLA]: https://cla.developers.google.com/
[Discord]: https://github.com/flutter/flutter/wiki/Chat
[Contributors Guide]:
https://github.com/flutter/samples/blob/main/CONTRIBUTING.md

[^1]: If you are super curious about this feature, see [the tracking
issue for its
implementation](flutter/flutter#143348).

---------

Co-authored-by: Eric Windmill <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants