JavaScript API

Vite’s JavaScript APIs are fully typed, and it’s recommended to use TypeScript or enable JS type checking in VSCode to leverage the intellisense and validation.

createServer

Type Signature

  1. async function createServer(inlineConfig?: InlineConfig): Promise<ViteDevServer>

Example Usage

  1. const { createServer } = require('vite')
  2. ;(async () => {
  3. const server = await createServer({
  4. // any valid user config options, plus `mode` and `configFile`
  5. configFile: false,
  6. root: __dirname,
  7. server: {
  8. port: 1337
  9. }
  10. })
  11. await server.listen()
  12. })()

Using the Vite Server as a Middleware

Vite can be used as a middleware in an existing raw Node.js http server or frameworks that are comaptible with the (req, res, next) => {} style middlewares. For example with express:

  1. const vite = require('vite')
  2. const express = require('express')
  3. ;(async () => {
  4. const app = express()
  5. // create vite dev server in middleware mode
  6. // so vite creates the hmr websocket server on its own.
  7. // the ws server will be listening at port 24678 by default, and can be
  8. // configured via server.hmr.port
  9. const viteServer = await vite.createServer({
  10. server: {
  11. middlewareMode: true
  12. }
  13. })
  14. // use vite's connect instance as middleware
  15. app.use(viteServer.app)
  16. app.listen(3000)
  17. })()

InlineConfig

The InlineConfig interface extends UserConfig with additional properties:

  • mode: override default mode ('development' for server)
  • configFile: specify config file to use. If not set, Vite will try to automatically resolve one from project root. Set to false to disable auto resolving.

ViteDevServer

  1. interface ViteDevServer {
  2. /**
  3. * The resolved vite config object
  4. */
  5. config: ResolvedConfig
  6. /**
  7. * connect app instance
  8. * This can also be used as the handler function of a custom http server
  9. * https://github.com/senchalabs/connect#use-middleware
  10. */
  11. app: Connect.Server
  12. /**
  13. * native Node http server instance
  14. */
  15. httpServer: http.Server
  16. /**
  17. * chokidar watcher instance
  18. * https://github.com/paulmillr/chokidar#api
  19. */
  20. watcher: FSWatcher
  21. /**
  22. * web socket server with `send(payload)` method
  23. */
  24. ws: WebSocketServer
  25. /**
  26. * Rollup plugin container that can run plugin hooks on a given file
  27. */
  28. pluginContainer: PluginContainer
  29. /**
  30. * Module graph that tracks the import relationships, url to file mapping
  31. * and hmr state.
  32. */
  33. moduleGraph: ModuleGraph
  34. /**
  35. * Programmatically resolve, load and transform a URL and get the result
  36. * without going through the http request pipeline.
  37. */
  38. transformRequest(url: string): Promise<TransformResult | null>
  39. /**
  40. * Util for transforming a file with esbuild.
  41. * Can be useful for certain plugins.
  42. */
  43. transformWithEsbuild(
  44. code: string,
  45. filename: string,
  46. options?: EsbuildTransformOptions,
  47. inMap?: object
  48. ): Promise<EsbuildTransformResult>
  49. /**
  50. * Start the server.
  51. */
  52. listen(port?: number): Promise<ViteDevServer>
  53. /**
  54. * Stop the server.
  55. */
  56. close(): Promise<void>
  57. }

build

Type Signature

  1. async function build(
  2. inlineConfig?: InlineConfig
  3. ): Promise<RollupOutput | RollupOutput[]>

Example Usage

  1. const path = require('path')
  2. const { build } = require('vite')
  3. ;(async () => {
  4. await build({
  5. root: path.resolve(__dirname, './project'),
  6. build: {
  7. base: '/foo/',
  8. rollupOptions: {
  9. // ...
  10. }
  11. }
  12. })
  13. })()

resolveConfig

Type Signature

  1. async function resolveConfig(
  2. inlineConfig: InlineConfig,
  3. command: 'build' | 'serve'
  4. ): Promise<ResolvedConfig>