reactjssassviteautoprefixer

How to use autoprefixer with ViteJS and React?


I'm using React with ViteJS and SASS, but i have a problem. It seems there is not autoprefixer for CSS/SCSS when i will build the project.

How to add an auto-prefixer with ViteJS and SASS?


Solution

  • Add postcss and autoprefixer: yarn add -D postcss@latest autoprefixer@latest

    then add a file postcss.config.js on your root project directory:

    module.exports = {
      plugins: {
        autoprefixer: {}
      }
    }
    

    ℹ️ Below is an example of a configuration file vite.config.ts that includes configuration for postcss.

    import { defineConfig } from "vite"
    import react from '@vitejs/plugin-react'
    import autoprefixer from 'autoprefixer'
    
    export default defineConfig({
      plugins: [
        react()
      ],
      css: {
        postcss: {
          plugins: [
            autoprefixer({}) // add options if needed
          ],
        }
      }
    })
    

    And if it still doesn't work please provide a reproducible project.