@@ -23,7 +23,7 @@ import {
23
23
setClientErrorHandler ,
24
24
} from '../http'
25
25
import type { InlineConfig , ResolvedConfig } from '../config'
26
- import { resolveConfig } from '../config'
26
+ import { isResolvedConfig , resolveConfig } from '../config'
27
27
import {
28
28
diffDnsOrderChange ,
29
29
getServerUrlByHost ,
@@ -101,6 +101,8 @@ import type { DevEnvironment } from './environment'
101
101
import { hostCheckMiddleware } from './middlewares/hostCheck'
102
102
import { rejectInvalidRequestMiddleware } from './middlewares/rejectInvalidRequest'
103
103
104
+ const usedConfigs = new WeakSet < ResolvedConfig > ( )
105
+
104
106
export interface ServerOptions extends CommonServerOptions {
105
107
/**
106
108
* Configure HMR-specific options (port, host, path & protocol)
@@ -420,19 +422,33 @@ export interface ResolvedServerUrls {
420
422
}
421
423
422
424
export function createServer (
423
- inlineConfig : InlineConfig = { } ,
425
+ inlineConfig : InlineConfig | ResolvedConfig = { } ,
424
426
) : Promise < ViteDevServer > {
425
427
return _createServer ( inlineConfig , { listen : true } )
426
428
}
427
429
428
430
export async function _createServer (
429
- inlineConfig : InlineConfig = { } ,
431
+ inlineConfig : InlineConfig | ResolvedConfig = { } ,
430
432
options : {
431
433
listen : boolean
432
434
previousEnvironments ?: Record < string , DevEnvironment >
433
435
} ,
434
436
) : Promise < ViteDevServer > {
435
- const config = await resolveConfig ( inlineConfig , 'serve' )
437
+ const config = isResolvedConfig ( inlineConfig )
438
+ ? inlineConfig
439
+ : await resolveConfig ( inlineConfig , 'serve' )
440
+
441
+ if ( usedConfigs . has ( config ) ) {
442
+ throw new Error ( `There is already a server associated with the config.` )
443
+ }
444
+
445
+ if ( config . command !== 'serve' ) {
446
+ throw new Error (
447
+ `Config was resolved for a "build", expected a "serve" command.` ,
448
+ )
449
+ }
450
+
451
+ usedConfigs . add ( config )
436
452
437
453
const initPublicFilesPromise = initPublicFiles ( config )
438
454
0 commit comments