Skip to content

Commit

Permalink
feat(css)!: change default sass api to modern/modern-compiler (#17937)
Browse files Browse the repository at this point in the history
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 25, 2024
1 parent e51dc40 commit d4e0442
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 36 deletions.
9 changes: 6 additions & 3 deletions docs/config/shared-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,12 @@ Note if an inline config is provided, Vite will not search for other PostCSS con

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:

- `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/).
- `less` - [Options](https://lesscss.org/usage/#less-options).
- `styl`/`stylus` - Only [`define`](https://stylus-lang.com/docs/js.html#define-name-node) is supported, which can be passed as an object.
- `sass`/`scss`:
- 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.
- [Options (modern)](https://sass-lang.com/documentation/js-api/interfaces/stringoptions/)
- [Options (legacy)](https://sass-lang.com/documentation/js-api/interfaces/LegacyStringOptions).
- `less`: [Options](https://lesscss.org/usage/#less-options).
- `styl`/`stylus`: Only [`define`](https://stylus-lang.com/docs/js.html#define-name-node) is supported, which can be passed as an object.

**Example:**

Expand Down
8 changes: 8 additions & 0 deletions docs/guide/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ From Vite 6, even when `json.stringify: true` is set, `json.namedExports` is not

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`.

### Sass now uses modern API by default

In Vite 5, the legacy API was used by default for Sass. Vite 5.4 added support for the modern API.

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.

To migrate to the modern API, see [the Sass documentation](https://sass-lang.com/documentation/breaking-changes/legacy-js-api/).

## Advanced

There are other breaking changes which only affect few users.
Expand Down
25 changes: 9 additions & 16 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1979,8 +1979,8 @@ type PreprocessorAdditionalData =
type SassPreprocessorOptions = {
additionalData?: PreprocessorAdditionalData
} & (
| ({ api?: 'legacy' } & SassLegacyPreprocessBaseOptions)
| ({ api: 'modern' | 'modern-compiler' } & SassModernPreprocessBaseOptions)
| ({ api: 'legacy' } & SassLegacyPreprocessBaseOptions)
| ({ api?: 'modern' | 'modern-compiler' } & SassModernPreprocessBaseOptions)
)

type LessPreprocessorOptions = {
Expand Down Expand Up @@ -2469,9 +2469,9 @@ const scssProcessor = (
},
async process(environment, source, root, options, resolvers) {
const sassPackage = loadSassPackage(root)
// TODO: change default in v6
// options.api ?? sassPackage.name === "sass-embedded" ? "modern-compiler" : "modern";
const api = options.api ?? 'legacy'
const api =
options.api ??
(sassPackage.name === 'sass-embedded' ? 'modern-compiler' : 'modern')

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

const sassProcess: StylePreprocessor<SassStylePreprocessorInternalOptions>['process'] =
(environment, source, root, options, resolvers) => {
let opts: SassStylePreprocessorInternalOptions
if (options.api === 'modern' || options.api === 'modern-compiler') {
opts = { ...options, syntax: 'indented' as const }
const opts: SassStylePreprocessorInternalOptions = { ...options }
if (opts.api === 'legacy') {
opts.indentedSyntax = true
} else {
const narrowedOptions =
options as SassStylePreprocessorInternalOptions & {
api?: 'legacy'
}
opts = {
...narrowedOptions,
indentedSyntax: true,
}
opts.syntax = 'indented'
}
return scss.process(environment, source, root, opts, resolvers)
}
Expand Down
7 changes: 0 additions & 7 deletions playground/backend-integration/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,4 @@ function BackendIntegrationExample() {
export default defineConfig({
base: '/dev/',
plugins: [BackendIntegrationExample()],
css: {
preprocessorOptions: {
scss: {
silenceDeprecations: ['legacy-js-api'],
},
},
},
})
3 changes: 0 additions & 3 deletions playground/css-sourcemap/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ export default defineConfig({
}
},
},
sass: {
silenceDeprecations: ['legacy-js-api', 'import'],
},
},
},
build: {
Expand Down
1 change: 1 addition & 0 deletions playground/css/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export default defineConfig({
},
preprocessorOptions: {
scss: {
api: 'legacy',
additionalData: `$injectedColor: orange;`,
importer: [
function (url) {
Expand Down
7 changes: 0 additions & 7 deletions playground/multiple-entrypoints/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,4 @@ export default defineConfig({
},
},
},
css: {
preprocessorOptions: {
scss: {
silenceDeprecations: ['legacy-js-api'],
},
},
},
})

0 comments on commit d4e0442

Please sign in to comment.