英文:
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.jsx或app.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
文件中。
例如,如果你使用 webpack 和 sass-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: ...
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论