英文:
How to correctly access nested aliases with sass/node-sass in create-react-app
问题
我正在尝试将我的 SCSS 部分拆分为多个文件,并将其聚合到一个文件中,然后根据需要访问变量。我有以下文件夹结构:
create-react-app/src
│
└───Styles
│ │
│ └───Tokens
│ │ │ _Colors.scss
│ │ │ _Tokens.scss
│ │ _Base.scss
在 _Colors.scss
中,我有一个简单的变量:$primary-color: red;
。
在 _Tokens.scss
中,我使用 @use
规则导入我的部分并为其提供别名:@use "./Colors.scss" as colors;
。
在我的 _Base.scss
中,我导入了我的 Tokens.scss
并为其提供别名:@use "Styles/Tokens/Tokens" as tokens;
。然后我尝试访问嵌套的别名/命名空间,例如:
// _Base.scss
@use "Styles/Tokens/Tokens" as tokens;
body {
color: tokens.colors.$primary-color; // Linter has an issue with .colors
}
我遇到了一个 linter 错误:identifier or variable expectedscss(css-idorvarexpected)
。React 也报错:
Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
SassError: expected "(".
╷
10 │ color: tokens.colors.$primary-color;
│ ^
在这一点上,我感到困惑,我已经尝试了几个小时在 Google 上查找,但找不到任何信息。希望能得到帮助,谢谢!如果您需要更多信息,请告诉我!
英文:
I am trying to break my scss partials into multiple files and aggregate it into one file and access variables accordingly. I have this folder structure:
create-react-app/src
│
└───Styles
│ │
│ └───Tokens
│ | │ _Colors.scss
│ | │ _Tokens.scss
│ | _Base.scss
Inside _Colors.scss
, I have a simple variable: $primary-color: red;
.
// _Colors.scss
$primary-color: red;
Inside _Tokens.scss
I use the @use
rule to import my partial and give it an alias: @use "./Colors.scss" as colors;
.
// _Tokens.scss
@use "./Colors" as colors;
In my _Base.scss
I am importing my Tokens.scss
and giving that an alias as well: @use "Styles/Tokens/Tokens" as tokens;
. I then try to access the nested alias/namespace, eg:
// _Base.scss
@use "Styles/Tokens/Tokens" as tokens;
body {
color: tokens.colors.$primary-color; // Linter has an issue with .colors
}
I am confronted with a linter error: identifier or variable expectedscss(css-idorvarexpected).
React also spits out an error:
Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
SassError: expected "(".
╷
10 │ color: tokens.colors.$primary-color;
│ ^
Confused on what to do at this point, I've tried for a few hours poking around Google but can't find anything. Help would be appreciated, thank you! Let me know if you need any more information!
答案1
得分: 1
你尝试过将 @use "Styles/Tokens/Tokens" as Tokens;
中的 "Tokens" 改成小写吗?因为在它下面的 SCSS 中,color: tokens.colors.$primary-color;
中的 "tokens" 是小写的。
英文:
Have you tried writing "Tokens" in @use "Styles/Tokens/Tokens" as Tokens;
lowercase? Because you have it lowercase in the scss below it, color: tokens.colors.$primary-color;
答案2
得分: 1
我使用@forward
语法解决了这个问题。
从文档中:
" @forward规则加载一个Sass样式表,并在使用@use规则加载样式表时使其混合、函数和变量可用。这使得可以将Sass库组织在多个文件中,同时允许用户加载单个入口文件。"
这允许您导入一个单个文件,例如_tokens.scss,而不是导入多个文件。
例如,我们有一个名为tokens/的目录。在tokens/内,我们有多个不同的部分文件,用于保存许多不同的变量,基于它们的类别(例如颜色、字体)。最后,我们有一个单独的_tokens.scss部分文件,我们将使用它来"转发"所有单独的部分文件,这样我们只需要导入一个部分,而不是每个单独的部分文件。
看一个例子:
tokens/
_colors.scss
_fonts.scss
_tokens.scss
在_colors.scss
中:
$brand: rebeccapurple;
现在,我们可以在我们的_tokens.scss
中转发它并根据需要进行命名空间:
// _tokens.scss
// 我们在_colors.scss内部对变量进行命名空间,使用colors-*表示我们需要在_colors.scss内部为变量名称添加前缀,例如$colors-VARIABLE,在这种情况下是$colors-brand
@forward "./colors" as colors-*;
现在,我们只需要在可能需要我们的令牌的任何Sass文件中调用_tokens.scss
。假设我们有一个_sections.scss
部分文件,其中包含我们的部分组件:
// _sections.scss
@use "path/to/tokens" as tokens; // 命名为tokens
// 我们可以在_colors.scss内调用$brand变量,如下所示:
.section-about {
// 请注意"$colors-"前缀。这是我在_tokens.scss中如何命名空间它的方式。
color: tokens.$colors-brand;
}
参见:https://sass-lang.com/documentation/at-rules/forward
英文:
I solved this with the @forward
syntax.
From the docs:
"The @forward rule loads a Sass stylesheet and makes its mixins, functions, and variables available when your stylesheet is loaded with the @use rule. It makes it possible to organize Sass libraries across many files, while allowing their users to load a single entrypoint file."
This allows you to import a single file, eg. _tokens.scss, rather than importing multiple files.
For example, we have a directory called tokens/. Inside tokens/, we have multiple different partial files to hold many different variables, based off their category (eg. colors, fonts). Lastly, we have a single _tokens.scss partial file which we will use to "forward" all of our separate partial files so that we only need to import one partial, instead of every single partial file.
Take a look at this example:
tokens/
_colors.scss
_fonts.scss
_tokens.scss
Inside _colors.scss
:
$brand: rebeccapurple;
Now, we can forward this in our _tokens.scss
and namespace it as needed:
// _tokens.scss
// we namespace our variables inside _colors.scss as colors-* meaning we need to prefix our variable names inside _colors.scss as $colors-VARIABLE, in this case $colors-brand
@forward "./colors" as colors-*;
Now, we only need to call _tokens.scss
in any sass file that may need our tokens. Let's say we have a _sections.scss
partial file that holds our section components:
// _sections.scss
@use "path/to/tokens" as tokens; // namespaced as tokens
// we can call our $brand variable inside _colors.scss as follows:
.section-about {
// notice the "$colors-" prefix. This is how I namespaced it in _tokens.scss.
color: tokens.$colors-brand;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论