重大变化:@import and global built-in functions
最初,Sass 使用 @import
规则通过单个全局命名空间加载其他文件,所有内置函数也可全局使用。由于模块系统(@use
和 @forward
规则)已经推出多年,我们现已弃用 Sass @import
规则和全局内置函数。
@import
会导致许多问题,需要手动为 Sass 成员指定命名空间以避免冲突,当多次导入同一个文件时会减慢编译速度,并且使人类和工具都很难分辨给定变量、混合或函数来自何处。
¥@import
causes numerous problems, requiring Sass members to be manually
namespaced to avoid conflicts, slowing down compilation when the same file is
imported more than once, and making it very difficult for both humans and tools
to tell where a given variable, mixin, or function comes from.
模块系统修复了这些问题,并使 Sass 的模块化与其他现代语言的最佳实践相媲美,但只要 @import
仍保留在语言中,我们就无法获得它的全部好处。
¥The module system fixes these problems and brings Sass’s modularity up to par
with the best practices of other modern languages, but we can’t get the full
benefits of it while @import
remains in the language.
@import
现在从 Dart Sass 1.80.0 开始已弃用。此外,我们还将弃用 sass:
模块中可用的 Sass 内置函数的全局版本。
¥@import
is now deprecated as of Dart Sass 1.80.0. Additionally, we’re also
deprecating the global versions of Sass built-in functions that are available
in sass:
modules.
过渡期过渡期 permalink
¥Transition Period
- Dart Sass
- since 1.80.0
- LibSass
- ✗
- Ruby Sass
- ✗
Sass @import
规则和全局内置函数调用现在会发出弃用警告。虽然 Dart Sass 2.0.0 即将发布,其中包含各种较小的重大更改,但我们预计直到 Dart Sass 3.0.0 才会删除 Sass @import
规则或全局内置函数,而 Dart Sass 3.0.0 将在 Dart Sass 1.80.0 发布两年后发布。
¥Sass @import
rules and global built-in function calls now emit deprecation
warnings. While Dart Sass 2.0.0 will be released soon with various smaller
breaking changes, we don’t expect to remove Sass @import
rules or global
built-in functions until Dart Sass 3.0.0, which will be released no sooner than
two years after Dart Sass 1.80.0.
最终,所有 @import
规则都将被视为 纯 CSS @import
,可能在中间时期之后,任何曾经是 Sass @import
的东西都会引发错误。
¥Eventually, all @import
rules will be treated as plain CSS @import
s,
likely after an intermediate period where anything that used to be a Sass
@import
throws an error.
自动迁移自动迁移 permalink
¥Automatic Migration
你可以使用 Sass 迁移器 自动更新样式表以使用模块系统。
¥You can use the Sass migrator to automatically update your stylesheets to use the module system.
$ npm install -g sass-migrator
$ sass-migrator module --migrate-deps your-entrypoint.scss
如果你想要迁移全局内置函数,但尚未准备好完全迁移 @import
规则,则可以传递 --built-in-only
标志来迁移函数,同时保留 @import
规则。
¥If you want to migrate away from global built-in functions, but aren’t yet
ready to fully migrate your @import
rules, you can pass the --built-in-only
flag to migrate the functions while leaving @import
rules as-is.
迁移秘诀迁移秘诀 permalink
¥Migration Recipes
嵌套导入嵌套导入 permalink
¥Nested Imports
虽然 @import
可以在 CSS 规则中使用,但 @use
必须写在文件的顶层(这是因为每个 @use
d 模块的 CSS 只包含在输出中一次,因此在嵌套上下文和顶层都允许它是没有意义的)。有两种方法可以将嵌套的 @import
迁移到模块系统:
¥While @import
can be used within CSS rules, @use
has to be written at the
top level of a file (this is because each @use
d module’s CSS is only included
in the output once, so it wouldn’t make sense to allow it both in a nested
context and the top level). There are two ways to migrate nested @import
s to
the module system:
-
推荐的方法需要更多的前期工作,即将嵌套模块发出的所有 CSS 封装在 混入 中,并在嵌套上下文中
@include
那些 mixin。这与大多数其他编程语言的工作方式一致,其中每个文件都定义一个由使用它的文件调用的函数或类,并且它非常清楚地表明了你希望如何使用该文件。它还使添加配置变得更容易,因为你只需将参数甚至@content
块 传递给 mixin 即可。¥The recommended way, which requires a little more up-front effort, is to wrap all the CSS emitted by your nested modules in mixins and
@include
those mixins in the nested context. This matches the way most other programming languages work, where each file defines a function or class that gets called by the files that use it, and it makes it very clear exactly how you expect that file to be used. It also makes it easier to add configuration, since you can just pass parameters or even@content
blocks to the mixin. -
更直接的翻译是使用
meta.load-css()
混入 直接加载你想要使用模块的 CSS。当你无法控制要加载的文件以创建混合封装器时,这是合适的。请注意,meta.load-css()
在进行任何嵌套之前会完全编译 CSS,因此任何 父选择器 都不会 "see"meta.load-css()
调用之外的规则。¥A more direct translation is to use the
meta.load-css()
mixin to directly load the module’s CSS where you want to use it. This is appropriate when you don’t have control over the file you’re loading to create a mixin wrapper. Note thatmeta.load-css()
fully compiles the CSS before it does any nesting, so any parent selectors won’t "see" the rules outside themeta.load-css()
call.
配置主题配置主题 permalink
¥Configured Themes
人们有时使用 @import
的一种模式是拥有一个充满部分的组件库,这些部分都使用相同的变量而不显式加载它们,然后有几个不同的 "theme" 入口点,为这些变量定义不同的值以提供不同的视觉主题。它们可以直接定义变量,也可以覆盖基本主题部分的默认值。作为一个简化示例:
¥A pattern that people sometimes use with @import
is to have a component
library full of partials that all use the same variables without explicitly
loading them, and then having several different "theme" entrypoints that define
different values for those variables to provide different visual themes. They
may either define the variables directly, or override defaults of a base theme
partial. As a simplified example:
SCSS Syntax
// components/_button.scss
button {
color: $text-color;
background-color: $background-color;
}
// _theme.scss
$text-color: black !default;
$background-color: white !default;
// dark.scss
$text-color: white;
$background-color: white;
@import "theme";
@import "components/button";
// More components are usually imported here.
Sass Syntax
// components/_button.scss
button
color: $text-color
background-color: $background-color
// _theme.scss
$text-color: black
$background-color: white
// dark.scss
$text-color: white
$background-color: white
@import "theme"
@import "components/button"
// More components are usually imported here.
CSS Output
button {
color: white;
background-color: black;
}
在模块系统中,组件部分需要明确引用它们引用的变量。但这并不意味着这种主题不起作用!因为多次对同一模块进行 @use
总是使用相同的配置,如果你在入口点配置一次,所有其他用途都会看到该配置:
¥In the module system, the component partials need to explicitly reference the
variables they refer to. But this doesn’t mean this kind of theming doesn’t
work! Because @use
-ing the same module multiple times always uses the same
configuration, if you configure it once in the entrypoint and all other uses
will see that configuration:
SCSS Syntax
// components/_button.scss
@use "../theme";
button {
color: theme.$text-color;
background-color: theme.$background-color;
}
// _theme.scss
$text-color: black !default;
$background-color: white !default;
// dark.scss
@use "theme" with (
$text-color: white,
$background-color: black,
);
@use "components/button";
// More components are usually imported here.
Sass Syntax
// components/_button.scss
@use "../theme"
button
color: theme.$text-color
background-color: theme.$background-color
// _theme.scss
$text-color: black !default
$background-color: white !default
// dark.scss
@use "theme" with (
$text-color: white,
$background-color: black,
)
@use "components/button"
// More components are usually imported here.
CSS Output
button {
color: white;
background-color: black;
}
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.
注意:虽然 @import
和全局内置函数的弃用将一起发布,并且我们预计这两个功能也将同时被删除(在 Dart Sass 3.0.0 中),但就 API 而言,它们被视为单独的弃用。如果你希望同时消除 @import
弃用警告和全局内置弃用警告,则需要将 import
和 global-builtin
都传递给 --silence-deprecation
/silenceDeprecations
。
¥Note: While the deprecations for @import
and global built-ins are being
released together and we expect both features to be removed simultaneously
as well (in Dart Sass 3.0.0), they are considered separate deprecations for the
purpose of the API. If you wish to silence both @import
deprecation warnings
and global built-in deprecation warnings, you’ll need to pass both import
and global-builtin
to --silence-deprecation
/silenceDeprecations
.