webpack-contrib / mini-css-extract-plugin

Lightweight CSS extraction plugin
MIT License
4.66k stars 388 forks source link

Order problem #555

Open alexander-akait opened 4 years ago

alexander-akait commented 4 years ago

Expected Behavior

After dynamically importing component, which composes css class, that an already present component also composes, the existing component should preserve its styles.

Screenshot from 2020-04-06 11 22 27

Actual Behavior

After dynamically importing component B, components C styles get overriden.

Screenshot from 2020-04-06 11 22 52

Code

// index.module.css

.test {
    background-color: red;
    padding: 30px;
}
// b.module.css

.test2 {
    composes: test from '../index.module.css';
    background-color: blue;
}
// c.moodule.css

.test3 {
    composes: test from '../index.module.css';
    background-color: green;
}

Result:

<head>
    <link rel="stylesheet" type="text/css" href="chunk-2.css">
    <script charset="utf-8" src="2.main.js"></script>
    <link rel="stylesheet" type="text/css" href="chunk-1.css">
    <script charset="utf-8" src="1.main.js"></script>
</head>
// chunk-2.css

._2D6NmF3oNXamuyZ2z-z6-2 {
  background-color: red;
  padding: 30px;
}

._2vSpwQvfEWT-h3qiyUMFPO {
  background-color: green;
}
// chunk-1.css

._2D6NmF3oNXamuyZ2z-z6-2 {
  background-color: red;
  padding: 30px;
}

._3IkLTxcAE-k8TWXxjYQrik {
  background-color: blue;
}

Webpack config:

// webpack.config.js
...
    module: {
        rules: [
            {
                test: /\.module\.css$/i,
                use: [
                    { loader: MiniCssExtractPlugin.loader },
                    {
                        loader: 'css-loader',
                        options: {
                            modules: true,
                            importLoaders: 1,
                        },
                    },
                ],
            },
        ],
    },
...

How Do We Reproduce?

For reproduction, see this minimal repo: https://github.com/OHUSAR/css-modules-chunk-specificity-bug

Originally posted by @OHUSAR in https://github.com/webpack-contrib/css-loader/issues/997#issuecomment-609688409

barak007 commented 4 years ago

When trying to solve this issue maybe take a look at #530

Also the real problem is that css order should not depended only on import order because it can create cases that you cant satisfy all constraints.

x.js imports a.css then b.css y.js imports b.css then a.css z.js imports x.js and y.js

Chimochikhin commented 2 years ago

Any news?

OliverJAsh commented 5 months ago

I made a proposal in css-loader: https://github.com/webpack-contrib/css-loader/issues/1602