Skip to content

Commit 0b93a92

Browse files
[flutter_tools] default tree-shake-icons to enabled and improve performance (flutter#54923)
1 parent 7eb8873 commit 0b93a92

File tree

8 files changed

+202
-100
lines changed

8 files changed

+202
-100
lines changed

dev/benchmarks/macrobenchmarks/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ dependencies:
1616
# and run
1717
# flutter update-packages --force-upgrade
1818
flutter_gallery_assets: 0.1.9+2
19+
cupertino_icons: 0.1.3
1920

2021
archive: 2.0.13 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
2122
args: 1.6.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
@@ -91,4 +92,4 @@ flutter:
9192
- packages/flutter_gallery_assets/food/cherry_pie.png
9293
- assets/999x1000.png
9394

94-
# PUBSPEC CHECKSUM: 3a55
95+
# PUBSPEC CHECKSUM: 49fa

packages/flutter_tools/lib/src/build_system/targets/assets.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ Future<Depfile> copyAssets(Environment environment, Directory outputDirectory) a
6060
file.parent.createSync(recursive: true);
6161
final DevFSContent content = entry.value;
6262
if (content is DevFSFileContent && content.file is File) {
63-
inputs.add(globals.fs.file(content.file.path));
63+
inputs.add(content.file as File);
6464
if (!await iconTreeShaker.subsetFont(
65-
inputPath: content.file.path,
65+
input: content.file as File,
6666
outputPath: file.path,
6767
relativePath: entry.key,
6868
)) {

packages/flutter_tools/lib/src/build_system/targets/icon_tree_shaker.dart

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:meta/meta.dart';
66
import 'package:process/process.dart';
7+
import 'package:mime/mime.dart' as mime;
78

89
import '../../artifacts.dart';
910
import '../../base/common.dart';
@@ -20,7 +21,7 @@ import 'dart.dart';
2021
const String kIconTreeShakerFlag = 'TreeShakeIcons';
2122

2223
/// Whether icon font subsetting is enabled by default.
23-
const bool kIconTreeShakerEnabledDefault = false;
24+
const bool kIconTreeShakerEnabledDefault = true;
2425

2526
List<Map<String, dynamic>> _getList(dynamic object, String errorMessage) {
2627
try {
@@ -66,6 +67,12 @@ class IconTreeShaker {
6667
}
6768
}
6869

70+
/// The MIME type for ttf fonts.
71+
static const Set<String> kTtfMimeTypes = <String>{
72+
'font/ttf', // based on internet search
73+
'application/x-font-ttf', // based on running locally.
74+
};
75+
6976
/// The [Source] inputs that targets using this should depend on.
7077
///
7178
/// See [Target.inputs].
@@ -77,6 +84,7 @@ class IconTreeShaker {
7784

7885
final Environment _environment;
7986
final String _fontManifest;
87+
Future<void> _iconDataProcessing;
8088
Map<String, _IconTreeShakerData> _iconData;
8189

8290
final ProcessManager _processManager;
@@ -89,10 +97,10 @@ class IconTreeShaker {
8997
&& _environment.defines[kIconTreeShakerFlag] == 'true'
9098
&& _environment.defines[kBuildMode] != 'debug';
9199

92-
/// Fills the [_iconData] map.
93-
Future<Map<String, _IconTreeShakerData>> _getIconData(Environment environment) async {
100+
// Fills the [_iconData] map.
101+
Future<void> _getIconData(Environment environment) async {
94102
if (!enabled) {
95-
return null;
103+
return;
96104
}
97105

98106
final File appDill = environment.buildDir.childFile('app.dill');
@@ -135,13 +143,11 @@ class IconTreeShaker {
135143
codePoints: iconData[entry.key],
136144
);
137145
}
138-
return result;
146+
_iconData = result;
139147
}
140148

141-
/// Calls font-subset, which transforms the `inputPath` font file to a
142-
/// subsetted version at `outputPath`.
143-
///
144-
/// The `relativePath` parameter
149+
/// Calls font-subset, which transforms the [input] font file to a
150+
/// subsetted version at [outputPath].
145151
///
146152
/// All parameters are required.
147153
///
@@ -150,15 +156,24 @@ class IconTreeShaker {
150156
/// If the font-subset subprocess fails, it will [throwToolExit].
151157
/// Otherwise, it will return true.
152158
Future<bool> subsetFont({
153-
@required String inputPath,
159+
@required File input,
154160
@required String outputPath,
155161
@required String relativePath,
156162
}) async {
157163
if (!enabled) {
158164
return false;
159165
}
160-
161-
_iconData ??= await _getIconData(_environment);
166+
if (input.lengthSync() < 12) {
167+
return false;
168+
}
169+
final String mimeType = mime.lookupMimeType(
170+
input.path,
171+
headerBytes: await input.openRead(0, 12).first,
172+
);
173+
if (!kTtfMimeTypes.contains(mimeType)) {
174+
return false;
175+
}
176+
await (_iconDataProcessing ??= _getIconData(_environment));
162177
assert(_iconData != null);
163178

164179
final _IconTreeShakerData iconTreeShakerData = _iconData[relativePath];
@@ -176,7 +191,7 @@ class IconTreeShaker {
176191
final List<String> cmd = <String>[
177192
fontSubset.path,
178193
outputPath,
179-
inputPath,
194+
input.path,
180195
];
181196
final String codePoints = iconTreeShakerData.codePoints.join(' ');
182197
_logger.printTrace('Running font-subset: ${cmd.join(' ')}, '
@@ -186,9 +201,7 @@ class IconTreeShaker {
186201
fontSubsetProcess.stdin.writeln(codePoints);
187202
await fontSubsetProcess.stdin.flush();
188203
await fontSubsetProcess.stdin.close();
189-
} on Exception catch (_) {
190-
// handled by checking the exit code.
191-
} on OSError catch (_) { // ignore: dead_code_on_catch_subtype
204+
} on Exception {
192205
// handled by checking the exit code.
193206
}
194207

packages/flutter_tools/test/commands.shard/permeable/build_bundle_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ void main() {
219219
kBuildMode: 'debug',
220220
kTargetPlatform: 'android-arm',
221221
kTrackWidgetCreation: 'true',
222-
kIconTreeShakerFlag: null,
222+
kIconTreeShakerFlag: 'true',
223223
});
224224

225225
return BuildResult(success: true);

0 commit comments

Comments
 (0)