Skip to content

Commit ca6455e

Browse files
authored
feat: allow passing down resolved config to vite's createServer (#20932)
1 parent 0e173d8 commit ca6455e

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

packages/vite/src/node/config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ import { getAdditionalAllowedHosts } from './server/middlewares/hostCheck'
106106

107107
const debug = createDebugger('vite:config', { depth: 10 })
108108
const promisifiedRealpath = promisify(fs.realpath)
109+
const SYMBOL_RESOLVED_CONFIG = Symbol('vite:resolved-config')
109110

110111
export interface ConfigEnv {
111112
/**
@@ -631,6 +632,8 @@ export interface ResolvedConfig
631632
safeModulePaths: Set<string>
632633
/** @internal */
633634
additionalAllowedHosts: string[]
635+
/** @internal */
636+
[SYMBOL_RESOLVED_CONFIG]: true
634637
} & PluginHookUtils
635638
> {}
636639

@@ -1034,6 +1037,15 @@ function resolveDepOptimizationOptions(
10341037
)
10351038
}
10361039

1040+
export function isResolvedConfig(
1041+
inlineConfig: InlineConfig | ResolvedConfig,
1042+
): inlineConfig is ResolvedConfig {
1043+
return (
1044+
SYMBOL_RESOLVED_CONFIG in inlineConfig &&
1045+
inlineConfig[SYMBOL_RESOLVED_CONFIG]
1046+
)
1047+
}
1048+
10371049
export async function resolveConfig(
10381050
inlineConfig: InlineConfig,
10391051
command: 'build' | 'serve',
@@ -1552,6 +1564,7 @@ export async function resolveConfig(
15521564
),
15531565
safeModulePaths: new Set<string>(),
15541566
additionalAllowedHosts: getAdditionalAllowedHosts(server, preview),
1567+
[SYMBOL_RESOLVED_CONFIG]: true,
15551568
}
15561569
resolved = {
15571570
...config,

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
setClientErrorHandler,
2424
} from '../http'
2525
import type { InlineConfig, ResolvedConfig } from '../config'
26-
import { resolveConfig } from '../config'
26+
import { isResolvedConfig, resolveConfig } from '../config'
2727
import {
2828
diffDnsOrderChange,
2929
getServerUrlByHost,
@@ -101,6 +101,8 @@ import type { DevEnvironment } from './environment'
101101
import { hostCheckMiddleware } from './middlewares/hostCheck'
102102
import { rejectInvalidRequestMiddleware } from './middlewares/rejectInvalidRequest'
103103

104+
const usedConfigs = new WeakSet<ResolvedConfig>()
105+
104106
export interface ServerOptions extends CommonServerOptions {
105107
/**
106108
* Configure HMR-specific options (port, host, path & protocol)
@@ -427,19 +429,33 @@ export interface ResolvedServerUrls {
427429
}
428430

429431
export function createServer(
430-
inlineConfig: InlineConfig = {},
432+
inlineConfig: InlineConfig | ResolvedConfig = {},
431433
): Promise<ViteDevServer> {
432434
return _createServer(inlineConfig, { listen: true })
433435
}
434436

435437
export async function _createServer(
436-
inlineConfig: InlineConfig = {},
438+
inlineConfig: InlineConfig | ResolvedConfig = {},
437439
options: {
438440
listen: boolean
439441
previousEnvironments?: Record<string, DevEnvironment>
440442
},
441443
): Promise<ViteDevServer> {
442-
const config = await resolveConfig(inlineConfig, 'serve')
444+
const config = isResolvedConfig(inlineConfig)
445+
? inlineConfig
446+
: await resolveConfig(inlineConfig, 'serve')
447+
448+
if (usedConfigs.has(config)) {
449+
throw new Error(`There is already a server associated with the config.`)
450+
}
451+
452+
if (config.command !== 'serve') {
453+
throw new Error(
454+
`Config was resolved for a "build", expected a "serve" command.`,
455+
)
456+
}
457+
458+
usedConfigs.add(config)
443459

444460
const initPublicFilesPromise = initPublicFiles(config)
445461

0 commit comments

Comments
 (0)