Using Vite Plugins in Nuxt

Learn how to integrate Vite plugins into your Nuxt project.

While Nuxt modules offer extensive functionality, sometimes a specific Vite plugin might meet your needs more directly.

First, we need to install the Vite plugin, for our example, we'll use @rollup/plugin-yaml:

npm install @rollup/plugin-yaml

Next, we need to import and add it to our nuxt.config.ts file:

nuxt.config.ts
import yaml from '@rollup/plugin-yaml'

export default defineNuxtConfig({
  vite: {
    plugins: [
      yaml(),
    ],
  },
})

Now we installed and configured our Vite plugin, we can start using YAML files directly in our project.

For example, we can have a config.yaml that stores configuration data and import this data in our Nuxt components:

greeting: "Hello, Nuxt with Vite!"

Using Vite Plugins in Nuxt Modules

If you're developing a Nuxt module and need to add Vite plugins, you should use the addVitePlugin utility:

modules/my-module.ts
import { addVitePlugin, defineNuxtModule } from '@nuxt/kit'
import yaml from '@rollup/plugin-yaml'

export default defineNuxtModule({
  setup () {
    addVitePlugin(yaml())
  },
})
If you're writing code that needs to access resolved Vite configuration, you should use the config and configResolved hooks within your Vite plugin, rather than using Nuxt's vite:extend, vite:extendConfig and vite:configResolved.
Read more about addVitePlugin in the Nuxt Kit documentation.