重大变化:斜线作为除法
Sass 目前在某些上下文中将 /
视为除法运算,在其他上下文中将 /
视为分隔符。这使得 Sass 用户很难判断任何给定的 /
的含义,并且很难使用使用 /
作为分隔符的新 CSS 功能。
- Dart Sass
- partial
- LibSass
- ✗
- Ruby Sass
- ✗
今天,Sass 使用 复杂的启发法 来确定 /
是否应该被视为除法或分隔符。即使这样,作为分隔符,它也只会生成一个不带引号的字符串,很难从 Sass 内部检查。随着越来越多的 CSS 功能(例如 CSS 网格 和 新的 rgb()
和 hsl()
语法)使用 /
作为分隔符,这对于 Sass 用户来说变得越来越痛苦。
¥Today, Sass uses complex heuristics to figure out whether a /
should be
treated as division or a separator. Even then, as a separator it just produces
an unquoted string that’s difficult to inspect from within Sass. As more and
more CSS features like CSS Grid and the new rgb()
and hsl()
syntax
use /
as a separator, this is becoming more and more painful to Sass users.
因为 Sass 是 CSS 超集,所以我们通过将 /
重新定义为仅分隔符来匹配 CSS 语法。/
将被视为一种新型列表分隔符,类似于当今 ,
的工作方式。除法将改为使用新的 math.div()
函数编写。该函数的行为与 /
今天的行为完全相同。
¥Because Sass is a CSS superset, we’re matching CSS’s syntax by redefining /
to
be only a separator. /
will be treated as a new type of list separator,
similar to how ,
works today. Division will instead be written using the new
math.div()
function. This function will behave exactly the same as /
does
today.
此弃用不会影响 /
在 calc()
表达式中的使用。
¥This deprecation does not affect uses of /
inside calc()
expressions.
SCSS Syntax
@use "sass:math";
// Future Sass, doesn't work yet!
.item3 {
$row: span math.div(6, 2) / 7; // A two-element slash-separated list.
grid-row: $row;
}
Sass Syntax
@use "sass:math"
// Future Sass, doesn't work yet!
.item3
$row: span math.div(6, 2) / 7 // A two-element slash-separated list.
grid-row: $row
CSS Output
.item3 {
grid-row: span 3 / 7;
}
过渡期过渡期 permalink
¥Transition Period
- Dart Sass
- since 1.33.0
- LibSass
- ✗
- Ruby Sass
- ✗
为了简化过渡,我们首先添加 math.div()
函数。/
运算符目前仍然执行除法,但执行此操作时还会打印一条弃用警告。用户应将所有分区切换为使用 math.div()
。
¥To ease the transition, we’ve begun by adding the math.div()
function. The /
operator still does division for now, but it also prints a deprecation warning
when it does so. Users should switch all division to use math.div()
instead.
💡 Fun fact:
Remember, you can silence deprecation warnings from libraries you don’t
control! If you’re using the command-line interface you can pass the
--quiet-deps
flag, and if you’re using the JavaScript API you can set the
quietDeps
option to true
.
SCSS Syntax
@use "sass:math";
// WRONG, will not work in future Sass versions.
@debug (12px/4px); // 3
// RIGHT, will work in future Sass versions.
@debug math.div(12px, 4px); // 3
Sass Syntax
@use "sass:math"
// WRONG, will not work in future Sass versions.
@debug (12px/4px) // 3
// RIGHT, will work in future Sass versions.
@debug math.div(12px, 4px) // 3
过渡期间也将提供斜杠分隔的列表。由于还无法使用 /
创建它们,因此将添加 list.slash()
函数来创建它们。你还可以将 "slash"
作为 $separator
传递给 list.join()
函数 和 list.append()
函数。
¥Slash-separated lists will also be available in the transition period. Because
they can’t be created with /
yet, the list.slash()
function will be added to
create them. You will also be able to pass "slash"
as the $separator
to the
list.join()
function and the list.append()
function.
SCSS Syntax
@use "sass:list";
@use "sass:math";
.item3 {
$row: list.slash(span math.div(6, 2), 7);
grid-row: $row;
}
Sass Syntax
@use "sass:list"
@use "sass:math"
.item3
$row: list.slash(span math.div(6, 2), 7)
grid-row: $row
CSS Output
.item3 {
grid-row: span 3 / 7;
}
- Dart Sass
- since 1.40.0
- LibSass
- ✗
- Ruby Sass
- ✗
或者,用户可以将除法运算封装在 calc()
表达式中,Sass 会将其简化为单个数字。
¥Alternatively, users can wrap division operations inside a calc()
expression,
which Sass will simplify to a single number.
SCSS Syntax
// WRONG, will not work in future Sass versions.
@debug (12px/4px); // 3
// RIGHT, will work in future Sass versions.
@debug calc(12px / 4px); // 3
Sass Syntax
// WRONG, will not work in future Sass versions.
@debug (12px/4px) // 3
// RIGHT, will work in future Sass versions.
@debug calc(12px / 4px) // 3
自动迁移自动迁移 permalink
¥Automatic Migration
你可以使用 Sass 迁移器 自动更新样式表以使用 math.div()
和 list.slash()
。
¥You can use the Sass migrator to automatically update your stylesheets to
use math.div()
and list.slash()
.
$ npm install -g sass-migrator
$ sass-migrator division **/*.scss
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.