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:
npm create vite@latest my-site -- --template vanilla-ts
cd my-site
npm install
npm install --save-dev sass-embeddedScaffolding 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:
// src/main.ts
import "./styles/main.scss";
document.querySelector("#app")!.innerHTML = `<button class="btn">Hi</button>`;// 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.scssConfiguring 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:
// 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.
// Card.module.scss
.card { padding: 16px; }
.title { font-weight: 700; }import styles from "./Card.module.scss";
el.className = styles.card; // e.g. "_card_1x8k2_1"Building for production
npm run buildvite 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):
@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
.vueand.sveltefiles, 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?
- A Vite Sass plugin
- The
sassorsass-embeddedpackage - node-sass
- Nothing at all
What does additionalData in preprocessorOptions.scss do?
- Adds extra CSS files to the build
- Prepends code to every SCSS file Vite compiles
- Minifies the CSS
- Enables source maps
What does naming a file Button.module.scss do in Vite?
- Makes it a Sass module for @use
- Turns it into a CSS Module with locally scoped class names
- Excludes it from the build
- Compiles it to JavaScript