Skip to content

Commit 51a5569

Browse files
authored
refactor: make properties of ResolvedServerOptions and ResolvedPreviewOptions required (#18796)
1 parent 47ec49f commit 51a5569

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

packages/vite/src/node/__tests__/config.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ describe('preview config', () => {
259259
expect(await resolveConfig(config, 'serve')).toMatchObject({
260260
preview: {
261261
...serverConfig(),
262-
port: undefined,
262+
port: 4173,
263263
},
264264
})
265265
})

packages/vite/src/node/preview.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,15 @@ import {
3434
import { printServerUrls } from './logger'
3535
import { bindCLIShortcuts } from './shortcuts'
3636
import type { BindCLIShortcutsOptions } from './shortcuts'
37-
import { configDefaults, resolveConfig } from './config'
37+
import { resolveConfig } from './config'
3838
import type { InlineConfig, ResolvedConfig } from './config'
39+
import { DEFAULT_PREVIEW_PORT } from './constants'
40+
import type { RequiredExceptFor } from './typeUtils'
3941

4042
export interface PreviewOptions extends CommonServerOptions {}
4143

42-
export interface ResolvedPreviewOptions extends PreviewOptions {}
44+
export interface ResolvedPreviewOptions
45+
extends RequiredExceptFor<PreviewOptions, 'host' | 'https' | 'proxy'> {}
4346

4447
export function resolvePreviewOptions(
4548
preview: PreviewOptions | undefined,
@@ -49,7 +52,7 @@ export function resolvePreviewOptions(
4952
// except for the port to enable having both the dev and preview servers running
5053
// at the same time without extra configuration
5154
return {
52-
port: preview?.port,
55+
port: preview?.port ?? DEFAULT_PREVIEW_PORT,
5356
strictPort: preview?.strictPort ?? server.strictPort,
5457
host: preview?.host ?? server.host,
5558
https: preview?.https ?? server.https,
@@ -243,10 +246,9 @@ export async function preview(
243246
}
244247

245248
const hostname = await resolveHostname(options.host)
246-
const port = options.port ?? configDefaults.preview.port
247249

248250
await httpServerStart(httpServer, {
249-
port,
251+
port: options.port,
250252
strictPort: options.strictPort,
251253
host: hostname.host,
252254
logger,

packages/vite/src/node/server/index.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import {
5656
} from '../watch'
5757
import { initPublicFiles } from '../publicDir'
5858
import { getEnvFilesForMode } from '../env'
59+
import type { RequiredExceptFor } from '../typeUtils'
5960
import type { PluginContainer } from './pluginContainer'
6061
import { ERR_CLOSED_SERVER, createPluginContainer } from './pluginContainer'
6162
import type { WebSocketServer } from './ws'
@@ -180,7 +181,20 @@ export interface ServerOptions extends CommonServerOptions {
180181
}
181182

182183
export interface ResolvedServerOptions
183-
extends Omit<ServerOptions, 'fs' | 'middlewareMode' | 'sourcemapIgnoreList'> {
184+
extends Omit<
185+
RequiredExceptFor<
186+
ServerOptions,
187+
| 'host'
188+
| 'https'
189+
| 'proxy'
190+
| 'hmr'
191+
| 'ws'
192+
| 'watch'
193+
| 'origin'
194+
| 'hotUpdateEnvironments'
195+
>,
196+
'fs' | 'middlewareMode' | 'sourcemapIgnoreList'
197+
> {
184198
fs: Required<FileSystemServeOptions>
185199
middlewareMode: NonNullable<ServerOptions['middlewareMode']>
186200
sourcemapIgnoreList: Exclude<

packages/vite/src/node/typeUtils.ts

+3
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ export type GetHookContextMap<Plugin> = {
2020

2121
type RollupPluginHooksContext = GetHookContextMap<RollupPlugin>
2222
export type RollupPluginHooks = NonNeverKeys<RollupPluginHooksContext>
23+
24+
export type RequiredExceptFor<T, K extends keyof T> = Pick<T, K> &
25+
Required<Omit<T, K>>

0 commit comments

Comments
 (0)