Unverified Commit 0b93a921 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] default tree-shake-icons to enabled and improve performance (#54923)

parent 7eb8873a
...@@ -16,6 +16,7 @@ dependencies: ...@@ -16,6 +16,7 @@ dependencies:
# and run # and run
# flutter update-packages --force-upgrade # flutter update-packages --force-upgrade
flutter_gallery_assets: 0.1.9+2 flutter_gallery_assets: 0.1.9+2
cupertino_icons: 0.1.3
archive: 2.0.13 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" archive: 2.0.13 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
args: 1.6.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" args: 1.6.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
...@@ -91,4 +92,4 @@ flutter: ...@@ -91,4 +92,4 @@ flutter:
- packages/flutter_gallery_assets/food/cherry_pie.png - packages/flutter_gallery_assets/food/cherry_pie.png
- assets/999x1000.png - assets/999x1000.png
# PUBSPEC CHECKSUM: 3a55 # PUBSPEC CHECKSUM: 49fa
...@@ -60,9 +60,9 @@ Future<Depfile> copyAssets(Environment environment, Directory outputDirectory) a ...@@ -60,9 +60,9 @@ Future<Depfile> copyAssets(Environment environment, Directory outputDirectory) a
file.parent.createSync(recursive: true); file.parent.createSync(recursive: true);
final DevFSContent content = entry.value; final DevFSContent content = entry.value;
if (content is DevFSFileContent && content.file is File) { if (content is DevFSFileContent && content.file is File) {
inputs.add(globals.fs.file(content.file.path)); inputs.add(content.file as File);
if (!await iconTreeShaker.subsetFont( if (!await iconTreeShaker.subsetFont(
inputPath: content.file.path, input: content.file as File,
outputPath: file.path, outputPath: file.path,
relativePath: entry.key, relativePath: entry.key,
)) { )) {
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import 'package:process/process.dart'; import 'package:process/process.dart';
import 'package:mime/mime.dart' as mime;
import '../../artifacts.dart'; import '../../artifacts.dart';
import '../../base/common.dart'; import '../../base/common.dart';
...@@ -20,7 +21,7 @@ import 'dart.dart'; ...@@ -20,7 +21,7 @@ import 'dart.dart';
const String kIconTreeShakerFlag = 'TreeShakeIcons'; const String kIconTreeShakerFlag = 'TreeShakeIcons';
/// Whether icon font subsetting is enabled by default. /// Whether icon font subsetting is enabled by default.
const bool kIconTreeShakerEnabledDefault = false; const bool kIconTreeShakerEnabledDefault = true;
List<Map<String, dynamic>> _getList(dynamic object, String errorMessage) { List<Map<String, dynamic>> _getList(dynamic object, String errorMessage) {
try { try {
...@@ -66,6 +67,12 @@ class IconTreeShaker { ...@@ -66,6 +67,12 @@ class IconTreeShaker {
} }
} }
/// The MIME type for ttf fonts.
static const Set<String> kTtfMimeTypes = <String>{
'font/ttf', // based on internet search
'application/x-font-ttf', // based on running locally.
};
/// The [Source] inputs that targets using this should depend on. /// The [Source] inputs that targets using this should depend on.
/// ///
/// See [Target.inputs]. /// See [Target.inputs].
...@@ -77,6 +84,7 @@ class IconTreeShaker { ...@@ -77,6 +84,7 @@ class IconTreeShaker {
final Environment _environment; final Environment _environment;
final String _fontManifest; final String _fontManifest;
Future<void> _iconDataProcessing;
Map<String, _IconTreeShakerData> _iconData; Map<String, _IconTreeShakerData> _iconData;
final ProcessManager _processManager; final ProcessManager _processManager;
...@@ -89,10 +97,10 @@ class IconTreeShaker { ...@@ -89,10 +97,10 @@ class IconTreeShaker {
&& _environment.defines[kIconTreeShakerFlag] == 'true' && _environment.defines[kIconTreeShakerFlag] == 'true'
&& _environment.defines[kBuildMode] != 'debug'; && _environment.defines[kBuildMode] != 'debug';
/// Fills the [_iconData] map. // Fills the [_iconData] map.
Future<Map<String, _IconTreeShakerData>> _getIconData(Environment environment) async { Future<void> _getIconData(Environment environment) async {
if (!enabled) { if (!enabled) {
return null; return;
} }
final File appDill = environment.buildDir.childFile('app.dill'); final File appDill = environment.buildDir.childFile('app.dill');
...@@ -135,13 +143,11 @@ class IconTreeShaker { ...@@ -135,13 +143,11 @@ class IconTreeShaker {
codePoints: iconData[entry.key], codePoints: iconData[entry.key],
); );
} }
return result; _iconData = result;
} }
/// Calls font-subset, which transforms the `inputPath` font file to a /// Calls font-subset, which transforms the [input] font file to a
/// subsetted version at `outputPath`. /// subsetted version at [outputPath].
///
/// The `relativePath` parameter
/// ///
/// All parameters are required. /// All parameters are required.
/// ///
...@@ -150,15 +156,24 @@ class IconTreeShaker { ...@@ -150,15 +156,24 @@ class IconTreeShaker {
/// If the font-subset subprocess fails, it will [throwToolExit]. /// If the font-subset subprocess fails, it will [throwToolExit].
/// Otherwise, it will return true. /// Otherwise, it will return true.
Future<bool> subsetFont({ Future<bool> subsetFont({
@required String inputPath, @required File input,
@required String outputPath, @required String outputPath,
@required String relativePath, @required String relativePath,
}) async { }) async {
if (!enabled) { if (!enabled) {
return false; return false;
} }
if (input.lengthSync() < 12) {
_iconData ??= await _getIconData(_environment); return false;
}
final String mimeType = mime.lookupMimeType(
input.path,
headerBytes: await input.openRead(0, 12).first,
);
if (!kTtfMimeTypes.contains(mimeType)) {
return false;
}
await (_iconDataProcessing ??= _getIconData(_environment));
assert(_iconData != null); assert(_iconData != null);
final _IconTreeShakerData iconTreeShakerData = _iconData[relativePath]; final _IconTreeShakerData iconTreeShakerData = _iconData[relativePath];
...@@ -176,7 +191,7 @@ class IconTreeShaker { ...@@ -176,7 +191,7 @@ class IconTreeShaker {
final List<String> cmd = <String>[ final List<String> cmd = <String>[
fontSubset.path, fontSubset.path,
outputPath, outputPath,
inputPath, input.path,
]; ];
final String codePoints = iconTreeShakerData.codePoints.join(' '); final String codePoints = iconTreeShakerData.codePoints.join(' ');
_logger.printTrace('Running font-subset: ${cmd.join(' ')}, ' _logger.printTrace('Running font-subset: ${cmd.join(' ')}, '
...@@ -186,9 +201,7 @@ class IconTreeShaker { ...@@ -186,9 +201,7 @@ class IconTreeShaker {
fontSubsetProcess.stdin.writeln(codePoints); fontSubsetProcess.stdin.writeln(codePoints);
await fontSubsetProcess.stdin.flush(); await fontSubsetProcess.stdin.flush();
await fontSubsetProcess.stdin.close(); await fontSubsetProcess.stdin.close();
} on Exception catch (_) { } on Exception {
// handled by checking the exit code.
} on OSError catch (_) { // ignore: dead_code_on_catch_subtype
// handled by checking the exit code. // handled by checking the exit code.
} }
......
...@@ -219,7 +219,7 @@ void main() { ...@@ -219,7 +219,7 @@ void main() {
kBuildMode: 'debug', kBuildMode: 'debug',
kTargetPlatform: 'android-arm', kTargetPlatform: 'android-arm',
kTrackWidgetCreation: 'true', kTrackWidgetCreation: 'true',
kIconTreeShakerFlag: null, kIconTreeShakerFlag: 'true',
}); });
return BuildResult(success: true); return BuildResult(success: true);
......
...@@ -110,7 +110,7 @@ void main() { ...@@ -110,7 +110,7 @@ void main() {
testUsingContext('build aot outputs timing info', () async { testUsingContext('build aot outputs timing info', () async {
globals.fs globals.fs
.file('.dart_tool/flutter_build/0c21fd4ab3b8bde8b385ff01d08e0093/app.so') .file('.dart_tool/flutter_build/3f206b606f73e08587a94405f2e86fad/app.so')
.createSync(recursive: true); .createSync(recursive: true);
when(globals.buildSystem.build(any, any)) when(globals.buildSystem.build(any, any))
.thenAnswer((Invocation invocation) async { .thenAnswer((Invocation invocation) async {
......
...@@ -238,6 +238,7 @@ void main() { ...@@ -238,6 +238,7 @@ void main() {
'-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}', '-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}',
'-Ptrack-widget-creation=true', '-Ptrack-widget-creation=true',
'-Pshrink=true', '-Pshrink=true',
'-Ptree-shake-icons=true',
'assembleRelease', 'assembleRelease',
], ],
workingDirectory: anyNamed('workingDirectory'), workingDirectory: anyNamed('workingDirectory'),
...@@ -267,6 +268,7 @@ void main() { ...@@ -267,6 +268,7 @@ void main() {
'-Ptrack-widget-creation=true', '-Ptrack-widget-creation=true',
'-Pshrink=true', '-Pshrink=true',
'-Psplit-debug-info=${tempDir.path}', '-Psplit-debug-info=${tempDir.path}',
'-Ptree-shake-icons=true',
'assembleRelease', 'assembleRelease',
], ],
workingDirectory: anyNamed('workingDirectory'), workingDirectory: anyNamed('workingDirectory'),
...@@ -299,6 +301,7 @@ void main() { ...@@ -299,6 +301,7 @@ void main() {
'-Ptrack-widget-creation=true', '-Ptrack-widget-creation=true',
'-Pextra-front-end-options=foo,bar', '-Pextra-front-end-options=foo,bar',
'-Pshrink=true', '-Pshrink=true',
'-Ptree-shake-icons=true',
'assembleRelease', 'assembleRelease',
], ],
workingDirectory: anyNamed('workingDirectory'), workingDirectory: anyNamed('workingDirectory'),
...@@ -329,6 +332,7 @@ void main() { ...@@ -329,6 +332,7 @@ void main() {
'-Ptarget-platform=android-arm,android-arm64,android-x64', '-Ptarget-platform=android-arm,android-arm64,android-x64',
'-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}', '-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}',
'-Ptrack-widget-creation=true', '-Ptrack-widget-creation=true',
'-Ptree-shake-icons=true',
'assembleRelease', 'assembleRelease',
], ],
workingDirectory: anyNamed('workingDirectory'), workingDirectory: anyNamed('workingDirectory'),
...@@ -353,6 +357,7 @@ void main() { ...@@ -353,6 +357,7 @@ void main() {
'-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}', '-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}',
'-Ptrack-widget-creation=true', '-Ptrack-widget-creation=true',
'-Pshrink=true', '-Pshrink=true',
'-Ptree-shake-icons=true',
'assembleRelease', 'assembleRelease',
], ],
workingDirectory: anyNamed('workingDirectory'), workingDirectory: anyNamed('workingDirectory'),
...@@ -414,6 +419,7 @@ void main() { ...@@ -414,6 +419,7 @@ void main() {
'-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}', '-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}',
'-Ptrack-widget-creation=true', '-Ptrack-widget-creation=true',
'-Pshrink=true', '-Pshrink=true',
'-Ptree-shake-icons=true',
'assembleRelease', 'assembleRelease',
], ],
workingDirectory: anyNamed('workingDirectory'), workingDirectory: anyNamed('workingDirectory'),
...@@ -465,6 +471,7 @@ void main() { ...@@ -465,6 +471,7 @@ void main() {
'-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}', '-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}',
'-Ptrack-widget-creation=true', '-Ptrack-widget-creation=true',
'-Pshrink=true', '-Pshrink=true',
'-Ptree-shake-icons=true',
'assembleRelease', 'assembleRelease',
], ],
workingDirectory: anyNamed('workingDirectory'), workingDirectory: anyNamed('workingDirectory'),
......
...@@ -225,6 +225,7 @@ void main() { ...@@ -225,6 +225,7 @@ void main() {
'-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}', '-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}',
'-Ptrack-widget-creation=false', '-Ptrack-widget-creation=false',
'-Pshrink=true', '-Pshrink=true',
'-Ptree-shake-icons=true',
'bundleRelease', 'bundleRelease',
], ],
workingDirectory: anyNamed('workingDirectory'), workingDirectory: anyNamed('workingDirectory'),
...@@ -257,6 +258,7 @@ void main() { ...@@ -257,6 +258,7 @@ void main() {
'-Ptarget-platform=android-arm,android-arm64,android-x64', '-Ptarget-platform=android-arm,android-arm64,android-x64',
'-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}', '-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}',
'-Ptrack-widget-creation=false', '-Ptrack-widget-creation=false',
'-Ptree-shake-icons=true',
'bundleRelease', 'bundleRelease',
], ],
workingDirectory: anyNamed('workingDirectory'), workingDirectory: anyNamed('workingDirectory'),
...@@ -281,6 +283,7 @@ void main() { ...@@ -281,6 +283,7 @@ void main() {
'-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}', '-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}',
'-Ptrack-widget-creation=false', '-Ptrack-widget-creation=false',
'-Pshrink=true', '-Pshrink=true',
'-Ptree-shake-icons=true',
'bundleRelease', 'bundleRelease',
], ],
workingDirectory: anyNamed('workingDirectory'), workingDirectory: anyNamed('workingDirectory'),
...@@ -342,6 +345,7 @@ void main() { ...@@ -342,6 +345,7 @@ void main() {
'-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}', '-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}',
'-Ptrack-widget-creation=false', '-Ptrack-widget-creation=false',
'-Pshrink=true', '-Pshrink=true',
'-Ptree-shake-icons=true',
'assembleRelease', 'assembleRelease',
], ],
workingDirectory: anyNamed('workingDirectory'), workingDirectory: anyNamed('workingDirectory'),
...@@ -393,6 +397,7 @@ void main() { ...@@ -393,6 +397,7 @@ void main() {
'-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}', '-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}',
'-Ptrack-widget-creation=false', '-Ptrack-widget-creation=false',
'-Pshrink=true', '-Pshrink=true',
'-Ptree-shake-icons=true',
'assembleRelease', 'assembleRelease',
], ],
workingDirectory: anyNamed('workingDirectory'), workingDirectory: anyNamed('workingDirectory'),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment