在整个React应用程序中只有一个基本的SCSS @use导入?

huangapple go评论81阅读模式
英文:

Having only one base SCSS @use import in entire React application?

问题

我的问题很简单。我喜欢在React中使用SCSS,但我一直避免使用变量/混合等,因为对我来说,在重构某些内容后修复所有导入问题真是一场噩梦(我为每个组件单独创建一个scss文件)。

后来我发现我可以在React导入中轻松创建绝对路径,例如:

import "src/scss/variables"

现在我对学习SCSS所能提供的一切感到非常兴奋。

所以我的问题是,我是否可以创建一个global.scss文件,然后在导入不同的SCSS文件时只使用这个文件。例如,global.scss可能如下所示:

@use "variables"
@use "mixins"
@use "functions"

@forward "variables"
@forward "mixins"
@forward "functions"

然后在components.scss中,我只需导入:

@use "src/scss/global"


.test {
  background-color: global.$primary-color;
}

另一个问题是,是否有可能在main.jsxapp.jsx中只导入全局样式,然后在所有文件中自动使用它们?

英文:

my question is pretty simple. I like using SCSS in React but I was avoiding variables/mixins etc. because for me it was just nightmare to fix all imports after reconstruction of something (I have one scss file for every component separately).

Than I figure out I can easily make absolute paths in React imports for example:

import "src/scss/variables"

Now I am again very excited to learn all good things SCSS can offer.

So my question is if I can create one global.scss file and use only this file in whole app when importing styles to different SCSS files. For example global.scss would look like this:

@use "variables"
@use "mixins"
@use "functions"

@forward "variables"
@forward "mixins"
@forward "functions"

And in components.scss I would just import:

@use "src/scss/global"


.test {
  background-color: global.$primary-color;
}

Another question is if there is any possibility that globals are just imported in main.jsx pr app.jsx and than already used in all files automatically?

答案1

得分: 1

可以将所有变量、混合和函数放在一个单独的文件中是完全可能的。你分享的示例几乎正确,但你只需要使用 @forward 而不是 @use

@forward "variables";
@forward "mixins";
@forward "functions";

确保这三个文件都是 partials

.jsx 中导入这个 global.scss 文件是没有意义的,因为它不包含任何样式。但是,你可以配置你的打包工具自动将它导入到所有 .scss 文件中。

例如,如果你使用 webpacksass-loader,你可以使用选项 additionalData

options: {
  additionalData: "@use 'src/scss/global';"
}

如果出现关于 @import loop 的错误,你需要排除全局样式文件,以防止它们被自动导入:

options: {
  additionalData: (content, loaderContext) => {
    const { resourcePath, rootContext } = loaderContext;

    // 假设你的全局文件在这个文件夹中
    const styleGlobalPathRegex = /^src\\scss\\./;

    const relativeFilePath = path.relative(rootContext, resourcePath);
    const isAGlobalStyleFile = relativeFilePath.match(styleGlobalPathRegex);

    return isAGlobalStyleFile ? content : "@use 'src/scss/global';" + content;
  }
}

如果你使用 Vite,你可以使用 css.preprocessorOptions 来添加它:

css: {
  preprocessorOptions: {
    scss: {
      additionalData: ...
    }
  }
}
英文:

It is indeed possible to have a single file for all your variables, mixins and functions. The example you shared is almost right, but you only need to use @forward and not @use:

@forward "variables";
@forward "mixins";
@forward "functions";

And make sure these three files are partials.

Importing this global.scss file in the .jsx is useless, since it doesn't contain any styles. However, what you can do is to configure your bundler to import it automatically into all of your .scss files.

For example, if you use webpack and sass-loader, you can use the option additionalData:

options: {
  additionalData: "@use \"src/scss/global\";"
}

If you get an error about @import loop, you need to exclude the global style files so they don't get the automatic import:

options: {
  additionalData: (content, loaderContext) => {
    const { resourcePath, rootContext } = loaderContext;

    // Assuming your global files are in this folder
    const styleGlobalPathRegex = /^src\\scss\\.*/;

    const relativeFilePath = path.relative(rootContext, resourcePath);
    const isAGlobalStyleFile = relativeFilePath.match(styleGlobalPathRegex);

    return isAGlobalStyleFile ? content : "@use \"src/scss/global\";" + content;
  };
}

If you use Vite, you can add it with css.preprocessorOptions:

css: {
  preprocessorOptions: {
    scss: {
      additionalData: ...
    }
  }
}

huangapple
  • 本文由 发表于 2023年6月16日 14:48:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定