Skip to content

Commit

Permalink
refactor: make properties of ResolvedServerOptions and ResolvedPrevie…
Browse files Browse the repository at this point in the history
…wOptions required (#18796)
  • Loading branch information
sapphi-red authored Nov 27, 2024
1 parent 47ec49f commit 51a5569
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/vite/src/node/__tests__/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ describe('preview config', () => {
expect(await resolveConfig(config, 'serve')).toMatchObject({
preview: {
...serverConfig(),
port: undefined,
port: 4173,
},
})
})
Expand Down
12 changes: 7 additions & 5 deletions packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PreviewOptions, 'host' | 'https' | 'proxy'> {}

export function resolvePreviewOptions(
preview: PreviewOptions | undefined,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 15 additions & 1 deletion packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -180,7 +181,20 @@ export interface ServerOptions extends CommonServerOptions {
}

export interface ResolvedServerOptions
extends Omit<ServerOptions, 'fs' | 'middlewareMode' | 'sourcemapIgnoreList'> {
extends Omit<
RequiredExceptFor<
ServerOptions,
| 'host'
| 'https'
| 'proxy'
| 'hmr'
| 'ws'
| 'watch'
| 'origin'
| 'hotUpdateEnvironments'
>,
'fs' | 'middlewareMode' | 'sourcemapIgnoreList'
> {
fs: Required<FileSystemServeOptions>
middlewareMode: NonNullable<ServerOptions['middlewareMode']>
sourcemapIgnoreList: Exclude<
Expand Down
3 changes: 3 additions & 0 deletions packages/vite/src/node/typeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ export type GetHookContextMap<Plugin> = {

type RollupPluginHooksContext = GetHookContextMap<RollupPlugin>
export type RollupPluginHooks = NonNeverKeys<RollupPluginHooksContext>

export type RequiredExceptFor<T, K extends keyof T> = Pick<T, K> &
Required<Omit<T, K>>

0 comments on commit 51a5569

Please sign in to comment.