-
Notifications
You must be signed in to change notification settings - Fork 29.7k
feat(assets): add platform-specific asset filtering in pubspec.yaml #176393
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds platform-specific asset filtering support to Flutter by introducing a new platforms field in pubspec.yaml asset entries. This allows developers to specify which platforms should include specific assets, reducing bundle sizes by excluding irrelevant assets from builds.
- Adds
platformsfield toAssetsEntryclass with validation for supported platform names - Implements platform filtering logic in the asset bundling process to skip assets not matching the target platform
- Updates asset bundle entry constructors to include the new platforms parameter
Reviewed Changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/flutter_tools/lib/src/flutter_manifest.dart | Adds platforms field parsing and validation to AssetsEntry |
| packages/flutter_tools/lib/src/asset.dart | Updates AssetBundleEntry and _Asset classes to support platforms field |
| packages/flutter_tools/lib/src/build_system/targets/assets.dart | Implements platform filtering logic during asset copying |
| packages/flutter_tools/lib/src/base/deferred_component.dart | Adds platforms display to deferred component output |
| packages/flutter_tools/lib/src/widget_preview/preview_pubspec_builder.dart | Preserves platforms field when transforming assets |
| packages/flutter_tools/test/general.shard/flutter_manifest_test.dart | Adds comprehensive tests for platforms field parsing and validation |
| packages/flutter_tools/test/general.shard/devfs_test.dart | Updates test asset bundle entries to include platforms parameter |
| packages/flutter_tools/test/general.shard/bundle_builder_test.dart | Updates test asset bundle entries to include platforms parameter |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a valuable feature for platform-specific asset filtering in pubspec.yaml, which can significantly improve bundle sizes. The implementation correctly propagates the new platforms field through the asset pipeline, from parsing in flutter_manifest.dart to the filtering logic in build_system/targets/assets.dart. The changes include necessary updates to data classes, equality checks, and hashing, and are accompanied by relevant tests. I have one suggestion to improve the efficiency and clarity of the platform validation logic.
4c3f52d to
414d66b
Compare
bkonyi
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution!
Overall, this LGTM with some minor comments that should be addressed.
packages/flutter_tools/test/general.shard/build_system/targets/assets_test.dart
Outdated
Show resolved
Hide resolved
packages/flutter_tools/test/general.shard/build_system/targets/assets_test.dart
Outdated
Show resolved
Hide resolved
packages/flutter_tools/lib/src/build_system/targets/assets.dart
Outdated
Show resolved
Hide resolved
|
Ready for re-review. FYI, the Linux tests are currently failing, but currently I don't think my changes are related. I quickly checked a few other new PRs and saw that the Linux tests failed for them as well. However, please let me know if you think my changes are the problem. I will look into it further. |
|
Looks good to me! Thanks @hm21! I think we're experiencing some infrastructure issues, so I don't think any of those Linux failures are the result of your change. |
chingjun
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the fixes, this now looks a lot cleaner!
There is still an open question about what assets should be included in tests, but even with that unanswered I would still be ok landing this PR, and figure that out in the future.
The following are fully optional. It is out of scope for this PR, and is is fully optional whether you want to fix it in this PR, in a separate PR, or leave it for now.
The PR led me to think whether targetPlatform should still be an optional parameter in assetBundle.build(). Perhaps we should make it a required parameter.
Again, fully optional.
|
Okay |
chingjun
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
One more minor nit, and you probably want to rebase your branch as it's now 200+ commits behind HEAD.
Thank you again for your contribution, and specifically for adding the changes to make the code base more robust. Thank you!
|
An existing Git SHA, To re-trigger presubmits after closing or re-opeing a PR, or pushing a HEAD commit (i.e. with |
- Introduce `platforms` key in `flutter.assets` entries in pubspec.yaml
to restrict assets to specific build targets (e.g. windows, android, ios, web).
- Update asset loading logic in `asset.dart` and `targets/assets.dart`
to skip assets not matching the current `TargetPlatform`.
- Extend `flutter_manifest.dart` to parse and validate `platforms`.
- Update preview pubspec builder and deferred component handling
to be aware of platform-specific assets.
- Add tests for manifest parsing, invalid platform values,
and bundle builder behavior with platform-restricted assets.
Example:
```yaml
flutter:
assets:
- path: assets/icon.png
platforms: [android, ios]
```
|
An existing Git SHA, To re-trigger presubmits after closing or re-opeing a PR, or pushing a HEAD commit (i.e. with |
|
I just fixed the Google Testing for this PR. Good to be submitted now. @bkonyi do you want to take a final look at it before adding the autosubmit tag? |
Thanks! I was going to take a look at that today :) I've approved the G3Fix and this change LGTM. We should look into adding this feature to our documentation (filed #178294). Thanks for your contribution @hm21! |
…lutter#176393) ## Description This PR introduces platform-specific asset support in `pubspec.yaml`. Currently, Flutter does not allow specifying which platforms an asset should be included for. This results in all declared assets being bundled for every target platform, even if some are irrelevant (e.g. desktop-only or mobile-only images). ### What this PR changes - Adds a new optional `platforms` field under each asset in `pubspec.yaml`. - The field accepts a list of strings (platform identifiers, e.g. `["android", "ios", "web", "windows", "macos", "linux"]`). - Assets with a `platforms` restriction are only included in the bundle when building for a matching platform. - Invalid values (non-strings or unknown platform names) log an error. ### Example ```yaml flutter: assets: - path: assets/logo.png - path: assets/web_worker.js platforms: [web] - path: assets/desktop_icon.png platforms: [windows, linux, macos] ``` #### Before All assets (`logo.png`, `web_worker.js`, `desktop_icon.png`) are bundled into **every build**, regardless of platform. #### After - `logo.png` is included on all platforms. - `web_worker.js` is included only on web builds. - `desktop_icon.png` is included only on desktop builds. ### Why this is useful This significantly improves bundle size, prevents unused resources from being shipped, and gives developers better control over asset management. ## Issues Fixes flutter#65065 ## Reviewer note Would a design document be helpful for this change, or is the current explanation sufficient? ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
…lutter#176393) ## Description This PR introduces platform-specific asset support in `pubspec.yaml`. Currently, Flutter does not allow specifying which platforms an asset should be included for. This results in all declared assets being bundled for every target platform, even if some are irrelevant (e.g. desktop-only or mobile-only images). ### What this PR changes - Adds a new optional `platforms` field under each asset in `pubspec.yaml`. - The field accepts a list of strings (platform identifiers, e.g. `["android", "ios", "web", "windows", "macos", "linux"]`). - Assets with a `platforms` restriction are only included in the bundle when building for a matching platform. - Invalid values (non-strings or unknown platform names) log an error. ### Example ```yaml flutter: assets: - path: assets/logo.png - path: assets/web_worker.js platforms: [web] - path: assets/desktop_icon.png platforms: [windows, linux, macos] ``` #### Before All assets (`logo.png`, `web_worker.js`, `desktop_icon.png`) are bundled into **every build**, regardless of platform. #### After - `logo.png` is included on all platforms. - `web_worker.js` is included only on web builds. - `desktop_icon.png` is included only on desktop builds. ### Why this is useful This significantly improves bundle size, prevents unused resources from being shipped, and gives developers better control over asset management. ## Issues Fixes flutter#65065 ## Reviewer note Would a design document be helpful for this change, or is the current explanation sufficient? ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
…lutter#176393) ## Description This PR introduces platform-specific asset support in `pubspec.yaml`. Currently, Flutter does not allow specifying which platforms an asset should be included for. This results in all declared assets being bundled for every target platform, even if some are irrelevant (e.g. desktop-only or mobile-only images). ### What this PR changes - Adds a new optional `platforms` field under each asset in `pubspec.yaml`. - The field accepts a list of strings (platform identifiers, e.g. `["android", "ios", "web", "windows", "macos", "linux"]`). - Assets with a `platforms` restriction are only included in the bundle when building for a matching platform. - Invalid values (non-strings or unknown platform names) log an error. ### Example ```yaml flutter: assets: - path: assets/logo.png - path: assets/web_worker.js platforms: [web] - path: assets/desktop_icon.png platforms: [windows, linux, macos] ``` #### Before All assets (`logo.png`, `web_worker.js`, `desktop_icon.png`) are bundled into **every build**, regardless of platform. #### After - `logo.png` is included on all platforms. - `web_worker.js` is included only on web builds. - `desktop_icon.png` is included only on desktop builds. ### Why this is useful This significantly improves bundle size, prevents unused resources from being shipped, and gives developers better control over asset management. ## Issues Fixes flutter#65065 ## Reviewer note Would a design document be helpful for this change, or is the current explanation sufficient? ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
Description
This PR introduces platform-specific asset support in
pubspec.yaml.Currently, Flutter does not allow specifying which platforms an asset should be included for.
This results in all declared assets being bundled for every target platform, even if some are irrelevant (e.g. desktop-only or mobile-only images).
What this PR changes
platformsfield under each asset inpubspec.yaml.["android", "ios", "web", "windows", "macos", "linux"]).platformsrestriction are only included in the bundle when building for a matching platform.Example
Before
All assets (
logo.png,web_worker.js,desktop_icon.png) are bundled into every build, regardless of platform.After
logo.pngis included on all platforms.web_worker.jsis included only on web builds.desktop_icon.pngis included only on desktop builds.Why this is useful
This significantly improves bundle size, prevents unused resources from being shipped, and gives developers better control over asset management.
Issues
Fixes #65065
Reviewer note
Would a design document be helpful for this change, or is the current explanation sufficient?
Pre-launch Checklist
///).