TExcelTAD
Home › Sass › Sass with Vite

Sass with Vite

Vite is a fast build tool for modern web apps. It has built-in support for Sass: install the compiler, import a .scss file, and Vite compiles it on the fly with hot reloading — no extra configuration or watch command needed. (This very site is built with Vite.)

Setup

Create a project and add Sass:

bash · try it yourself
npm create vite@latest my-site -- --template vanilla-ts
cd my-site
npm install
npm install --save-dev sass-embedded
Scaffolding project in ./my-site...

Done. Now run:

  cd my-site
  npm install
  npm run dev

added 12 packages, and audited 13 packages in 3s

found 0 vulnerabilities

added 18 packages, and audited 31 packages in 2s

found 0 vulnerabilities

Vite works with either the sass package or the faster sass-embedded package (the same Dart Sass, running as a native binary). Vite prefers sass-embedded when both are installed.

Importing SCSS

Import the stylesheet from your JavaScript or TypeScript entry file:

ts
// src/main.ts
import "./styles/main.scss";

document.querySelector("#app")!.innerHTML = `<button class="btn">Hi</button>`;
scss
// src/styles/main.scss
@use "abstracts" as *;

.btn {
  background: $brand;
  border-radius: $radius;
}

Run npm run dev: Vite compiles the SCSS, injects it into the page and, when you save a .scss file, updates the styles without reloading the page.

Project layout

my-site/
├── index.html
├── package.json
├── vite.config.ts
└── src/
    ├── main.ts
    └── styles/
        ├── main.scss
        ├── abstracts/
        │   ├── _index.scss
        │   ├── _tokens.scss
        │   └── _mixins.scss
        └── components/
            └── _button.scss

Configuring Sass in vite.config.ts

css.preprocessorOptions.scss passes options to the Sass compiler. A common use is additionalData, which prepends code to every SCSS file — for example, making your tokens available everywhere:

ts
// vite.config.ts
import { defineConfig } from "vite";

export default defineConfig({
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "/src/styles/abstracts" as *;\n`,
        silenceDeprecations: ["import"],   // only if old @import code remains
      },
    },
    devSourcemap: true,
  },
});

Note: Be careful with additionalData: whatever it adds is included in every file. Only prepend modules that output no CSS (abstracts), or styles will be duplicated.

CSS Modules with Sass

Name a file *.module.scss and Vite turns it into a CSS Module: class names are made unique per file, and importing gives you a mapping object.

scss
// Card.module.scss
.card { padding: 16px; }
.title { font-weight: 700; }
ts
import styles from "./Card.module.scss";
el.className = styles.card;   // e.g. "_card_1x8k2_1"

Building for production

bash · try it yourself
npm run build
vite v8.3.4 building client environment for production...
✓ 6 modules transformed.
rendering chunks...
computing gzip size...
dist/index.html                  0.46 kB │ gzip: 0.30 kB
dist/assets/index-B8cR2l1q.css   1.21 kB │ gzip: 0.58 kB
dist/assets/index-Cq9mV0oP.js    0.95 kB │ gzip: 0.53 kB
✓ built in 312ms

The production CSS is compiled, minified and given a content hash in its file name so browsers can cache it forever.

What the compiled CSS looks like

Vite runs the same Dart Sass compiler as the playgrounds. Here's a small component as it would be compiled during the build (before minification):

scss · try it yourself
@use "sass:color";

$brand: #cc6699;

.btn {
  background: $brand;
  color: white;
  border: 0;
  padding: 8px 16px;
  border-radius: 6px;
  transition: background 150ms;

  &:hover { background: color.scale($brand, $lightness: -15%); }
}
.btn {
  background: #cc6699;
  color: white;
  border: 0;
  padding: 8px 16px;
  border-radius: 6px;
  transition: background 150ms;
}
.btn:hover {
  background: rgb(75.5%, 26.5%, 51%);
}

Other frameworks

Tool Sass support
Vite (Vue, React, Svelte templates) Built in — just install sass or sass-embedded
Astro, Nuxt, SvelteKit Built in through Vite
Next.js Built in (npm install sass)
Angular CLI Choose SCSS when creating the project
Webpack sass-loader + css-loader

Tip: In .vue and .svelte files, write <style lang="scss"> to use Sass inside single-file components.

Test yourself

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

  1. A Vite Sass plugin
  2. The sass or sass-embedded package
  3. node-sass
  4. Nothing at all

What does additionalData in preprocessorOptions.scss do?

  1. Adds extra CSS files to the build
  2. Prepends code to every SCSS file Vite compiles
  3. Minifies the CSS
  4. Enables source maps

What does naming a file Button.module.scss do in Vite?

  1. Makes it a Sass module for @use
  2. Turns it into a CSS Module with locally scoped class names
  3. Excludes it from the build
  4. Compiles it to JavaScript