Skip to content

Commit 796c62b

Browse files
salemiranloyekevmooSydneyBao
authored
Web dev proxy (flutter#172175)
Adding a development Proxy and dedicated Web configuration File <!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> Flutter's current web development configuration relies on CLI arguments and does not have a development proxy. This PR adds a development proxy to flutter and a designated web_dev_config.yaml where web configuration settings are loaded from. Issues: flutter#170834 Document: [HERE](https://docs.google.com/document/d/1Ud9D3F0GxB5Ocoo5NnAy7PH5oo3qxvALxUlAXMCANJ0/edit?usp=sharing) ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md Co-authored-by: Sydney Bao <s.bao2115@gmail.com> Co-authored by: Salem Iranloye <salemiranloye@gmail.com> --------- Co-authored-by: Kevin Moore <kevmoo@google.com> Co-authored-by: Sydney Bao <sydneybao@google.com>
1 parent 9264b70 commit 796c62b

File tree

18 files changed

+1174
-266
lines changed

18 files changed

+1174
-266
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import '../ios/devices.dart';
2929
import '../resident_runner.dart';
3030
import '../runner/flutter_command.dart'
3131
show FlutterCommandCategory, FlutterCommandResult, FlutterOptions;
32+
import '../web/devfs_config.dart';
3233
import '../web/web_device.dart';
3334
import 'run.dart';
3435

@@ -303,8 +304,26 @@ class DriveCommand extends RunCommandBase {
303304
if (screenshot != null && !device.supportsScreenshot) {
304305
_logger.printError('Screenshot not supported for ${device.displayName}.');
305306
}
307+
final String? webPortArg = stringArg('web-port');
308+
final int? webPort = webPortArg != null ? int.tryParse(webPortArg) : null;
309+
310+
final WebDevServerConfig? fileConfig = (device is WebServerDevice || device is ChromiumDevice)
311+
? (await WebDevServerConfig.loadFromFile(fileSystem: globals.fs, logger: globals.logger))
312+
: null;
313+
314+
final HttpsConfig? httpsConfig = fileConfig?.https?.copyWith(
315+
certPath: stringArg('web-tls-cert-path'),
316+
certKeyPath: stringArg('web-tls-cert-key-path'),
317+
);
318+
319+
final WebDevServerConfig? webDevServerConfig = fileConfig?.copyWith(
320+
host: stringArg('web-hostname'),
321+
port: webPort,
322+
https: httpsConfig,
323+
headers: extractWebHeaders(),
324+
);
325+
final web = webDevServerConfig != null;
306326

307-
final bool web = device is WebServerDevice || device is ChromiumDevice;
308327
_flutterDriverFactory ??= FlutterDriverFactory(
309328
applicationPackageFactory: ApplicationPackageFactory.instance!,
310329
logger: _logger,
@@ -324,7 +343,9 @@ class DriveCommand extends RunCommandBase {
324343
);
325344
final DriverService driverService = _flutterDriverFactory!.createDriverService(web);
326345
final BuildInfo buildInfo = await getBuildInfo();
327-
final DebuggingOptions debuggingOptions = await createDebuggingOptions(web);
346+
final DebuggingOptions debuggingOptions = await createDebuggingOptions(
347+
webDevServerConfig: webDevServerConfig,
348+
);
328349
final File? applicationBinary = applicationBinaryPath == null
329350
? null
330351
: _fileSystem.file(applicationBinaryPath);

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

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import '../runner/flutter_command.dart';
2525
import '../runner/flutter_command_runner.dart';
2626
import '../tracing.dart';
2727
import '../web/compile.dart';
28+
import '../web/devfs_config.dart';
2829
import '../web/web_constants.dart';
2930
import '../web/web_runner.dart';
3031
import 'daemon.dart';
@@ -279,7 +280,7 @@ abstract class RunCommandBase extends FlutterCommand with DeviceBasedDevelopment
279280
/// Create a debugging options instance for the current `run` or `drive` invocation.
280281
@visibleForTesting
281282
@protected
282-
Future<DebuggingOptions> createDebuggingOptions(bool webMode) async {
283+
Future<DebuggingOptions> createDebuggingOptions({WebDevServerConfig? webDevServerConfig}) async {
283284
final BuildInfo buildInfo = await getBuildInfo();
284285
final int? webBrowserDebugPort =
285286
featureFlags.isWebEnabled && argResults!.wasParsed('web-browser-debug-port')
@@ -289,18 +290,10 @@ abstract class RunCommandBase extends FlutterCommand with DeviceBasedDevelopment
289290
? stringsArg(FlutterOptions.kWebBrowserFlag)
290291
: const <String>[];
291292

292-
final Map<String, String> webHeaders = featureFlags.isWebEnabled
293-
? extractWebHeaders()
294-
: const <String, String>{};
295-
296293
if (buildInfo.mode.isRelease) {
297294
return DebuggingOptions.disabled(
298295
buildInfo,
299296
dartEntrypointArgs: stringsArg('dart-entrypoint-args'),
300-
hostname: featureFlags.isWebEnabled ? stringArg('web-hostname') : '',
301-
port: featureFlags.isWebEnabled ? stringArg('web-port') : '',
302-
tlsCertPath: featureFlags.isWebEnabled ? stringArg('web-tls-cert-path') : null,
303-
tlsCertKeyPath: featureFlags.isWebEnabled ? stringArg('web-tls-cert-key-path') : null,
304297
webUseSseForDebugProxy:
305298
featureFlags.isWebEnabled && stringArg('web-server-debug-protocol') == 'sse',
306299
webUseSseForDebugBackend:
@@ -312,7 +305,6 @@ abstract class RunCommandBase extends FlutterCommand with DeviceBasedDevelopment
312305
webRunHeadless: featureFlags.isWebEnabled && boolArg('web-run-headless'),
313306
webBrowserDebugPort: webBrowserDebugPort,
314307
webBrowserFlags: webBrowserFlags,
315-
webHeaders: webHeaders,
316308
webRenderer: webRenderer,
317309
webUseWasm: useWasm,
318310
enableImpeller: enableImpeller,
@@ -323,6 +315,7 @@ abstract class RunCommandBase extends FlutterCommand with DeviceBasedDevelopment
323315
enableEmbedderApi: enableEmbedderApi,
324316
usingCISystem: usingCISystem,
325317
debugLogsDirectoryPath: debugLogsDirectoryPath,
318+
webDevServerConfig: webDevServerConfig,
326319
);
327320
} else {
328321
return DebuggingOptions.enabled(
@@ -354,10 +347,6 @@ abstract class RunCommandBase extends FlutterCommand with DeviceBasedDevelopment
354347
ddsPort: ddsPort,
355348
devToolsServerAddress: devToolsServerAddress,
356349
verboseSystemLogs: boolArg('verbose-system-logs'),
357-
hostname: featureFlags.isWebEnabled ? stringArg('web-hostname') : '',
358-
port: featureFlags.isWebEnabled ? stringArg('web-port') : '',
359-
tlsCertPath: featureFlags.isWebEnabled ? stringArg('web-tls-cert-path') : null,
360-
tlsCertKeyPath: featureFlags.isWebEnabled ? stringArg('web-tls-cert-key-path') : null,
361350
webUseSseForDebugProxy:
362351
featureFlags.isWebEnabled && stringArg('web-server-debug-protocol') == 'sse',
363352
webUseSseForDebugBackend:
@@ -372,7 +361,6 @@ abstract class RunCommandBase extends FlutterCommand with DeviceBasedDevelopment
372361
webEnableExpressionEvaluation:
373362
featureFlags.isWebEnabled && boolArg('web-enable-expression-evaluation'),
374363
webLaunchUrl: featureFlags.isWebEnabled ? stringArg('web-launch-url') : null,
375-
webHeaders: webHeaders,
376364
webRenderer: webRenderer,
377365
webUseWasm: useWasm,
378366
vmserviceOutFile: stringArg('vmservice-out-file'),
@@ -393,6 +381,7 @@ abstract class RunCommandBase extends FlutterCommand with DeviceBasedDevelopment
393381
enableDevTools: boolArg(FlutterCommand.kEnableDevTools),
394382
ipv6: boolArg(FlutterCommand.ipv6Flag),
395383
printDtd: boolArg(FlutterGlobalOptions.kPrintDtd, global: true),
384+
webDevServerConfig: webDevServerConfig,
396385
);
397386
}
398387
}
@@ -504,7 +493,37 @@ class RunCommand extends RunCommandBase {
504493
String get category => FlutterCommandCategory.project;
505494

506495
List<Device>? devices;
507-
var webMode = false;
496+
Future<WebDevServerConfig?> getWebDevServerConfig() async {
497+
// Only support "web mode" with a single web device due to resident runner
498+
// refactoring required otherwise.
499+
500+
if (featureFlags.isWebEnabled &&
501+
devices != null &&
502+
devices!.length == 1 &&
503+
await devices!.single.targetPlatform == TargetPlatform.web_javascript) {
504+
final String? webPortArg = stringArg('web-port');
505+
final int? webPort = webPortArg != null ? int.tryParse(webPortArg) : null;
506+
507+
final WebDevServerConfig fileConfig = await WebDevServerConfig.loadFromFile(
508+
fileSystem: globals.fs,
509+
logger: globals.logger,
510+
);
511+
512+
final HttpsConfig? httpsConfig = fileConfig.https?.copyWith(
513+
certPath: stringArg('web-tls-cert-path'),
514+
certKeyPath: stringArg('web-tls-cert-key-path'),
515+
);
516+
517+
final WebDevServerConfig webDevServerConfig = fileConfig.copyWith(
518+
host: stringArg('web-hostname'),
519+
port: webPort,
520+
https: httpsConfig,
521+
headers: extractWebHeaders(),
522+
);
523+
return webDevServerConfig;
524+
}
525+
return null;
526+
}
508527

509528
String? get userIdentifier => stringArg(FlutterOptions.kDeviceUser);
510529

@@ -655,6 +674,8 @@ class RunCommand extends RunCommandBase {
655674
if (devices == null) {
656675
throwToolExit(null);
657676
}
677+
final WebDevServerConfig? webDevServerConfig = await getWebDevServerConfig();
678+
final webMode = webDevServerConfig != null;
658679
if (globals.deviceManager!.hasSpecifiedAllDevices && runningWithPrebuiltApplication) {
659680
throwToolExit(
660681
'Using "-d all" with "--${FlutterOptions.kUseApplicationBinary}" is not supported',
@@ -672,13 +693,6 @@ class RunCommand extends RunCommandBase {
672693
_deviceDeprecationBehavior = DeprecationBehavior.exit;
673694
}
674695

675-
// Only support "web mode" with a single web device due to resident runner
676-
// refactoring required otherwise.
677-
webMode =
678-
featureFlags.isWebEnabled &&
679-
devices!.length == 1 &&
680-
await devices!.single.targetPlatform == TargetPlatform.web_javascript;
681-
682696
if (useWasm && !webMode) {
683697
throwToolExit('--wasm is only supported on the web platform');
684698
}
@@ -707,11 +721,17 @@ class RunCommand extends RunCommandBase {
707721
required String? applicationBinaryPath,
708722
required FlutterProject flutterProject,
709723
}) async {
724+
final WebDevServerConfig? webDevServerConfig = await getWebDevServerConfig();
725+
final webMode = webDevServerConfig != null;
726+
final DebuggingOptions debuggingOptions = await createDebuggingOptions(
727+
webDevServerConfig: webDevServerConfig,
728+
);
729+
710730
if (hotMode && !webMode) {
711731
return HotRunner(
712732
flutterDevices,
713733
target: targetFile,
714-
debuggingOptions: await createDebuggingOptions(webMode),
734+
debuggingOptions: debuggingOptions,
715735
benchmarkMode: boolArg('benchmark'),
716736
applicationBinary: applicationBinaryPath == null
717737
? null
@@ -727,7 +747,7 @@ class RunCommand extends RunCommandBase {
727747
flutterDevices.single,
728748
target: targetFile,
729749
flutterProject: flutterProject,
730-
debuggingOptions: await createDebuggingOptions(webMode),
750+
debuggingOptions: debuggingOptions,
731751
stayResident: stayResident,
732752
fileSystem: globals.fs,
733753
analytics: globals.analytics,
@@ -741,7 +761,7 @@ class RunCommand extends RunCommandBase {
741761
return ColdRunner(
742762
flutterDevices,
743763
target: targetFile,
744-
debuggingOptions: await createDebuggingOptions(webMode),
764+
debuggingOptions: debuggingOptions,
745765
traceStartup: traceStartup,
746766
awaitFirstFrameWhenTracing: awaitFirstFrameWhenTracing,
747767
applicationBinary: applicationBinaryPath == null
@@ -763,20 +783,25 @@ class RunCommand extends RunCommandBase {
763783
// debug mode.
764784
final bool hotMode = shouldUseHotMode(buildInfo);
765785
final String? applicationBinaryPath = stringArg(FlutterOptions.kUseApplicationBinary);
786+
final WebDevServerConfig? webDevServerConfig = await getWebDevServerConfig();
766787

767788
if (outputMachineFormat) {
768789
if (devices!.length > 1) {
769790
throwToolExit('"--machine" does not support "-d all".');
770791
}
771792
final Daemon daemon = createMachineDaemon();
772793
late AppInstance app;
794+
795+
final DebuggingOptions debuggingOptions = await createDebuggingOptions(
796+
webDevServerConfig: webDevServerConfig,
797+
);
773798
try {
774799
app = await daemon.appDomain.startApp(
775800
devices!.first,
776801
globals.fs.currentDirectory.path,
777802
targetFile,
778803
route,
779-
await createDebuggingOptions(webMode),
804+
debuggingOptions,
780805
hotMode,
781806
applicationBinary: applicationBinaryPath == null
782807
? null

0 commit comments

Comments
 (0)