SCSS文件部分编译成CSS

问题描述

我使用扩展名为“live SASS Compiler”的lates Vscode。我想使用 bootstrap,所以我在 html 中链接一个 style.css 文件,它应该包含所有已编译的 bootstrap.css 代码包括我的)。为了自定义 Bootstrap 主题我有一个 style.scss 文件,它导入本地 bootstrap.scss 文件。现在在 style.scss 中导入本地 bootstrap.scss 文件后,我将 scss 文件编译/转换(观察)为 style.css 文件,该文件有效但部分有效,只有我添加的额外 scss 代码部分被转换为 css,排除所有 bootstrap.css 代码。这就是为什么我的 html 没有使用 style.css 进行引导。

为了简单起见,我提到了代码部分。 这是 html 文件

<head>
  <Meta charset="UTF-8" />
  <Meta name="viewport" content="width=device-width,initial-scale=1.0" />
  <title>Bootstrap Site</title>
  <link rel="stylesheet" href="/src/css/style.css" />
</head>

这是 style.scss 文件,我观看/转换这个 style.scss 到 style.css

@import url(/node_modules/bootstrap/scss/bootstrap.scss);
$bg-color: rgb(221,204,117);
$font-color: rgb(170,12,12);
body {
  background-color: $bg-color;
  color: $font-colo; 
}

这是转译后的 style.css 文件(仅转译了部分代码):

@import url(/node_modules/bootstrap/scss/bootstrap.scss);
body {
  background-color: #ddcc75;
  color: #aa0c0c;
}

如您所见,转换后的 style.css 文件没有所有引导程序 css 代码,这就是 html 未与引导程序链接的原因(引导程序仅在我将其单独链接到另一个链接标记中时才起作用)。如何使其与 style.css 一起用于引导程序主题自定义

注意它的NPM项目,我用NPM下载bootstrap。

解决方法

这就是我让它工作的方式。我将 scss 文件的代码更改为更简单的代码,并确保 bootstrap.scss 的位置正确。我发现的一个问题是,无论您做什么,最后都必须 @import 引导程序文件。我不知道如何只导入必要的文件。

// 2. Include any default variable overrides here
$body-color: rgb(238,79,30);
$body-bg : rgb(230,224,224);

// // 3. Include remainder of required Bootstrap stylesheets
@import  "../../node_modules/bootstrap/scss/functions";
@import "../../node_modules/bootstrap/scss/variables";


$custom-theme-colors: (
"altlight": rgb(189,163,194),"altdark" : rgb(170,90,163)
);

$theme-colors : map-merge($custom-theme-colors,$theme-colors );

@import "../../node_modules/bootstrap/scss/bootstrap";