TExcelTAD
Home › Vite › Vite Pre-processors & PostCSS

Vite Pre-processors & PostCSS

Plain CSS is powerful today, but many teams still use Sass, Less or Stylus, and almost everyone benefits from PostCSS tools such as Autoprefixer or Tailwind CSS. Vite supports all of them with almost no configuration.

Pre-processors: just install the compiler

Vite has built-in support for .scss, .sass, .less, .styl and .stylus files. It does not bundle the compilers itself (to keep installs small), so you add the one you need:

bash · try it yourself
npm install -D sass-embedded
added 18 packages, and audited 31 packages in 5s

found 0 vulnerabilities
Language Package to install
Sass / SCSS sass-embedded (fastest) or sass
Less less
Stylus stylus

That's it — no plugin, no loader configuration. Import the file like any CSS:

scss
// src/styles/main.scss
$brand: #646cff;
$radius: 8px;

.card {
  border: 1px solid rgba($brand, 0.3);
  border-radius: $radius;

  &__title {
    color: $brand;
  }

  &:hover {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }
}
js
// src/main.js
import './styles/main.scss';

CSS Modules work with pre-processors too: name the file Card.module.scss.

Sharing variables everywhere

A common need is making variables or mixins available in every SCSS file (including Vue/Svelte <style lang="scss"> blocks) without importing them manually. Use preprocessorOptions.additionalData:

js
// vite.config.js
import { defineConfig } from 'vite';

export default defineConfig({
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "/src/styles/tokens" as *;\n`,
      },
    },
  },
});

Note: Modern Sass prefers @use over the deprecated @import. Vite 7+ always uses Sass's modern compiler API, so older options like api: 'legacy' are gone.

What a pre-processor does

Nesting and variables are compiled down to flat CSS before the browser sees anything. This tiny compiler handles one level of & nesting and $variables — enough to see the idea:

js · try it yourself
function miniScss(src) {
  const vars = {};
  src = src.replace(/\$([\w-]+):\s*([^;]+);/g, (_, n, v) => { vars[n] = v.trim(); return ''; });
  src = src.replace(/\$([\w-]+)/g, (_, n) => vars[n]);
  const out = [];
  const outer = /([^{}]+)\{([^{}]*(?:\{[^{}]*\}[^{}]*)*)\}/g;
  for (const [, sel, body] of src.matchAll(outer)) {
    const parent = sel.trim();
    const own = body.replace(/&([^{]*)\{([^}]*)\}/g, (_, suffix, inner) => {
      out.push(`${parent}${suffix.trim()} { ${inner.trim()} }`);
      return '';
    });
    out.unshift(`${parent} { ${own.trim().replace(/\s+/g, ' ')} }`);
  }
  return out.join('\n');
}

console.log(miniScss(`
  $brand: #646cff;
  .card {
    color: $brand;
    &:hover { opacity: 0.8; }
    &__title { font-weight: bold; }
  }
`));
.card { color: #646cff; }
.card:hover { opacity: 0.8; }
.card__title { font-weight: bold; }

Native CSS can do a lot now

Before reaching for Sass, remember modern CSS has variables and nesting built in. This page uses custom properties and native nesting with no build step at all:

html · try it yourself
<!doctype html>
<html>
  <head>
    <style>
      :root { --brand: #646cff; --radius: 8px; }
      body { font-family: system-ui; padding: 1rem; }
      .card {
        border: 2px solid var(--brand);
        border-radius: var(--radius);
        padding: 1rem;
        max-width: 260px;
        & .title { color: var(--brand); margin: 0 0 .5rem; }
        &:hover { background: color-mix(in srgb, var(--brand) 12%, white); }
      }
    </style>
  </head>
  <body>
    <div class="card">
      <h3 class="title">Native nesting</h3>
      <p>Hover me. No Sass required.</p>
    </div>
  </body>
</html>

PostCSS

PostCSS is a tool that transforms CSS with JavaScript plugins. Vite automatically applies PostCSS if it finds a config file (postcss.config.js, .postcssrc, …) in the project. It runs on all CSS — including CSS produced by Sass or Less.

bash · try it yourself
npm install -D postcss autoprefixer
added 7 packages, and audited 38 packages in 2s

found 0 vulnerabilities
js
// postcss.config.js
import autoprefixer from 'autoprefixer';

export default {
  plugins: [autoprefixer()],
};

You can also put the config inline in vite.config.js under css.postcss; then Vite does not search for a separate file.

Popular PostCSS plugin Purpose
autoprefixer Adds vendor prefixes based on your browserslist
postcss-preset-env Lets you use future CSS today
postcss-nesting Spec-compliant nesting for older browsers
cssnano Extra minification (Vite already minifies)

Tailwind CSS v4

Tailwind CSS v4 ships a dedicated Vite plugin, which is faster than going through PostCSS:

bash · try it yourself
npm install -D tailwindcss @tailwindcss/vite
added 14 packages, and audited 52 packages in 4s

found 0 vulnerabilities
js
// vite.config.js
import { defineConfig } from 'vite';
import tailwindcss from '@tailwindcss/vite';

export default defineConfig({
  plugins: [tailwindcss()],
});
css
/* src/style.css */
@import "tailwindcss";

Lightning CSS (optional)

Vite 8 uses Lightning CSS to minify CSS by default. You can also make it the CSS transformer (replacing PostCSS for prefixing and syntax lowering) with css: { transformer: 'lightningcss' } — fast, but PostCSS plugins are then not applied.

Tip: If styles don't update or Sass errors mention a missing package, check that the compiler is installed as a dev dependency and restart the dev server.

Test yourself

What do you need to use .scss files in a Vite project?

  1. A Vite Sass plugin and a loader
  2. Only to install sass-embedded (or sass)
  3. Nothing at all, Sass is bundled with Vite
  4. A webpack config

When does Vite apply PostCSS?

  1. Never
  2. Only in production
  3. Automatically when a PostCSS config is found (or css.postcss is set)
  4. Only for .module.css files

Which option injects shared SCSS (like variables) into every SCSS file?

  1. css.modules
  2. css.preprocessorOptions.scss.additionalData
  3. build.cssTarget
  4. resolve.alias