createServer
Create a Vite development server instance programmatically. Type Signature:Parameters
InlineConfig
Configuration object that extends
UserConfig with additional properties:Returns
ViteDevServer
A promise that resolves to a ViteDevServer instance
Example Usage
When using
createServer and build in the same Node.js process, both functions rely on process.env.NODE_ENV to work properly, which also depends on the mode config option. To prevent conflicting behavior, set process.env.NODE_ENV or the mode of the two APIs to development. Otherwise, you can spawn a child process to run the APIs separately.Middleware Mode Example
When using middleware mode combined with proxy config for WebSocket, the parent http server should be provided inmiddlewareMode to bind the proxy correctly.
ViteDevServer
The ViteDevServer interface provides methods and properties for controlling the dev server.Properties
ResolvedConfig
The resolved Vite config object
Connect.Server
A connect app instance that can be used to attach custom middlewares to the dev server or as the handler function of a custom http server
http.Server | null
Native Node http server instance. Will be null in middleware mode.
FSWatcher
Chokidar watcher instance. If
config.server.watch is set to null, it will not watch any files and calling add or unwatch will have no effect.WebSocketServer
WebSocket server with
send(payload) methodPluginContainer
Rollup plugin container that can run plugin hooks on a given file
ModuleGraph
Module graph that tracks the import relationships, url to file mapping and hmr state
ResolvedServerUrls | null
The resolved urls Vite prints on the CLI (URL-encoded). Returns
null in middleware mode or if the server is not listening on any port.Methods
(url: string, options?: TransformOptions) => Promise<TransformResult | null>
Programmatically resolve, load and transform a URL and get the result without going through the http request pipeline
(url: string, html: string, originalUrl?: string) => Promise<string>
Apply Vite built-in HTML transforms and any plugin HTML transforms
(url: string, options?: { fixStacktrace?: boolean }) => Promise<Record<string, any>>
Load a given URL as an instantiated module for SSR
(e: Error) => void
Fix ssr error stacktrace
(module: ModuleNode) => Promise<void>
Triggers HMR for a module in the module graph. You can use the
server.moduleGraph API to retrieve the module to be reloaded. If hmr is false, this is a no-op.(port?: number, isRestart?: boolean) => Promise<ViteDevServer>
Start the server
(forceOptimize?: boolean) => Promise<void>
Restart the server. The
forceOptimize parameter forces the optimizer to re-bundle, same as --force cli flag.() => Promise<void>
Stop the server
(options?: BindCLIShortcutsOptions<ViteDevServer>) => void
Bind CLI shortcuts
(ignoredId?: string) => Promise<void>
Experimental - Wait until all static imports are processed. If called from a load or transform plugin hook, the id needs to be passed as a parameter to avoid deadlocks.
build
Build for production programmatically. Type Signature:Parameters
InlineConfig
Configuration object that extends
UserConfig with additional properties like mode and configFileReturns
RollupOutput | RollupOutput[]
Returns a Rollup output object or array of outputs
Example Usage
preview
Create a Vite preview server to serve the built application. Type Signature:Parameters
InlineConfig
Configuration object with preview-specific options
Returns
PreviewServer
A promise that resolves to a PreviewServer instance
Example Usage
PreviewServer
The PreviewServer interface for controlling the preview server.Properties
ResolvedConfig
The resolved vite config object
Connect.Server
A connect app instance that can be used to attach custom middlewares to the preview server
http.Server
Native Node http server instance
ResolvedServerUrls | null
The resolved urls Vite prints on the CLI (URL-encoded). Returns
null if the server is not listening on any port.resolveConfig
Resolve the Vite configuration programmatically. Type Signature:Parameters
InlineConfig
required
Inline configuration object
'build' | 'serve'
required
The command value is
serve in dev and preview, and build in buildstring
default:"'development'"
Default mode to use if not specified
string
default:"'development'"
Default NODE_ENV to use if not specified
boolean
default:"false"
Whether this is for preview mode
Returns
ResolvedConfig
The fully resolved Vite configuration
mergeConfig
Deeply merge two Vite configs. Type Signature:Parameters
Record<string, any>
required
The default configuration object
Record<string, any>
required
The override configuration object
boolean
default:"true"
Represents the level within the Vite config which is being merged. Set to
false if you’re merging two build options, for example.Returns
Record<string, any>
The merged configuration object
mergeConfig accepts only config in object form. If you have a config in callback form, you should call it before passing into mergeConfig.Example with Callback Config
You can use thedefineConfig helper to merge a config in callback form with another config:
searchForWorkspaceRoot
Search for the root of the potential workspace. Type Signature:Parameters
string
required
Current directory to start searching from
string
Root directory to use as fallback
Returns
string
The workspace root directory path
root:
- contains
workspacesfield inpackage.json - contains one of the following file:
lerna.jsonpnpm-workspace.yaml
loadEnv
Load.env files from the environment directory.
Type Signature:
Parameters
string
required
The mode to load env files for (e.g., ‘development’, ‘production’)
string
required
The directory to load env files from
string | string[]
default:"'VITE_'"
Only env variables prefixed with these values are loaded
Returns
Record<string, string>
An object containing the loaded environment variables
VITE_ are loaded, unless prefixes is changed.
normalizePath
Normalize a path to interoperate between Vite plugins. Type Signature:Parameters
string
required
The file path to normalize
Returns
string
The normalized path with forward slashes
transformWithOxc
Transform JavaScript or TypeScript with Oxc Transformer. Type Signature:Parameters
string
required
The source code to transform
string
required
The filename for the code
OxcTransformOptions
Oxc transformer options
object
Input source map
Returns
OxcTransformResult & { warnings: string[] }
The transformed code with source map and warnings
transformWithEsbuild
Transform JavaScript or TypeScript with esbuild. Type Signature:loadConfigFromFile
Load a Vite config file manually. Type Signature:Parameters
ConfigEnv
required
Configuration environment with
command and modestring
Path to the config file to load
string
default:"process.cwd()"
Root directory to search for config
LogLevel
Logging level
Logger
Custom logger instance
Returns
{ path: string, config: UserConfig, dependencies: string[] } | null
The loaded config with its path and dependencies, or null if not found
preprocessCSS
Pre-process CSS files to plain CSS. Type Signature:Parameters
string
required
The CSS source code
string
required
The filename with extension (e.g.,
.scss, .less, .styl)ResolvedConfig
required
The resolved Vite configuration
Returns
PreprocessCSSResult
The processed CSS with optional source map and CSS modules mapping
.css, .scss, .sass, .less, .styl and .stylus files to plain CSS so it can be used in browsers or parsed by other tools. The pre-processor used is inferred from the filename extension.
The corresponding pre-processor must be installed if used. Pre-processing will not resolve URLs in
url() or image-set().