@@ -23,7 +23,7 @@ import {
2323 setClientErrorHandler ,
2424} from '../http'
2525import type { InlineConfig , ResolvedConfig } from '../config'
26- import { resolveConfig } from '../config'
26+ import { isResolvedConfig , resolveConfig } from '../config'
2727import {
2828 diffDnsOrderChange ,
2929 getServerUrlByHost ,
@@ -101,6 +101,8 @@ import type { DevEnvironment } from './environment'
101101import { hostCheckMiddleware } from './middlewares/hostCheck'
102102import { rejectInvalidRequestMiddleware } from './middlewares/rejectInvalidRequest'
103103
104+ const usedConfigs = new WeakSet < ResolvedConfig > ( )
105+
104106export interface ServerOptions extends CommonServerOptions {
105107 /**
106108 * Configure HMR-specific options (port, host, path & protocol)
@@ -427,19 +429,33 @@ export interface ResolvedServerUrls {
427429}
428430
429431export function createServer (
430- inlineConfig : InlineConfig = { } ,
432+ inlineConfig : InlineConfig | ResolvedConfig = { } ,
431433) : Promise < ViteDevServer > {
432434 return _createServer ( inlineConfig , { listen : true } )
433435}
434436
435437export 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