createServer
Create a Vite development server instance programmatically. Type Signature:Parameters
Configuration object that extends
UserConfig with additional properties:Returns
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
The resolved Vite config object
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
Native Node http server instance. Will be null in middleware mode.
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.WebSocket server with
send(payload) methodRollup plugin container that can run plugin hooks on a given file
Module graph that tracks the import relationships, url to file mapping and hmr state
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
Programmatically resolve, load and transform a URL and get the result without going through the http request pipeline
Apply Vite built-in HTML transforms and any plugin HTML transforms
Load a given URL as an instantiated module for SSR
Fix ssr error stacktrace
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.Start the server
Restart the server. The
forceOptimize parameter forces the optimizer to re-bundle, same as --force cli flag.Stop the server
Bind CLI shortcuts
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
Configuration object that extends
UserConfig with additional properties like mode and configFileReturns
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
Configuration object with preview-specific options
Returns
A promise that resolves to a PreviewServer instance
Example Usage
PreviewServer
The PreviewServer interface for controlling the preview server.Properties
The resolved vite config object
A connect app instance that can be used to attach custom middlewares to the preview server
Native Node http server instance
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
Inline configuration object
The command value is
serve in dev and preview, and build in buildDefault mode to use if not specified
Default NODE_ENV to use if not specified
Whether this is for preview mode
Returns
The fully resolved Vite configuration
mergeConfig
Deeply merge two Vite configs. Type Signature:Parameters
The default configuration object
The override configuration object
Represents the level within the Vite config which is being merged. Set to
false if you’re merging two build options, for example.Returns
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
Current directory to start searching from
Root directory to use as fallback
Returns
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
The mode to load env files for (e.g., ‘development’, ‘production’)
The directory to load env files from
Only env variables prefixed with these values are loaded
Returns
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
The file path to normalize
Returns
The normalized path with forward slashes
transformWithOxc
Transform JavaScript or TypeScript with Oxc Transformer. Type Signature:Parameters
The source code to transform
The filename for the code
Oxc transformer options
Input source map
Returns
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
Configuration environment with
command and modePath to the config file to load
Root directory to search for config
Logging level
Custom logger instance
Returns
The loaded config with its path and dependencies, or null if not found
preprocessCSS
Pre-process CSS files to plain CSS. Type Signature:Parameters
The CSS source code
The filename with extension (e.g.,
.scss, .less, .styl)The resolved Vite configuration
Returns
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().