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. })()

InlineConfig

The InlineConfig interface extends UserConfig with additional properties:

  • 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.
  • envFile: Set to false to disable .env files.

ViteDevServer

  1. interface ViteDevServer {
  2. /**
  3. * The resolved vite config object.
  4. */
  5. config: ResolvedConfig
  6. /**
  7. * A connect app instance
  8. * - Can be used to attach custom middlewares to the dev server.
  9. * - Can also be used as the handler function of a custom http server
  10. * or as a middleware in any connect-style Node.js frameworks.
  11. *
  12. * https://github.com/senchalabs/connect#use-middleware
  13. */
  14. middlewares: Connect.Server
  15. /**
  16. * Native Node http server instance.
  17. * Will be null in middleware mode.
  18. */
  19. httpServer: http.Server | null
  20. /**
  21. * Chokidar watcher instance.
  22. * https://github.com/paulmillr/chokidar#api
  23. */
  24. watcher: FSWatcher
  25. /**
  26. * Web socket server with `send(payload)` method.
  27. */
  28. ws: WebSocketServer
  29. /**
  30. * Rollup plugin container that can run plugin hooks on a given file.
  31. */
  32. pluginContainer: PluginContainer
  33. /**
  34. * Module graph that tracks the import relationships, url to file mapping
  35. * and hmr state.
  36. */
  37. moduleGraph: ModuleGraph
  38. /**
  39. * Programmatically resolve, load and transform a URL and get the result
  40. * without going through the http request pipeline.
  41. */
  42. transformRequest(
  43. url: string,
  44. options?: TransformOptions
  45. ): Promise<TransformResult | null>
  46. /**
  47. * Apply vite built-in HTML transforms and any plugin HTML transforms.
  48. */
  49. transformIndexHtml(url: string, html: string): Promise<string>
  50. /**
  51. * Util for transforming a file with esbuild.
  52. * Can be useful for certain plugins.
  53. */
  54. transformWithEsbuild(
  55. code: string,
  56. filename: string,
  57. options?: EsbuildTransformOptions,
  58. inMap?: object
  59. ): Promise<ESBuildTransformResult>
  60. /**
  61. * Load a given URL as an instantiated module for SSR.
  62. */
  63. ssrLoadModule(
  64. url: string,
  65. options?: { isolated?: boolean }
  66. ): Promise<Record<string, any>>
  67. /**
  68. * Fix ssr error stacktrace.
  69. */
  70. ssrFixStacktrace(e: Error): void
  71. /**
  72. * Start the server.
  73. */
  74. listen(port?: number, isRestart?: boolean): Promise<ViteDevServer>
  75. /**
  76. * Stop the server.
  77. */
  78. close(): Promise<void>
  79. }

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. defaultMode?: string
  5. ): Promise<ResolvedConfig>