From 46a7665b40cdd4276d4c3efb929f9223b094b3d4 Mon Sep 17 00:00:00 2001 From: jinzuoyu Date: Tue, 24 Dec 2024 18:10:14 +0800 Subject: [PATCH] feat: support host configuration in metro.config --- docs/Configuration.md | 6 ++++++ packages/metro-config/src/configTypes.flow.js | 1 + packages/metro-config/src/defaults/index.js | 1 + packages/metro-config/src/loadConfig.js | 4 ++++ packages/metro-config/types/configTypes.d.ts | 1 + packages/metro/src/commands/serve.js | 5 ++++- 6 files changed, 17 insertions(+), 1 deletion(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index 99de4f2aef..8e50152b7b 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -613,6 +613,12 @@ Type: `number` Which port to listen on. +#### `host` + +Type: `string` + +Which host to use. + #### `useGlobalHotkey` Type: `boolean` diff --git a/packages/metro-config/src/configTypes.flow.js b/packages/metro-config/src/configTypes.flow.js index ea3468880d..8d4d24882b 100644 --- a/packages/metro-config/src/configTypes.flow.js +++ b/packages/metro-config/src/configTypes.flow.js @@ -172,6 +172,7 @@ type ServerConfigT = { enhanceMiddleware: (Middleware, MetroServer) => Middleware | Server, forwardClientLogs: boolean, port: number, + host: string, rewriteRequestUrl: string => string, unstable_serverRoot: ?string, useGlobalHotkey: boolean, diff --git a/packages/metro-config/src/defaults/index.js b/packages/metro-config/src/defaults/index.js index 07192a8273..0d895075e2 100644 --- a/packages/metro-config/src/defaults/index.js +++ b/packages/metro-config/src/defaults/index.js @@ -76,6 +76,7 @@ const getDefaultValues = (projectRoot: ?string): ConfigT => ({ enhanceMiddleware: (middleware, _) => middleware, forwardClientLogs: true, port: 8081, + host: 'localhost', rewriteRequestUrl: url => url, unstable_serverRoot: null, useGlobalHotkey: true, diff --git a/packages/metro-config/src/loadConfig.js b/packages/metro-config/src/loadConfig.js index 2db3874056..4d4926358a 100644 --- a/packages/metro-config/src/loadConfig.js +++ b/packages/metro-config/src/loadConfig.js @@ -234,6 +234,10 @@ function overrideConfigWithArguments( output.server.port = Number(argv.port); } + if (argv.host != null) { + output.server.host = argv.host; + } + if (argv.projectRoot != null) { output.projectRoot = argv.projectRoot; } diff --git a/packages/metro-config/types/configTypes.d.ts b/packages/metro-config/types/configTypes.d.ts index c88a962305..0f29427125 100644 --- a/packages/metro-config/types/configTypes.d.ts +++ b/packages/metro-config/types/configTypes.d.ts @@ -169,6 +169,7 @@ export interface ServerConfigT { ) => Middleware | Server; forwardClientLogs: boolean; port: number; + host: string; rewriteRequestUrl: (url: string) => string; unstable_serverRoot: string | null; useGlobalHotkey: boolean; diff --git a/packages/metro/src/commands/serve.js b/packages/metro/src/commands/serve.js index c69a08e1a1..c726b7a2c2 100644 --- a/packages/metro/src/commands/serve.js +++ b/packages/metro/src/commands/serve.js @@ -107,7 +107,10 @@ module.exports = (): { resetCache: _resetCache, ...runServerOptions } = argv; - server = await MetroApi.runServer(config, runServerOptions); + server = await MetroApi.runServer(config, { + ...runServerOptions, + host: config.server.host, + }); restarting = false; }