Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't work with css-modules #519

Closed
TotooriaHyperion opened this issue Mar 24, 2020 · 7 comments
Closed

Don't work with css-modules #519

TotooriaHyperion opened this issue Mar 24, 2020 · 7 comments

Comments

@TotooriaHyperion
Copy link

TotooriaHyperion commented Mar 24, 2020

  • Operating System: macOS Mojave 10.14.6
  • Node Version: 12.13.1
  • NPM Version: 6.14.2
  • webpack Version: 4.42.0
  • mini-css-extract-plugin Version: 0.9.0

Expected Behavior

Should work with css-module

Actual Behavior

If any css file includes a css-module export, then the HMR breaks.
a.css

:local(.aaa) {
  background-color: pink; // hmr warns: do not know how to reload themselves, and style won't update
}
.aa {
  background-color: khaki; // hmr warns: do not know how to reload themselves, and style won't update
}

b.css

.bbb {
  background-color: skyblue; // this works, no warning, style updated.
}

Those style without css-module will work. scss file is the same behaviour with css.

Code

// webpack.config.js
// If your code blocks are over 20 lines, please paste a link to a gist
// (https://gist.github.com).
// additional code, HEY YO remove this block if you don't need it
        {
          test: /\.s?css$/,
          exclude: /node_modules/,
          use: [
            {
              loader: MiniCssExtractPlugin.loader,
              options: {
                hmr: !isProd,
              },
            },
            {
              loader: "css-loader",
              options: {
                importLoaders: 2,
                modules: {
                  mode: "global",
                  localIdentName: isProd
                    ? "_[hash:base64]"
                    : "[path][name]__[local]",
                },
              },
            },
            {
              loader: "postcss-loader",
              options: {
                plugins: [
                  require("autoprefixer")(),
                  require("postcss-flexbugs-fixes")(),
                ],
              },
            },
            { loader: "sass-loader" },
          ],
        },

How Do We Reproduce?

just add css-modules into the advanced example shown in readme.md.
Then those css/scss file with a :local() in it will fail to hot reload.
Those css/scss file without any :local() works fine.

@alexander-akait
Copy link
Member

Please create reproducible test repo

@TotooriaHyperion
Copy link
Author

Please create reproducible test repo

reproduce: https://github.com/TotooriaHyperion/mini-css-extract-plugin-bug

@TotooriaHyperion
Copy link
Author

If style inside :local() block is empty, there would not be a css module and then hmr works.
But if there is a css-module actually exist, the hmr breaks:
image

@TotooriaHyperion
Copy link
Author

TotooriaHyperion commented Mar 25, 2020

Looks like we need a similar behavior like webpack-contrib/style-loader#298

If the locals didn't change, then it shouldn't require a full reload.

TotooriaHyperion added a commit to TotooriaHyperion/mini-css-extract-plugin that referenced this issue Mar 25, 2020
@alexander-akait
Copy link
Member

alexander-akait commented Mar 25, 2020

It is not a bug, it is feature, css-modules doesn't support HMR

@sethomas
Copy link

replacing the loader of mini-css-extract-plugin with style-loader, I am able to HMR css-modules without a page reload.

@alexander-akait
Copy link
Member

Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants