Enable Sass preprocessing in a Vite project
Learn how to add Sass support to a Vite project, verify HMR during dev, check production output, and roll back if needed.
27 Feb 2026, 02:30 UTC

Desired outcome
Enable Sass (.scss/.sass) preprocessing in a Vite‑powered frontend project so that styles are compiled automatically during development and production builds.
Prerequisites
- Node.js >= 18 (or the version your project uses)
- npm or yarn installed
- An existing Vite project created with
npm create vite@latestor similar - Basic familiarity with editing JavaScript/TypeScript and importing CSS
Procedure
- Add the Sass compiler as a dev dependency:
# Run in the project root npm i -D sass # or: yarn add -D sassThis installs Dart Sass, which Vite’s built‑in CSS pipeline uses automatically.
- Import or reference Sass files:
- In a JavaScript/TypeScript entry point:
import './styles/main.scss'; - In a Vue single‑file component:
<style lang="scss"> - In a Svelte component:
<style lang="scss">
No additional Vite configuration is required for basic Sass support.
- In a JavaScript/TypeScript entry point:
- Verify hot‑module replacement (HMR) in development:
npm run dev # or: yarn devEdit a
.scssfile and save; the browser should update the style instantly without a full page reload. Open DevTools → Network → look for an HMR update or check the console for “[Vite] HMR update”. - Confirm production build output:
npm run build # or: yarn buildInspect the generated
dist/assets/*.cssfile (or the path configured bybuild.outDir). The file should contain plain CSS, no Sass syntax. Ifbuild.sourcemapis true, a corresponding.css.mapfile will be present.
Expected checks
- During
dev, no Sass syntax errors appear in the console; changes trigger HMR. - The
devserver logs show “css preprocessor: sass” (Vite prints this when it detects the Sass dependency). - After
build, theassetsdirectory contains only.css(and optionally.css.map) files. - File size of the compiled CSS is reasonable; source maps can be opened in DevTools to map back to original Sass lines.
Recovery / rollback options
If you need to disable Sass support:
- Remove the dev dependency:
npm uninstall -D sass # or: yarn remove -D sass - Delete or rename any
.scss/.sassfiles you no longer wish to use, or change imports to plain CSS. - Restart the dev server and rebuild; Vite will fall back to treating the files as plain CSS (or will report missing module errors if imports remain).
- Commit the changes to your version‑control system to retain a known‑good state.
Limitations and practical tips
- Using an outdated Sass version (< 1.0) may cause compatibility errors; prefer Dart Sass 1.60 or newer.
- Large projects with deep
@usechains can increase dev‑server start time; consider splitting styles into smaller partials or enablingcss.devSourcemaponly when debugging. - If you add custom PostCSS plugins, ensure they are listed after the Sass loader in
css.preprocessorOptionsto avoid processing order issues.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.