[flutter_tools] tree shake icons from web builds (#115886)

* wip

* remove temp text file

* fix tests

* add test

* default to off

* restore gitignore

* update

* apply annotation to cupertino icons as well

* update reference to library in icon_tree_shaker.dart

* update tests

* fix tests

* remove hack to skip non-const check on web

* add hint about how much reduction and test
This commit is contained in:
Christopher Fujino
2022-12-15 14:13:17 -08:00
committed by GitHub
parent ada4460502
commit 1eaf5c0f08
8 changed files with 83 additions and 42 deletions

View File

@@ -190,7 +190,7 @@ class KernelSnapshot extends Target {
// Force linking of the platform for desktop embedder targets since these
// do not correctly load the core snapshots in debug mode.
// See https://github.com/flutter/flutter/issues/44724
bool forceLinkPlatform;
final bool forceLinkPlatform;
switch (targetPlatform) {
case TargetPlatform.darwin:
case TargetPlatform.windows_x64:

View File

@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:meta/meta.dart';
import 'package:mime/mime.dart' as mime;
import 'package:process/process.dart';
@@ -150,8 +151,6 @@ class IconTreeShaker {
/// Calls font-subset, which transforms the [input] font file to a
/// subsetted version at [outputPath].
///
/// All parameters are required.
///
/// If [enabled] is false, or the relative path is not recognized as an icon
/// font used in the Flutter application, this returns false.
/// If the font-subset subprocess fails, it will [throwToolExit].
@@ -161,6 +160,7 @@ class IconTreeShaker {
required String outputPath,
required String relativePath,
}) async {
if (!enabled) {
return false;
}
@@ -212,9 +212,23 @@ class IconTreeShaker {
_logger.printError(await utf8.decodeStream(fontSubsetProcess.stderr));
throw IconTreeShakerException._('Font subsetting failed with exit code $code.');
}
_logger.printStatus(getSubsetSummaryMessage(input, _fs.file(outputPath)));
return true;
}
@visibleForTesting
String getSubsetSummaryMessage(File inputFont, File outputFont) {
final String fontName = inputFont.basename;
final double inputSize = inputFont.lengthSync().toDouble();
final double outputSize = outputFont.lengthSync().toDouble();
final double reductionBytes = inputSize - outputSize;
final String reductionPercentage = (reductionBytes / inputSize * 100).toStringAsFixed(1);
return 'Font asset "$fontName" was tree-shaken, reducing it from '
'${inputSize.ceil()} to ${outputSize.ceil()} bytes '
'($reductionPercentage% reduction). Tree-shaking can be disabled '
'by providing the --no-tree-shake-icons flag when building your app.';
}
/// Returns a map of { fontFamily: relativePath } pairs.
Future<Map<String, String>> _parseFontJson(
String fontManifestData,
@@ -268,6 +282,8 @@ class IconTreeShaker {
'--kernel-file', appDill.path,
'--class-library-uri', 'package:flutter/src/widgets/icon_data.dart',
'--class-name', 'IconData',
'--annotation-class-name', '_StaticIconProvider',
'--annotation-class-library-uri', 'package:flutter/src/widgets/icon_data.dart',
];
_logger.printTrace('Running command: ${cmd.join(' ')}');
final ProcessResult constFinderProcessResult = await _processManager.run(cmd);

View File

@@ -19,7 +19,7 @@ class BuildWebCommand extends BuildSubCommand {
required FileSystem fileSystem,
required bool verboseHelp,
}) : _fileSystem = fileSystem, super(verboseHelp: verboseHelp) {
addTreeShakeIconsFlag(enabledByDefault: false);
addTreeShakeIconsFlag();
usesTargetOption();
usesOutputDir();
usesPubOption();