Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make properties of ResolvedServerOptions and ResolvedPreviewOptions required #18796

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>>