Skip to content

Commit

Permalink
[flutter_tools] Add --dump-info, --no-frequency-based-minification fl…
Browse files Browse the repository at this point in the history
…ags (#115862)

* [flutter_tools] Add --dump-info, --no-frequency-based-minification flags

Also some cleanup to named arguments to the buildWeb function

Fixes flutter/flutter#115854

* fix tests
  • Loading branch information
kevmoo authored Nov 22, 2022
1 parent 73024eb commit 259373d
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 10 deletions.
10 changes: 10 additions & 0 deletions packages/flutter_tools/lib/src/build_system/targets/web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ const String kHasWebPlugins = 'HasWebPlugins';
/// Valid values are O1 (lowest, profile default) to O4 (highest, release default).
const String kDart2jsOptimization = 'Dart2jsOptimization';

/// If `--dump-info` should be passed to dart2js.
const String kDart2jsDumpInfo = 'Dart2jsDumpInfo';

// If `--no-frequency-based-minification` should be based to dart2js
const String kDart2jsNoFrequencyBasedMinification = 'Dart2jsNoFrequencyBasedMinification';

/// Whether to disable dynamic generation code to satisfy csp policies.
const String kCspMode = 'cspMode';

Expand Down Expand Up @@ -217,13 +223,17 @@ class Dart2JSTarget extends Target {
}

final String? dart2jsOptimization = environment.defines[kDart2jsOptimization];
final bool dumpInfo = environment.defines[kDart2jsDumpInfo] == 'true';
final bool noFrequencyBasedMinification = environment.defines[kDart2jsNoFrequencyBasedMinification] == 'true';
final File outputJSFile = environment.buildDir.childFile('main.dart.js');
final bool csp = environment.defines[kCspMode] == 'true';

final ProcessResult javaScriptResult = await environment.processManager.run(<String>[
...sharedCommandOptions,
if (dart2jsOptimization != null) '-$dart2jsOptimization' else '-O4',
if (buildMode == BuildMode.profile) '--no-minify',
if (dumpInfo) '--dump-info',
if (noFrequencyBasedMinification) '--no-frequency-based-minification',
if (csp) '--csp',
'-o',
outputJSFile.path,
Expand Down
14 changes: 12 additions & 2 deletions packages/flutter_tools/lib/src/commands/build_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ class BuildWebCommand extends BuildSubCommand {
help: 'Sets the optimization level used for Dart compilation to JavaScript. '
'Valid values range from O0 to O4.'
);
argParser.addFlag('dump-info', negatable: false,
help: 'Passes "--dump-info" to the Javascript compiler which generates '
'information about the generated code is a .js.info.json file.'
);
argParser.addFlag('no-frequency-based-minification', negatable: false,
help: 'Disables the frequency based minifier. '
'Useful for comparing the output between builds.'
);
}

final FileSystem _fileSystem;
Expand Down Expand Up @@ -132,9 +140,11 @@ class BuildWebCommand extends BuildSubCommand {
stringArgDeprecated('pwa-strategy')!,
boolArgDeprecated('source-maps'),
boolArgDeprecated('native-null-assertions'),
baseHref,
stringArgDeprecated('dart2js-optimization'),
baseHref: baseHref,
dart2jsOptimization: stringArgDeprecated('dart2js-optimization'),
outputDirectoryPath: outputDirectoryPath,
dumpInfo: boolArgDeprecated('dump-info'),
noFrequencyBasedMinification: boolArgDeprecated('no-frequency-based-minification'),
);
return FlutterCommandResult.success();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,6 @@ class ResidentWebRunner extends ResidentRunner {
kNoneWorker,
true,
debuggingOptions.nativeNullAssertions,
null,
null,
);
}
await device!.device!.startApp(
Expand Down Expand Up @@ -372,8 +370,7 @@ class ResidentWebRunner extends ResidentRunner {
kNoneWorker,
true,
debuggingOptions.nativeNullAssertions,
kBaseHref,
null,
baseHref: kBaseHref,
);
} on ToolExit {
return OperationResult(1, 'Failed to recompile application.');
Expand Down
12 changes: 8 additions & 4 deletions packages/flutter_tools/lib/src/web/compile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ Future<void> buildWeb(
bool csp,
String serviceWorkerStrategy,
bool sourceMaps,
bool nativeNullAssertions,
String? baseHref,
bool nativeNullAssertions, {
String? dart2jsOptimization,
{String? outputDirectoryPath}
) async {
String? baseHref,
bool dumpInfo = false,
bool noFrequencyBasedMinification = false,
String? outputDirectoryPath,
}) async {
final bool hasWebPlugins = (await findPlugins(flutterProject))
.any((Plugin p) => p.platforms.containsKey(WebPlugin.kConfigKey));
final Directory outputDirectory = outputDirectoryPath == null
Expand Down Expand Up @@ -66,6 +68,8 @@ Future<void> buildWeb(
kServiceWorkerStrategy: serviceWorkerStrategy,
if (dart2jsOptimization != null)
kDart2jsOptimization: dart2jsOptimization,
kDart2jsDumpInfo: dumpInfo.toString(),
kDart2jsNoFrequencyBasedMinification: noFrequencyBasedMinification.toString(),
...buildInfo.toBuildSystemEnvironment(),
},
artifacts: globals.artifacts!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ void main() {
'SourceMaps': 'false',
'NativeNullAssertions': 'true',
'ServiceWorkerStrategy': 'offline-first',
'Dart2jsDumpInfo': 'false',
'Dart2jsNoFrequencyBasedMinification': 'false',
'Dart2jsOptimization': 'O3',
'BuildMode': 'release',
'DartDefines': 'Zm9vPWE=,RkxVVFRFUl9XRUJfQVVUT19ERVRFQ1Q9dHJ1ZQ==',
Expand Down Expand Up @@ -179,6 +181,8 @@ void main() {
'SourceMaps': 'false',
'NativeNullAssertions': 'true',
'ServiceWorkerStrategy': 'offline-first',
'Dart2jsDumpInfo': 'false',
'Dart2jsNoFrequencyBasedMinification': 'false',
'BuildMode': 'release',
'DartDefines': 'RkxVVFRFUl9XRUJfQVVUT19ERVRFQ1Q9dHJ1ZQ==',
'DartObfuscation': 'false',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,74 @@ void main() {
ProcessManager: () => processManager,
}));

test('Dart2JSTarget calls dart2js with expected args with dump-info', () => testbed.run(() async {
environment.defines[kBuildMode] = 'profile';
environment.defines[kDart2jsDumpInfo] = 'true';
processManager.addCommand(FakeCommand(
command: <String>[
...kDart2jsLinuxArgs,
'-Ddart.vm.profile=true',
'--no-source-maps',
'-o',
environment.buildDir.childFile('app.dill').absolute.path,
'--packages=.dart_tool/package_config.json',
'--cfe-only',
environment.buildDir.childFile('main.dart').absolute.path,
]
));
processManager.addCommand(FakeCommand(
command: <String>[
...kDart2jsLinuxArgs,
'-Ddart.vm.profile=true',
'--no-source-maps',
'-O4',
'--no-minify',
'--dump-info',
'-o',
environment.buildDir.childFile('main.dart.js').absolute.path,
environment.buildDir.childFile('app.dill').absolute.path,
]
));

await const Dart2JSTarget().build(environment);
}, overrides: <Type, Generator>{
ProcessManager: () => processManager,
}));

test('Dart2JSTarget calls dart2js with expected args with no-frequency-based-minification', () => testbed.run(() async {
environment.defines[kBuildMode] = 'profile';
environment.defines[kDart2jsNoFrequencyBasedMinification] = 'true';
processManager.addCommand(FakeCommand(
command: <String>[
...kDart2jsLinuxArgs,
'-Ddart.vm.profile=true',
'--no-source-maps',
'-o',
environment.buildDir.childFile('app.dill').absolute.path,
'--packages=.dart_tool/package_config.json',
'--cfe-only',
environment.buildDir.childFile('main.dart').absolute.path,
]
));
processManager.addCommand(FakeCommand(
command: <String>[
...kDart2jsLinuxArgs,
'-Ddart.vm.profile=true',
'--no-source-maps',
'-O4',
'--no-minify',
'--no-frequency-based-minification',
'-o',
environment.buildDir.childFile('main.dart.js').absolute.path,
environment.buildDir.childFile('app.dill').absolute.path,
]
));

await const Dart2JSTarget().build(environment);
}, overrides: <Type, Generator>{
ProcessManager: () => processManager,
}));

test('Generated service worker is empty with none-strategy', () {
final String result = generateServiceWorker(<String, String>{'/foo': 'abcd'}, <String>[], serviceWorkerStrategy: ServiceWorkerStrategy.none);

Expand Down

0 comments on commit 259373d

Please sign in to comment.