Skip to content

Commit d4e0442

Browse files
hi-ogawabluwysapphi-red
authored
feat(css)!: change default sass api to modern/modern-compiler (#17937)
Co-authored-by: Bjorn Lu <[email protected]> Co-authored-by: sapphi-red <[email protected]>
1 parent e51dc40 commit d4e0442

File tree

7 files changed

+24
-36
lines changed

7 files changed

+24
-36
lines changed

docs/config/shared-options.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,12 @@ Note if an inline config is provided, Vite will not search for other PostCSS con
227227

228228
Specify options to pass to CSS pre-processors. The file extensions are used as keys for the options. The supported options for each preprocessors can be found in their respective documentation:
229229

230-
- `sass`/`scss` - top level option `api: "legacy" | "modern" | "modern-compiler"` (default `"legacy"`) allows switching which sass API to use. For the best performance, it's recommended to use `api: "modern-compiler"` with `sass-embedded` package. [Options (legacy)](https://sass-lang.com/documentation/js-api/interfaces/LegacyStringOptions), [Options (modern)](https://sass-lang.com/documentation/js-api/interfaces/stringoptions/).
231-
- `less` - [Options](https://lesscss.org/usage/#less-options).
232-
- `styl`/`stylus` - Only [`define`](https://stylus-lang.com/docs/js.html#define-name-node) is supported, which can be passed as an object.
230+
- `sass`/`scss`:
231+
- Select the sass API to use with `api: "modern-compiler" | "modern" | "legacy"` (default `"modern-compiler"` if `sass-embedded` is installed, otherwise `"modern"`). For the best performance, it's recommended to use `api: "modern-compiler"` with the `sass-embedded` package. The `"legacy"` API is deprecated and will be removed in Vite 7.
232+
- [Options (modern)](https://sass-lang.com/documentation/js-api/interfaces/stringoptions/)
233+
- [Options (legacy)](https://sass-lang.com/documentation/js-api/interfaces/LegacyStringOptions).
234+
- `less`: [Options](https://lesscss.org/usage/#less-options).
235+
- `styl`/`stylus`: Only [`define`](https://stylus-lang.com/docs/js.html#define-name-node) is supported, which can be passed as an object.
233236

234237
**Example:**
235238

docs/guide/migration.md

+8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ From Vite 6, even when `json.stringify: true` is set, `json.namedExports` is not
2020

2121
Vite 6 also introduces a new default value for `json.stringify` which is `'auto'`, which will only stringify large JSON files. To disable this behavior, set `json.stringify: false`.
2222

23+
### Sass now uses modern API by default
24+
25+
In Vite 5, the legacy API was used by default for Sass. Vite 5.4 added support for the modern API.
26+
27+
From Vite 6, the modern API is used by default for Sass. If you wish to still use the legacy API, you can set [`css.preprocessorOptions.sass.api: 'legacy'` / `css.preprocessorOptions.scss.api: 'legacy'`](/config/shared-options#css-preprocessoroptions). But note that the legacy API support will be removed in Vite 7.
28+
29+
To migrate to the modern API, see [the Sass documentation](https://sass-lang.com/documentation/breaking-changes/legacy-js-api/).
30+
2331
## Advanced
2432

2533
There are other breaking changes which only affect few users.

packages/vite/src/node/plugins/css.ts

+9-16
Original file line numberDiff line numberDiff line change
@@ -1979,8 +1979,8 @@ type PreprocessorAdditionalData =
19791979
type SassPreprocessorOptions = {
19801980
additionalData?: PreprocessorAdditionalData
19811981
} & (
1982-
| ({ api?: 'legacy' } & SassLegacyPreprocessBaseOptions)
1983-
| ({ api: 'modern' | 'modern-compiler' } & SassModernPreprocessBaseOptions)
1982+
| ({ api: 'legacy' } & SassLegacyPreprocessBaseOptions)
1983+
| ({ api?: 'modern' | 'modern-compiler' } & SassModernPreprocessBaseOptions)
19841984
)
19851985

19861986
type LessPreprocessorOptions = {
@@ -2469,9 +2469,9 @@ const scssProcessor = (
24692469
},
24702470
async process(environment, source, root, options, resolvers) {
24712471
const sassPackage = loadSassPackage(root)
2472-
// TODO: change default in v6
2473-
// options.api ?? sassPackage.name === "sass-embedded" ? "modern-compiler" : "modern";
2474-
const api = options.api ?? 'legacy'
2472+
const api =
2473+
options.api ??
2474+
(sassPackage.name === 'sass-embedded' ? 'modern-compiler' : 'modern')
24752475

24762476
if (!workerMap.has(options.alias)) {
24772477
workerMap.set(
@@ -3001,18 +3001,11 @@ const createPreprocessorWorkerController = (maxWorkers: number | undefined) => {
30013001

30023002
const sassProcess: StylePreprocessor<SassStylePreprocessorInternalOptions>['process'] =
30033003
(environment, source, root, options, resolvers) => {
3004-
let opts: SassStylePreprocessorInternalOptions
3005-
if (options.api === 'modern' || options.api === 'modern-compiler') {
3006-
opts = { ...options, syntax: 'indented' as const }
3004+
const opts: SassStylePreprocessorInternalOptions = { ...options }
3005+
if (opts.api === 'legacy') {
3006+
opts.indentedSyntax = true
30073007
} else {
3008-
const narrowedOptions =
3009-
options as SassStylePreprocessorInternalOptions & {
3010-
api?: 'legacy'
3011-
}
3012-
opts = {
3013-
...narrowedOptions,
3014-
indentedSyntax: true,
3015-
}
3008+
opts.syntax = 'indented'
30163009
}
30173010
return scss.process(environment, source, root, opts, resolvers)
30183011
}

playground/backend-integration/vite.config.js

-7
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,4 @@ function BackendIntegrationExample() {
5454
export default defineConfig({
5555
base: '/dev/',
5656
plugins: [BackendIntegrationExample()],
57-
css: {
58-
preprocessorOptions: {
59-
scss: {
60-
silenceDeprecations: ['legacy-js-api'],
61-
},
62-
},
63-
},
6457
})

playground/css-sourcemap/vite.config.js

-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ export default defineConfig({
3131
}
3232
},
3333
},
34-
sass: {
35-
silenceDeprecations: ['legacy-js-api', 'import'],
36-
},
3734
},
3835
},
3936
build: {

playground/css/vite.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export default defineConfig({
6161
},
6262
preprocessorOptions: {
6363
scss: {
64+
api: 'legacy',
6465
additionalData: `$injectedColor: orange;`,
6566
importer: [
6667
function (url) {

playground/multiple-entrypoints/vite.config.js

-7
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,4 @@ export default defineConfig({
3737
},
3838
},
3939
},
40-
css: {
41-
preprocessorOptions: {
42-
scss: {
43-
silenceDeprecations: ['legacy-js-api'],
44-
},
45-
},
46-
},
4740
})

0 commit comments

Comments
 (0)