From 51a5569e66bd7f0de79ac14b9e902d1382ccd0aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Thu, 28 Nov 2024 00:25:13 +0900 Subject: [PATCH] refactor: make properties of ResolvedServerOptions and ResolvedPreviewOptions required (#18796) --- packages/vite/src/node/__tests__/config.spec.ts | 2 +- packages/vite/src/node/preview.ts | 12 +++++++----- packages/vite/src/node/server/index.ts | 16 +++++++++++++++- packages/vite/src/node/typeUtils.ts | 3 +++ 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/packages/vite/src/node/__tests__/config.spec.ts b/packages/vite/src/node/__tests__/config.spec.ts index 9fbbdd61f9ac7d..b1f1adc1179633 100644 --- a/packages/vite/src/node/__tests__/config.spec.ts +++ b/packages/vite/src/node/__tests__/config.spec.ts @@ -259,7 +259,7 @@ describe('preview config', () => { expect(await resolveConfig(config, 'serve')).toMatchObject({ preview: { ...serverConfig(), - port: undefined, + port: 4173, }, }) }) diff --git a/packages/vite/src/node/preview.ts b/packages/vite/src/node/preview.ts index 2ff3984515a292..a50cc2a6609a03 100644 --- a/packages/vite/src/node/preview.ts +++ b/packages/vite/src/node/preview.ts @@ -34,12 +34,15 @@ import { import { printServerUrls } from './logger' import { bindCLIShortcuts } from './shortcuts' import type { BindCLIShortcutsOptions } from './shortcuts' -import { configDefaults, resolveConfig } from './config' +import { resolveConfig } from './config' import type { InlineConfig, ResolvedConfig } from './config' +import { DEFAULT_PREVIEW_PORT } from './constants' +import type { RequiredExceptFor } from './typeUtils' export interface PreviewOptions extends CommonServerOptions {} -export interface ResolvedPreviewOptions extends PreviewOptions {} +export interface ResolvedPreviewOptions + extends RequiredExceptFor {} export function resolvePreviewOptions( preview: PreviewOptions | undefined, @@ -49,7 +52,7 @@ export function resolvePreviewOptions( // except for the port to enable having both the dev and preview servers running // at the same time without extra configuration return { - port: preview?.port, + port: preview?.port ?? DEFAULT_PREVIEW_PORT, strictPort: preview?.strictPort ?? server.strictPort, host: preview?.host ?? server.host, https: preview?.https ?? server.https, @@ -243,10 +246,9 @@ export async function preview( } const hostname = await resolveHostname(options.host) - const port = options.port ?? configDefaults.preview.port await httpServerStart(httpServer, { - port, + port: options.port, strictPort: options.strictPort, host: hostname.host, logger, diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 772b163a745eb7..13912a3f13f26f 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -56,6 +56,7 @@ import { } from '../watch' import { initPublicFiles } from '../publicDir' import { getEnvFilesForMode } from '../env' +import type { RequiredExceptFor } from '../typeUtils' import type { PluginContainer } from './pluginContainer' import { ERR_CLOSED_SERVER, createPluginContainer } from './pluginContainer' import type { WebSocketServer } from './ws' @@ -180,7 +181,20 @@ export interface ServerOptions extends CommonServerOptions { } export interface ResolvedServerOptions - extends Omit { + extends Omit< + RequiredExceptFor< + ServerOptions, + | 'host' + | 'https' + | 'proxy' + | 'hmr' + | 'ws' + | 'watch' + | 'origin' + | 'hotUpdateEnvironments' + >, + 'fs' | 'middlewareMode' | 'sourcemapIgnoreList' + > { fs: Required middlewareMode: NonNullable sourcemapIgnoreList: Exclude< diff --git a/packages/vite/src/node/typeUtils.ts b/packages/vite/src/node/typeUtils.ts index ece36fe9c9eb9a..09f62cf4b7f9f1 100644 --- a/packages/vite/src/node/typeUtils.ts +++ b/packages/vite/src/node/typeUtils.ts @@ -20,3 +20,6 @@ export type GetHookContextMap = { type RollupPluginHooksContext = GetHookContextMap export type RollupPluginHooks = NonNeverKeys + +export type RequiredExceptFor = Pick & + Required>