Writing a custom build script, for example to add a post-processing build step.
dev()
Start the development server.
import { dev } from 'vike/api'console.log('Starting development server...')const { viteConfig, // Resolved Vite configuration viteServer, // Vite's development server} = await dev({ viteConfig // Vite configuration (optional)})await viteServer.listen()viteServer.printUrls()viteServer.bindCLIShortcuts({ print: true })const { port } = viteConfig.serverconsole.log(`Development server is ready at http://localhost:${port}`)
Start preview server using production build (only works for SSG apps).
import { preview } from 'vike/api'const { viteConfig, // Resolved Vite configuration viteServer, // Vite's development server} = await preview({ viteConfig // Vite configuration (optional)})viteServer.printUrls()viteServer.bindCLIShortcuts({ print: true })const { port } = viteConfig.previewconsole.log(`Preview server is ready at http://localhost:${port}`)
import { prerender } from 'vike/api'const { viteConfig // Resolved Vite configuration} = await prerender({ viteConfig, // Vite configuration (optional) pageContextInit, // Initial pageContext (optional) onPagePrerender // Called before writing an HTML file (optional)})console.log('Pre-rendering is done')
onPagePrerender(): control where/how HTML files are written.
⚠️
Don't use this option without having contacted a Vike maintainer: this functionality may be changed/removed at any time.
prerender({ // If onPagePrerender() is set, then Vike won't create HTML files onPagePrerender(pageContext) { // Cusotm logic for writing HTML files to the filesystem // ... }})
viteConfig
All API functions accept the option viteConfig (with type InlineConfig).
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('Development 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')