Eri*_*ell 5 css sass webpack css-modules next.js
最近我开始使用 CSS 模块来使用 NextJS。为了消除重复,我想在每个样式表中自动导入带有混合、变量等的部分。
使用 GatsbyJS,我发现如何在项目的每个样式表中自动导入一些特定的 SASS 部分。设置看起来像这样:
{
resolve: `gatsby-plugin-sass`,
options: {
data: '@import "_reset.scss", "_typography.scss", "_mixins.scss", "_extends.scss", "_themes.scss";',
includePaths: [
'src/styles/partials',
],
},
},
Run Code Online (Sandbox Code Playgroud)
现在我的 next.settings.js 看起来像这样:
const path = require("path");
const withSass = require("@zeit/next-sass");
const partials = [
"_reset.scss",
"_variables.scss",
"_themes.scss",
"_typography.scss",
"_mixins.scss",
"_extends.scss",
];
module.exports = withSass({
cssModules: true,
webpack: config => {
config.resolve.alias["components"] = path.join(__dirname, "components");
return config;
},
});
Run Code Online (Sandbox Code Playgroud)
如果可能的话,如何在每个样式表中自动包含部分内容?
问题解决,感谢@eric-tan的回复:
所有部分都导入到一个“_utilty.scss”部分中,该部分包含在 sassLoaderOptions 中: -> data:导入部分本身;-> includePaths:导入的部分所在的位置。
const path = require("path");
const withSass = require("@zeit/next-sass");
module.exports = withSass({
cssModules: true,
sassLoaderOptions: {
data: '@import "_utility.scss";',
includePaths: [
path.resolve(__dirname, "styles/"),
]
},
webpack: config => {
config.resolve.alias["components"] = path.join(__dirname, "components");
return config;
},
});
Run Code Online (Sandbox Code Playgroud)
我们可以像这样使用 sass 加载器:
{
// Loads a SASS/SCSS file and include the partial files before compilation
loader: 'sass-loader',
options: {
data:
'@import "foo-partial";\n ' +
'@import "bar-partial";',
includePaths: [
path.resolve(__dirname, '../src/assets/styles')
]
}
}
Run Code Online (Sandbox Code Playgroud)
和nextSass提供了sass加载器选项,你可以尝试将它们结合到你的设置中。
| 归档时间: |
|
| 查看次数: |
2517 次 |
| 最近记录: |