Skip to content

Commit 9f0c72a

Browse files
ianmacielIan MacielAlexV525
authored
Add support for deferred components (#576)
## What does this change? This PR include support for [Flutter Deferred Components](https://docs.flutter.dev/perf/deferred-components). Deferred components allow developers to the app into multiple apk to reduce its size. This can be used to optimize the initial download and download components only on necessary but it is also mandatory for apk with more than 200MB. This library initially looks for assets listed on `flutter.assets` of `pubspec.yaml`, with this change the library will continue looking for the assets lists under `flutter.assets` but will merge with the list of assets included on `flutter.deferred-components.$`. This will be merged in a single list. Ideally in the future assets should have different classname, so users will easily know when dealing with deferred components. This wasn't implemented at this moment because it would requeire a significative changes. Fixes #577 🎯 ## Type of change - [x] New feature (non-breaking change which adds functionality) - [x] This change requires a documentation update --------- Co-authored-by: Ian Maciel <[email protected]> Co-authored-by: Alex Li <[email protected]>
1 parent d252f15 commit 9f0c72a

File tree

9 files changed

+493
-1
lines changed

9 files changed

+493
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,14 @@ flutter:
212212
- assets/flare/Penguin.flr
213213
- assets/rive/vehicles.riv
214214
- pictures/ocean_view.jpg
215+
216+
# Also include assets from deferred components
217+
# https://docs.flutter.dev/perf/deferred-components
218+
deferred-components:
219+
- name: myDeferredComponent
220+
assets:
221+
- assets/images/another_image.jps
222+
- assets/videos/a_large_video.mp4
215223
```
216224

217225
These configurations will generate **`assets.gen.dart`** under the **`lib/gen/`** directory by default.

packages/core/lib/generators/assets_generator.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class AssetsGenConfig {
3333
pubspecFile.parent.absolute.path,
3434
config.pubspec.packageName,
3535
config.pubspec.flutterGen,
36-
config.pubspec.flutter.assets,
36+
_buildFlutterAssetsList(config.pubspec.flutter),
3737
config.pubspec.flutterGen.assets.exclude.map(Glob.new).toList(),
3838
);
3939
}
@@ -48,6 +48,19 @@ class AssetsGenConfig {
4848
flutterGen.assets.outputs.packageParameterEnabled ? _packageName : '';
4949
}
5050

51+
/// Build assets from the main list and the deferred components.
52+
List<Object> _buildFlutterAssetsList(Flutter flutter) {
53+
final flutterAssets = flutter.assets;
54+
// We may have several deferred components, with a list of assets for each.
55+
// So before spreading the list of deferred components, we need to spread
56+
// the list of assets for each deferred component.
57+
final deferredComponents = flutter.deferredComponents ?? [];
58+
return deferredComponents.fold(
59+
flutterAssets,
60+
(list, component) => list + (component.assets ?? []),
61+
);
62+
}
63+
5164
Future<String> generateAssets(
5265
AssetsGenConfig config,
5366
DartFormatter formatter,

packages/core/lib/settings/pubspec.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class Flutter {
7676
const Flutter({
7777
required this.assets,
7878
required this.fonts,
79+
required this.deferredComponents,
7980
});
8081

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

8687
@JsonKey(name: 'fonts', required: true)
8788
final List<FlutterFonts> fonts;
89+
90+
@JsonKey(name: 'deferred-components', required: false)
91+
final List<FlutterDeferredComponents>? deferredComponents;
8892
}
8993

9094
@JsonSerializable(disallowUnrecognizedKeys: false)
@@ -311,3 +315,20 @@ class FlutterGenElementFontsOutputs extends FlutterGenElementOutputs {
311315
@JsonKey(name: 'package_parameter_enabled', defaultValue: false)
312316
final bool packageParameterEnabled;
313317
}
318+
319+
@JsonSerializable()
320+
class FlutterDeferredComponents {
321+
const FlutterDeferredComponents({
322+
required this.name,
323+
required this.assets,
324+
});
325+
326+
factory FlutterDeferredComponents.fromJson(Map json) =>
327+
_$FlutterDeferredComponentsFromJson(json);
328+
329+
@JsonKey(name: 'name', required: true)
330+
final String name;
331+
332+
@JsonKey(name: 'assets', required: false)
333+
final List<Object>? assets;
334+
}

packages/core/lib/settings/pubspec.g.dart

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/test/assets_gen_test.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ void main() {
102102
await expectedAssetsGen(pubspec);
103103
});
104104

105+
test('Assets with deferred components assets', () async {
106+
const pubspec = 'test_resources/pubspec_assets_deferred_components.yaml';
107+
await expectedAssetsGen(pubspec);
108+
});
109+
105110
test('Assets with duplicate flavoring entries', () async {
106111
const pubspec =
107112
'test_resources/pubspec_assets_flavored_duplicate_entry.yaml';

0 commit comments

Comments
 (0)