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 7 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
14 changes: 13 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,18 @@ 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 deferredComponents = config.pubspec.flutter.deferredComponents ?? [];
return deferredComponents.fold<List<Object>>(
config.pubspec.flutter.assets,
(list, deferredComponent) => list + (deferredComponent.assets ?? []),
);
}
Comment on lines +52 to +62
Copy link
Member

Choose a reason for hiding this comment

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

nit: For better readability maybe:

Suggested change
/// 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 deferredComponents = config.pubspec.flutter.deferredComponents ?? [];
return deferredComponents.fold<List<Object>>(
config.pubspec.flutter.assets,
(list, deferredComponent) => list + (deferredComponent.assets ?? []),
);
}
/// Build assets from the main list and the deferred components.
List<Object> _buildFlutterAssetsList(Flutter flutter) {
final flutterAssets = flutter.assets;
// 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 deferredComponents = flutter.deferredComponents ?? [];
return deferredComponents.fold(
flutterAssets,
(list, component) => list + (component.assets ?? []),
);
}


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
Loading