重大变化:私有配置

Sass 引入新的模块系统时,也引入了私有变量的概念,这些变量只能在模块内部可见或修改。但存在一个意外的漏洞:这些变量仍然可以被配置。

可以编写 @use "module" with ($-private: value) 并影响模块私有变量的值。这违背了语言的预期行为,也可能违背了模块作者的意图。为了弥补这个漏洞,我们正在努力使配置私有变量成为错误。

¥It’s possible to write @use "module" with ($-private: value) and affect the value of a module’s private variable. This goes against the intended behavior of the language, and likely against the module author’s intentions as well. To close this loophole, we’re moving towards making it an error to ever configure a private variable.

我们仍然计划允许使用 !default 声明私有变量,因为只有当变量当前为 null(类似于 JavaScript 中的 ??= 赋值)时,这仍然是一种有效的赋值方式。

¥We still plan to allow private variables to be declared with !default, because this remains a useful way to assign a value only if the variable is currently null (similar to ??= assignment in JavaScript).

⚠️ Heads up!

任何以 -_ 开头的变量都被视为私有变量,包括以 -- 开头的变量。因此,我们不鼓励对 Sass 变量使用 CSS 自定义属性样式的名称。

¥Any variable whose name starts with - or _ is considered private, including variables whose names start with --. For this reason, we discourage the use of CSS custom property-style names for Sass variables.

阶段 1阶段 1 permalink

¥Phase 1

兼容性:
Dart Sass
since 1.92.0
LibSass
Ruby Sass

目前,如果在配置中使用私有变量名称,Dart Sass 会发出弃用警告,但如果模块使用 !default 声明该变量,Dart Sass 仍然允许配置该变量。

¥Currently, Dart Sass emits a deprecation warning if you use a private variable name in a configuration, but it will still allow that variable to be configured if the module declares it with !default.

要修复任何违规行为,请通过删除所有前导 -_ 字符,将所有要配置的变量更改为不再是私有的。

¥To fix any violations, change all variables that are intended to be configured to no longer be private by removing all leading - or _ characters.

Can I Silence the Warnings?Can I Silence the Warnings? permalink

Sass provides a powerful suite of options for managing which deprecation warnings you see and when.

Terse and Verbose ModeTerse and Verbose Mode permalink

By default, Sass runs in terse mode, where it will only print each type of deprecation warning five times before it silences additional warnings. This helps ensure that users know when they need to be aware of an upcoming breaking change without creating an overwhelming amount of console noise.

If you run Sass in verbose mode instead, it will print every deprecation warning it encounters. This can be useful for tracking the remaining work to be done when fixing deprecations. You can enable verbose mode using the --verbose flag on the command line, or the verbose option in the JavaScript API.

⚠️ Heads up!

When running from the JS API, Sass doesn’t share any information across compilations, so by default it’ll print five warnings for each stylesheet that’s compiled. However, you can fix this by writing (or asking the author of your favorite framework’s Sass plugin to write) a custom Logger that only prints five errors per deprecation and can be shared across multiple compilations.

Silencing Deprecations in DependenciesSilencing Deprecations in Dependencies permalink

Sometimes, your dependencies have deprecation warnings that you can’t do anything about. You can silence deprecation warnings from dependencies while still printing them for your app using the --quiet-deps flag on the command line, or the quietDeps option in the JavaScript API.

For the purposes of this flag, a "dependency" is any stylesheet that’s not just a series of relative loads from the entrypoint stylesheet. This means anything that comes from a load path, and most stylesheets loaded through custom importers.

Silencing Specific DeprecationsSilencing Specific Deprecations permalink

If you know that one particular deprecation isn’t a problem for you, you can silence warnings for that specific deprecation using the --silence-deprecation flag on the command line, or the silenceDeprecations option in the JavaScript API.

阶段 2阶段 2 permalink

¥Phase 2

兼容性:
Dart Sass
LibSass
Ruby Sass

在 Dart Sass 2.0.0 中,在配置中包含私有变量将会导致错误。

¥In Dart Sass 2.0.0, including a private variable in a configuration will be an error.