From b1b816a6bb470637fac04b06a7637b6c3d053455 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Tue, 22 Aug 2023 16:04:28 +0800 Subject: [PATCH] fix: if host is specified check whether it is valid (#14013) --- packages/vite/src/node/cli.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/cli.ts b/packages/vite/src/node/cli.ts index df858f403..f6d3c9b6d 100644 --- a/packages/vite/src/node/cli.ts +++ b/packages/vite/src/node/cli.ts @@ -102,6 +102,16 @@ function cleanOptions( return ret } +/** + * host may be a number (like 0), should convert to string + */ +const convertHost = (v: any) => { + if (typeof v === 'number') { + return String(v) + } + return v +} + cli .option('-c, --config ', `[string] use specified config file`) .option('--base ', `[string] public base path (default: /)`) @@ -116,7 +126,7 @@ cli .command('[root]', 'start dev server') // default command .alias('serve') // the command is called 'serve' in Vite's API .alias('dev') // alias to align with the script name - .option('--host [host]', `[string] specify hostname`) + .option('--host [host]', `[string] specify hostname`, { type: [convertHost] }) .option('--port ', `[number] specify port`) .option('--https', `[boolean] use TLS + HTTP/2`) .option('--open [path]', `[boolean | string] open browser on startup`) @@ -306,7 +316,7 @@ cli // preview cli .command('preview [root]', 'locally preview production build') - .option('--host [host]', `[string] specify hostname`) + .option('--host [host]', `[string] specify hostname`, { type: [convertHost] }) .option('--port ', `[number] specify port`) .option('--strictPort', `[boolean] exit if specified port is already in use`) .option('--https', `[boolean] use TLS + HTTP/2`)