通过 @use 而不是 @import 将 Bootstrap 导入我的 SCSS 会导致问题

Wil*_*ont 8 sass twitter-bootstrap

我的项目扩展了 Bootstrap 的响应断点。这是对我有用的入口点 SCSS 文件:

@import "~bootstrap/scss/functions";
@import "~bootstrap/scss/variables";

$magnetar-grid-breakpoints : (
    1440: 1440px,
    1600: 1600px,
    1920: 1920px,
    2560: 2560px
);
$grid-breakpoints : map-merge($grid-breakpoints, $magnetar-grid-breakpoints);

$magnetar-container-max-widths : ();

@each $key, $val in $magnetar-grid-breakpoints {
    $magnetar-container-max-widths : map-merge($magnetar-container-max-widths, ($key : 0.95 * $val));
}

$container-max-widths : map-merge($container-max-widths, $magnetar-container-max-widths);

@import "~bootstrap/scss/bootstrap";
Run Code Online (Sandbox Code Playgroud)

我刚刚开始这个项目,我已经知道必须在我的所有变量(以及 mixin 和函数)前加上“magnetar-”,以免与 Bootstrap 发生冲突,这会很快过时。因此,我想我会尝试使用新的 Sass 模块系统@use而不是@import. 这是重写的 SCSS:

@use "~bootstrap/scss/functions" as bootstrap_func;
@use "~bootstrap/scss/variables" as bootstrap_var;
@use "sass:map";

$grid-breakpoints : (
    1440: 1440px,
    1600: 1600px,
    1920: 1920px,
    2560: 2560px
);
bootstrap_var.$grid-breakpoints : map.merge(bootstrap_var.$grid-breakpoints, $grid-breakpoints);

$container-max-widths : ();

@each $key, $val in $grid-breakpoints {
    $container-max-widths : map.merge($container-max-widths, ($key : 0.95 * $val));
}

bootstrap_var.$container-max-widths : map.merge(bootstrap_var.$container-max-widths, $container-max-widths);

@use "~bootstrap/scss/bootstrap";
Run Code Online (Sandbox Code Playgroud)

但以上编译时出现此错误:

SassError: @use rules must be written before any other rules.
Run Code Online (Sandbox Code Playgroud)

它指的是最后一行。所以我想我会尝试将该行改回@import. 这导致了这个错误:

SassError: $color: theme-color("primary") is not a color.
    ?
174 ? $link-hover-color:                        darken($link-color, 15%) !default;
    ?                                           ^^^^^^^^^^^^^^^^^^^^^^^^
    ?
  node_modules\bootstrap\scss\_variables.scss 174:43  @use
Run Code Online (Sandbox Code Playgroud)

我不知道这意味着什么。有任何想法吗?

Did*_*dii 7

这是关于新 SASS 模块系统的博客文章:http : //sass.logdown.com/posts/7858341-the-module-system-is-launched

这提到了以下几点:

除了命名空间之外,@use 和@import 之间还有一些重要的区别:

  • @use 只执行一个样式表并包含它的 CSS 一次,无论该样式表被使用多少次。
  • @use 仅使名称在当前样式表中可用,而不是全局可用。
  • 名称以 - 或 _ 开头的成员对于使用 @use 的当前样式表是私有的。
  • 如果样式表包含@extend,则该扩展名仅适用于它导入的样式表,而不适用于导入它的样式表。

大胆的声明就是为什么它不与工作@use指令。所有导入的函数都不是全局可用的,这意味着该文件~bootstrap/scss/variables不知道在~bootstrap/scss/functions.

老实说,我不知道如何解决这个问题......

  • 我就是这么想的。唯一的“修复”是等待 Bootstrap 用 @use 重写。 (5认同)