我尝试通过使用Webpack的css-loader插件分别为每个组件设置样式。但是,当文件具有相同的名称(例如:'index.scss')并具有相同的属性(例如:'。container')时,它将结合两种样式,但是在这种情况下,我只需要一种样式。我该怎么解决
这是我的Webpack配置:
rules: [
{
test: /\.(sa|sc|c)ss$/,
use: [
{
loader: 'style-loader',
options: {
insertAt: 'top',
sourceMap: true
}
},
{
// Handle import/ require css
// It help to css for each component
// https://github.com/webpack-contrib/css-loader#scope
loader: 'css-loader',
options: {
sourceMap: true ,
localIdentName: '[path][name]__[local]--[hash:base64:5]',
camelCase: true,
}
},
{
loader: 'postcss-loader',
options: {
sourceMap: true,
// minimize: true,
plugins: () => [
require('postcss-flexbugs-fixes'),
// Help to generate specific css for each component
// require('postcss-modules'),
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway
],
flexbox: 'no-2009',
}),
],
}
},
{ loader: 'sass-loader', options: { sourceMap: true } },
],
},
]
示例:
src/screen1/index.tsx
const styles = require('./index.scss')
const Dashboard = ({ children, url }: IDashboardPropsIn) => {
return (
<div className={styles.body}>
{children}
</div>
)
}
我有2个文件:src/screen2/index.scss
和src/screen1/index.scss
在班级中以某种方式body--[hash:64]
拥有来自2个文件的两种样式
src/screen1/index.scss
:local {
.body {
grid-area: body;
background-color: $white;
}
}
和src/screen2/index.scss
:local {
.body {
display: flex;
}
}
body--[hash:64]
的样式将
grid-area: body;
background-color: $white;
display: flex;
但是在这种情况下,我只有一个导入文件src/screen1/index.scss
,例如
grid-area: body;
background-color: $white;
我该如何解决?
答案 0 :(得分:1)
您未在配置中指定modules
选项
{
// Handle import/ require css
// It help to css for each component
// https://github.com/webpack-contrib/css-loader#scope
loader: 'css-loader',
options: {
sourceMap: true ,
localIdentName: '[path][name]__[local]--[hash:base64:5]',
camelCase: true,
modules: true, // or 'local' | 'global' | false
}
},
在docs
中了解更多信息答案 1 :(得分:0)
将css-loader
软件包更新为0.28.11版本