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

Add support for deferred components #576

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@ flutter:
- assets/flare/Penguin.flr
- assets/rive/vehicles.riv
- pictures/ocean_view.jpg

# Also include assets from deferred components
# https://docs.flutter.dev/perf/deferred-components
deferred-components:
- name: myDeferredComponent
assets:
- assets/images/another_image.jps
- assets/videos/a_large_video.mp4
```

These configurations will generate **`assets.gen.dart`** under the **`lib/gen/`** directory by default.
Expand Down
17 changes: 16 additions & 1 deletion packages/core/lib/generators/assets_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class AssetsGenConfig {
pubspecFile.parent.absolute.path,
config.pubspec.packageName,
config.pubspec.flutterGen,
config.pubspec.flutter.assets,
_buildAssetsList(config),
config.pubspec.flutterGen.assets.exclude.map(Glob.new).toList(),
);
}
Expand All @@ -49,6 +49,21 @@ class AssetsGenConfig {
flutterGen.assets.outputs.packageParameterEnabled ? _packageName : '';
}

/// Merge the deferred assets with the main assets.
List<Object> _buildAssetsList(Config config) {
// We may have several deferred components, with a list of assets for each.
// So before spreading the list of deferred components, we need to spread
// the list of assets for each deferred component.
final List<Object> deferredAssets = [];
config.pubspec.flutter.deferredComponents?.forEach((deferredComponent) {
// Include all manipulated assets to the list of deferred assets.
deferredAssets.addAll(deferredComponent.assets ?? []);
});

// Merge the deferred assets with the main assets.
return [...config.pubspec.flutter.assets, ...deferredAssets];
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't [...?config.pubspec.flutter.deferredAssets?.assets] do the same thing as above?

Copy link
Author

@ianmaciel ianmaciel Sep 26, 2024

Choose a reason for hiding this comment

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

That's true...! good catch!
I actually don't need the forEach loop, just need to merge the two lists.

BTW, do you mean?

image

Copy link
Member

Choose a reason for hiding this comment

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

Yeah exactly

Copy link
Author

Choose a reason for hiding this comment

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

I'm sorry. For a moment I thought it was the same but it is not. I need to revert it.

The fact is we may have many Deferret Components, with many assets each (a list of lists).

So before spreading the list of deferredComponents, it is necessary to spread the assets from each deferredComponent

Copy link
Member

Choose a reason for hiding this comment

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

So you may reduce components' assets into one?

Copy link
Author

Choose a reason for hiding this comment

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

I didn't get that. 🤔
What do you mean by reduce components assets?

Copy link
Member

Choose a reason for hiding this comment

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

Something like reduce((list, assets) => list + assets)

Copy link
Author

Choose a reason for hiding this comment

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

I belive I'd need to use fold instead of reduce to have an initial empty list (I might have nothing at this point):
So I got something like this:
image

Copy link
Author

Choose a reason for hiding this comment

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

any thoughts?

}

Future<String> generateAssets(
AssetsGenConfig config,
DartFormatter formatter,
Expand Down
21 changes: 21 additions & 0 deletions packages/core/lib/settings/pubspec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Flutter {
Flutter({
required this.assets,
required this.fonts,
required this.deferredComponents,
});

@JsonKey(name: 'assets', required: true)
Expand All @@ -37,6 +38,9 @@ class Flutter {
@JsonKey(name: 'fonts', required: true)
final List<FlutterFonts> fonts;

@JsonKey(name: 'deferred-components', required: false)
final List<FlutterDeferredComponents>? deferredComponents;

factory Flutter.fromJson(Map json) => _$FlutterFromJson(json);
}

Expand Down Expand Up @@ -243,3 +247,20 @@ class FlutterGenElementFontsOutputs extends FlutterGenElementOutputs {
factory FlutterGenElementFontsOutputs.fromJson(Map json) =>
_$FlutterGenElementFontsOutputsFromJson(json);
}

AlexV525 marked this conversation as resolved.
Show resolved Hide resolved
@JsonSerializable()
class FlutterDeferredComponents {
const FlutterDeferredComponents({
required this.name,
required this.assets,
});

@JsonKey(name: 'name', required: true)
final String name;

@JsonKey(name: 'assets', required: false)
final List<Object>? assets;

factory FlutterDeferredComponents.fromJson(Map json) =>
_$FlutterDeferredComponentsFromJson(json);
}
24 changes: 24 additions & 0 deletions packages/core/lib/settings/pubspec.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions packages/core/test/assets_gen_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ void main() {
await expectedAssetsGen(pubspec, generated, fact);
});

test('Assets with deferred components assets', () async {
const pubspec = 'test_resources/pubspec_assets_deferred_components.yaml';
const fact =
'test_resources/actual_data/assets_deferred_components.gen.dart';
const generated =
'test_resources/lib/gen/assets_deferred_components.gen.dart';

await expectedAssetsGen(pubspec, generated, fact);
});

test('Assets with duplicate flavoring entries', () async {
const pubspec =
'test_resources/pubspec_assets_flavored_duplicate_entry.yaml';
Expand Down
Loading