Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cb3150f

Browse files
authored
[tool] Add --no-minify flag to JS Compiler (#169102)
Also hides (behind verbose) dump info and frequency minify flags
1 parent e78b434 commit cb3150f

4 files changed

Lines changed: 75 additions & 6 deletions

File tree

packages/flutter_tools/lib/src/commands/build_web.dart

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,22 @@ class BuildWebCommand extends BuildSubCommand {
9696
help:
9797
'Passes "--dump-info" to the Javascript compiler which generates '
9898
'information about the generated code in main.dart.js.info.json.',
99+
hide: !verboseHelp,
100+
);
101+
argParser.addFlag(
102+
'minify',
103+
help:
104+
'Generate minified output. '
105+
'If not explicitly set, uses the compilation mode (debug, profile, release).',
106+
hide: !verboseHelp,
99107
);
100108
argParser.addFlag(
101109
'no-frequency-based-minification',
102110
negatable: false,
103111
help:
104112
'Disables the frequency based minifier. '
105113
'Useful for comparing the output between builds.',
114+
hide: !verboseHelp,
106115
);
107116

108117
//
@@ -166,6 +175,7 @@ class BuildWebCommand extends BuildSubCommand {
166175
);
167176

168177
final bool sourceMaps = boolArg('source-maps');
178+
final bool minify = boolArg('minify');
169179

170180
final List<WebCompilerConfig> compilerConfigs;
171181

@@ -187,21 +197,23 @@ class BuildWebCommand extends BuildSubCommand {
187197
),
188198
JsCompilerConfig(
189199
csp: boolArg('csp'),
190-
optimizationLevel: jsOptimizationLevel,
191200
dumpInfo: boolArg('dump-info'),
201+
minify: minify,
192202
nativeNullAssertions: boolArg('native-null-assertions'),
193203
noFrequencyBasedMinification: boolArg('no-frequency-based-minification'),
204+
optimizationLevel: jsOptimizationLevel,
194205
sourceMaps: sourceMaps,
195206
),
196207
];
197208
} else {
198209
compilerConfigs = <WebCompilerConfig>[
199210
JsCompilerConfig(
200211
csp: boolArg('csp'),
201-
optimizationLevel: jsOptimizationLevel,
202212
dumpInfo: boolArg('dump-info'),
213+
minify: minify,
203214
nativeNullAssertions: boolArg('native-null-assertions'),
204215
noFrequencyBasedMinification: boolArg('no-frequency-based-minification'),
216+
optimizationLevel: jsOptimizationLevel,
205217
sourceMaps: sourceMaps,
206218
renderer: webRenderer,
207219
),

packages/flutter_tools/lib/src/web/compiler_config.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class JsCompilerConfig extends WebCompilerConfig {
6060
super.optimizationLevel,
6161
this.noFrequencyBasedMinification = false,
6262
super.sourceMaps = true,
63+
this.minify,
6364
super.renderer = WebRendererMode.defaultForJs,
6465
});
6566

@@ -75,6 +76,11 @@ class JsCompilerConfig extends WebCompilerConfig {
7576
/// If `--dump-info` should be passed to the compiler.
7677
final bool dumpInfo;
7778

79+
/// If minification should be used in the JS compiler.
80+
///
81+
/// If `null`, minifies in release mode only.
82+
final bool? minify;
83+
7884
/// Whether native null assertions are enabled.
7985
final bool nativeNullAssertions;
8086

@@ -107,7 +113,7 @@ class JsCompilerConfig extends WebCompilerConfig {
107113
///
108114
/// Includes the contents of [toSharedCommandOptions].
109115
List<String> toCommandOptions(BuildMode buildMode) => <String>[
110-
if (buildMode != BuildMode.release) '--no-minify',
116+
if (minify ?? buildMode == BuildMode.release) '--minify' else '--no-minify',
111117
...toSharedCommandOptions(buildMode),
112118
'-O${optimizationLevelForBuildMode(buildMode)}',
113119
if (dumpInfo) '--stage=dump-info-all',
@@ -123,6 +129,7 @@ class JsCompilerConfig extends WebCompilerConfig {
123129
'dumpInfo': dumpInfo,
124130
'nativeNullAssertions': nativeNullAssertions,
125131
'noFrequencyBasedMinification': noFrequencyBasedMinification,
132+
'minify': minify,
126133
'sourceMaps': sourceMaps,
127134
};
128135
return jsonEncode(settings);

packages/flutter_tools/test/commands.shard/hermetic/build_web_test.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,11 @@ void main() {
547547

548548
void expectVisible(String option) {
549549
expect(command.argParser.options.keys, contains(option));
550-
expect(command.argParser.options[option]!.hide, isFalse);
550+
expect(
551+
command.argParser.options[option]!.hide,
552+
isFalse,
553+
reason: 'Expecting `$option` to be visible',
554+
);
551555
expect(command.usage, contains(option));
552556
}
553557

@@ -557,8 +561,6 @@ void main() {
557561
expectVisible('source-maps');
558562
expectVisible('csp');
559563
expectVisible('dart2js-optimization');
560-
expectVisible('dump-info');
561-
expectVisible('no-frequency-based-minification');
562564
expectVisible('wasm');
563565
expectVisible('strip-wasm');
564566
expectVisible('base-href');

packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,47 @@ name: foo
554554
}, overrides: <Type, Generator>{ProcessManager: () => processManager}),
555555
);
556556

557+
test(
558+
'Dart2JSTarget calls dart2js with expected args with minify false',
559+
() => testbed.run(() async {
560+
environment.defines[kBuildMode] = 'release';
561+
processManager.addCommand(
562+
FakeCommand(
563+
command: <String>[
564+
..._kDart2jsLinuxArgs,
565+
'-Ddart.vm.product=true',
566+
'-DFLUTTER_WEB_USE_SKIA=true',
567+
'-DFLUTTER_WEB_USE_SKWASM=false',
568+
'-DFLUTTER_WEB_CANVASKIT_URL=https://www.gstatic.com/flutter-canvaskit/abcdefghijklmnopqrstuvwxyz/',
569+
'-o',
570+
environment.buildDir.childFile('app.dill').absolute.path,
571+
'--packages=/.dart_tool/package_config.json',
572+
'--cfe-only',
573+
environment.buildDir.childFile('main.dart').absolute.path,
574+
],
575+
),
576+
);
577+
processManager.addCommand(
578+
FakeCommand(
579+
command: <String>[
580+
..._kDart2jsLinuxArgs,
581+
'-Ddart.vm.product=true',
582+
'-DFLUTTER_WEB_USE_SKIA=true',
583+
'-DFLUTTER_WEB_USE_SKWASM=false',
584+
'-DFLUTTER_WEB_CANVASKIT_URL=https://www.gstatic.com/flutter-canvaskit/abcdefghijklmnopqrstuvwxyz/',
585+
'--no-minify',
586+
'-O4',
587+
'-o',
588+
environment.buildDir.childFile('main.dart.js').absolute.path,
589+
environment.buildDir.childFile('app.dill').absolute.path,
590+
],
591+
),
592+
);
593+
594+
await Dart2JSTarget(const JsCompilerConfig(minify: false)).build(environment);
595+
}, overrides: <Type, Generator>{ProcessManager: () => processManager}),
596+
);
597+
557598
test(
558599
'Dart2JSTarget ignores frontend server starter path option when calling dart2js',
559600
() => testbed.run(() async {
@@ -716,6 +757,7 @@ name: foo
716757
'-DFLUTTER_WEB_USE_SKIA=true',
717758
'-DFLUTTER_WEB_USE_SKWASM=false',
718759
'-DFLUTTER_WEB_CANVASKIT_URL=https://www.gstatic.com/flutter-canvaskit/abcdefghijklmnopqrstuvwxyz/',
760+
'--minify',
719761
'--no-source-maps',
720762
'-O4',
721763
'-o',
@@ -759,6 +801,7 @@ name: foo
759801
'-DFLUTTER_WEB_USE_SKIA=true',
760802
'-DFLUTTER_WEB_USE_SKWASM=false',
761803
'-DFLUTTER_WEB_CANVASKIT_URL=https://www.gstatic.com/flutter-canvaskit/abcdefghijklmnopqrstuvwxyz/',
804+
'--minify',
762805
'--native-null-assertions',
763806
'--no-source-maps',
764807
'-O4',
@@ -804,6 +847,7 @@ name: foo
804847
'-DFLUTTER_WEB_USE_SKIA=true',
805848
'-DFLUTTER_WEB_USE_SKWASM=false',
806849
'-DFLUTTER_WEB_CANVASKIT_URL=https://www.gstatic.com/flutter-canvaskit/abcdefghijklmnopqrstuvwxyz/',
850+
'--minify',
807851
'--no-source-maps',
808852
'-O3',
809853
'-o',
@@ -851,6 +895,7 @@ name: foo
851895
'-DFLUTTER_WEB_USE_SKIA=true',
852896
'-DFLUTTER_WEB_USE_SKWASM=false',
853897
'-DFLUTTER_WEB_CANVASKIT_URL=https://www.gstatic.com/flutter-canvaskit/abcdefghijklmnopqrstuvwxyz/',
898+
'--minify',
854899
'--no-source-maps',
855900
'-O4',
856901
'-o',
@@ -909,6 +954,7 @@ name: foo
909954
'-DFLUTTER_WEB_USE_SKIA=true',
910955
'-DFLUTTER_WEB_USE_SKWASM=false',
911956
'-DFLUTTER_WEB_CANVASKIT_URL=https://www.gstatic.com/flutter-canvaskit/abcdefghijklmnopqrstuvwxyz/',
957+
'--minify',
912958
'--no-source-maps',
913959
'-O4',
914960
'-o',
@@ -951,6 +997,7 @@ name: foo
951997
'-DFLUTTER_WEB_USE_SKIA=true',
952998
'-DFLUTTER_WEB_USE_SKWASM=false',
953999
'-DFLUTTER_WEB_CANVASKIT_URL=https://www.gstatic.com/flutter-canvaskit/abcdefghijklmnopqrstuvwxyz/',
1000+
'--minify',
9541001
'-O4',
9551002
'-o',
9561003
environment.buildDir.childFile('main.dart.js').absolute.path,
@@ -1251,6 +1298,7 @@ name: foo
12511298
JsCompilerConfig(optimizationLevel: 0),
12521299
JsCompilerConfig(noFrequencyBasedMinification: true),
12531300
JsCompilerConfig(sourceMaps: false),
1301+
JsCompilerConfig(minify: false),
12541302

12551303
// All properties non-default
12561304
JsCompilerConfig(

0 commit comments

Comments
 (0)