为什么文件名相同时模块样式重叠

时间:2019-01-22 09:13:50

标签: css reactjs webpack sass styles

我尝试通过使用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.scsssrc/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;

我该如何解决?

2 个答案:

答案 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版本