Skip to content

Add support for deferred components #576

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

Merged
Merged
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 @@ -212,6 +212,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
15 changes: 14 additions & 1 deletion packages/core/lib/generators/assets_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AssetsGenConfig {
pubspecFile.parent.absolute.path,
config.pubspec.packageName,
config.pubspec.flutterGen,
config.pubspec.flutter.assets,
_buildFlutterAssetsList(config.pubspec.flutter),
config.pubspec.flutterGen.assets.exclude.map(Glob.new).toList(),
);
}
Expand All @@ -48,6 +48,19 @@ class AssetsGenConfig {
flutterGen.assets.outputs.packageParameterEnabled ? _packageName : '';
}

/// 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 @@ -76,6 +76,7 @@ class Flutter {
const Flutter({
required this.assets,
required this.fonts,
required this.deferredComponents,
});

factory Flutter.fromJson(Map json) => _$FlutterFromJson(json);
Expand All @@ -85,6 +86,9 @@ class Flutter {

@JsonKey(name: 'fonts', required: true)
final List<FlutterFonts> fonts;

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

@JsonSerializable(disallowUnrecognizedKeys: false)
Expand Down Expand Up @@ -311,3 +315,20 @@ class FlutterGenElementFontsOutputs extends FlutterGenElementOutputs {
@JsonKey(name: 'package_parameter_enabled', defaultValue: false)
final bool packageParameterEnabled;
}

@JsonSerializable()
class FlutterDeferredComponents {
const FlutterDeferredComponents({
required this.name,
required this.assets,
});

factory FlutterDeferredComponents.fromJson(Map json) =>
_$FlutterDeferredComponentsFromJson(json);

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

@JsonKey(name: 'assets', required: false)
final List<Object>? assets;
}
25 changes: 25 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.

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

test('Assets with deferred components assets', () async {
const pubspec = 'test_resources/pubspec_assets_deferred_components.yaml';
await expectedAssetsGen(pubspec);
});

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