import { dev, build, preview, prerender } from 'vike/api'
 
// Same as CLI `$ vike dev`
await dev()
console.log('Dev server is ready')
 
// Same as CLI `$ vike build`
await build()
console.log('Build is done')
 
// Same as CLI `$ vike preview`
await preview()
console.log('Preview server is ready')
 
// Same as CLI `$ vike prerender`
await prerender()
console.log('Pre-rendering is done')

Options

All API functions have the option { viteConfig: { /*...*/ }}. (It's the only option.)

We generally recommend to define your Vite settings in your vite.config.js file instead of using the viteConfig option. The API automatically loads your vite.config.js file (just like the CLI).

If you want to define Vite settings outside of your app (typically when building your own framework) you can do this:

import { dev } from 'vike/api'
 
await dev({
  viteConfig: {
    // vite.config.js can live in node_modules/my-framework/src/vite.config.ts
    configFile: './path/to/vite.config.js',
    // The app's root can be somewhere completely else than vite.config.js
    root: './path/to/app/'
    // Some Vite settings overriding vite.config.js
    /* ... */
  }
})
console.log('Dev server is ready')

If you want to define your entire Vite config programmatically:

import { build } from 'vike/api'
 
await build({
  viteConfig: {
    // Don't load any vite.config.js
    configFile: false,
    // All Vite settings
    /* ... */
  }
})
console.log('Build is done')

See also: Vite > JavaScript API > InlineConfig

See also