Skip to content

ViteConfig

interface ViteConfig

The main configuration block, exposed by the vite extension:

plugins {
    // …
}

vite {
    // …
}

Properties

autoRewriteIndex

@get:



Internal



abstract val autoRewriteIndex: Property<Boolean>

Whether to rewrite <script> declaration in the index.html file to be compatible with the Vite plugin and Hot Module Replacement. Disabled by default.

Example
vite {
    autoRewriteIndex.set(true)
}

base

@get:



Internal



abstract val base: Property<String>

Base public path when served in development or production.

Valid values include:

  • Absolute URL pathname, e.g. /foo/,

  • Full URL, e.g. https://foo.com/,

  • Empty string or ./ (for embedded deployment).

Example

To expose the generate website to the /ui path instead of the website root, use:

vite {
    base.set("/ui")
}
External resources

build

open fun build(block: ViteBuildConfig.() -> Unit): ViteBuildConfig
abstract val build: ViteBuildConfig

Configuration for Rollup to build the production bundle.

Example
vite {
    build {
        // …
    }
}

cacheDir

@get:



Internal



abstract val cacheDir: Property<String>

Directory to save cache files. Files in this directory are pre-bundled deps or some other cache files generated by vite, which can improve the performance.

The value can be either an absolute file system path or a path relative to project root.

plugins

@get:



Internal



abstract val plugins: ListProperty<ExternalVitePlugin>

The list of Vite plugins from NPM imported by this project.

To easily add values of this property, we provide the plugin helper function.

For more information on plugins, see ExternalVitePlugin.

publicDir

@get:



Internal



abstract val publicDir: Property<String>

Directory to serve as plain static assets. Files in this directory are served at / during dev and copied to the root outDir during build, and are always served or copied as-is without transform.

The value can be either an absolute file system path or a path relative to the project root.

root

@get:



Internal



abstract val root: DirectoryProperty

Project root directory, where index.html is located.

Example
vite {
    root.set(project.layout.projectDirectory.file("index.html"))
}
External resources

server

open fun server(block: ViteServerConfig.() -> Unit): ViteServerConfig
abstract val server: ViteServerConfig

Configuration for the development server.

Example
vite {
    server {
        // …
    }
}

version

@get:



Internal



abstract val version: Property<String>

The version of the Vite package used by this build.

Example
vite {
    version.set("3.0.0")
}
External resources

Functions

localPlugin

open fun localPlugin(
    path: String, 
    exportedAs: String, 
    version: String = "*", 
    configuration: String? = null, 
    isNamedExport: Boolean = false
)

Imports a local file as a Vite plugin.

Local plugins are not added to package.json and are not downloaded. For more information, see ExternalVitePlugin.isLocal.

This is a helper function to add an element to plugins. For more information on the different parameters, see ExternalVitePlugin.

Example
vite {
    localPlugin("../../../../vite.config.d/myplugin.mjs", "myplugin")
}

plugin

open fun plugin(
    packageName: String, 
    exportedAs: String, 
    version: String, 
    configuration: String? = null, 
    isNamedExport: Boolean = false, 
    isLocal: Boolean = false
)

Imports a plugin from NPM.

This is a helper function to add an element to plugins. For more information on the different parameters, see ExternalVitePlugin.

Example
vite {
    plugin("@originjs/vite-plugin-commonjs", "viteCommonJs", "1.0.3")
}

setDefaults

open fun setDefaults()

Sets the default values for all configuration fields.

setDefaultsFrom

open fun setDefaultsFrom(other: ViteConfig)