简体   繁体   English

如何使用导入全局 sass 变量与 create-react-app(弹出)

[英]How to use import global sass variables with create-react-app (ejected)

Please see this repo for further information and reproduction.请参阅此 repo以获取更多信息和复制。

I am using an ejected create-react-app and I want to be able to use global sass variables in an individual stylesheet without having to import the stylesheet.我正在使用弹出的 create-react-app 并且我希望能够在单个样式表中使用全局 sass 变量,而无需导入样式表。 I think using sass-resources-loader is what I should use (although if there is a better way of doing this, please let me know).我认为使用sass-resources-loader是我应该使用的(尽管如果有更好的方法,请告诉我)。 I have updated the create-react-app ejected webpack config here , but whenever I run yarn start , I still get the complaint:我在这里更新了 create-react-appjected webpack 配置,但是每当我运行yarn start ,我仍然会收到投诉:

SassError: Undefined variable: "$boxSize". SassError:未定义的变量:“$boxSize”。 on line 5 of src/SampleComponent/sampleComponent.scss, height: $boxSize;在 src/SampleComponent/sampleComponent.scss 的第 5 行,高度:$boxSize;

Is there something that I am doing wrong in trying to use sass-resources-loader or should I be using something different instead?在尝试使用sass-resources-loader时我做错了什么还是应该使用不同的东西? Thanks, and here are some file snippets below for more context:谢谢,下面是一些文件片段以获取更多上下文:

file structure:文件结构:

src/
  - SampleComponent/
    - SampleComponent.js
    - SampleComponent.scss
  - styles/
    - sizes.scss
  - index.js  

SampleComponent.js:示例组件.js:

import * as styles from './sampleComponent.scss';

const SampleComponent = () => (
  <div className={styles.div}>
    <h3>hello world</h3>
  </div>
);

export default SampleComponent;

sampleComponent.scss:示例组件.scss:

.div {
  align-items: center;
  background: pink;
  display: flex;
  height: $boxSize;
  justify-content: center;
  margin: 5rem;
  width: $boxSize;
}

sizes.scss:尺寸.scss:

$boxSize: 30rem;

Use the sass-loader library, I use it that way and it works very well.使用 sass-loader 库,我就是这样使用的,而且效果很好。

{
  loader: 'sass-loader',
  options: {
    sourceMap: true,
    additionalData: `
        @import "sources/scss/modules/_config.scss";
        @import "sources/scss/modules/_global.scss";
      `
  },
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM