question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Don't work with css-modules

See original GitHub issue
  • 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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
alexander-akaitcommented, Mar 25, 2020

It is bug, it is feature, css-modules doesn’t support HMR

4reactions
sethomascommented, Oct 30, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

CSS Modules doesn't work, React 16 - Stack Overflow
I suppose there's a problem with webpack because component's syntax isn't complicated. The component ignores styles. Why? package.json " ...
Read more >
Can CSS Modules solve all the problems? - YIOU CHEN
With CSS Modules, it's recommended to pass styles object around as themes. Let's create a public interface called styleName prop. import styles ...
Read more >
CSS modules and TypeScript doesn't work very well together
The support for CSS modules in the latest release of WebStorm is great but it doesn't work as well in TypeScript. What steps...
Read more >
Using CSS Modules in React - OpenReplay Blog
In contrast to Styled Components, CSS Modules don't accept props. In a time of working with global scope, CSS Modules will be a...
Read more >
Global vs. Local Styling In Next.js - Smashing Magazine
CSS custom properties, by contrast, also work with the cascade and can ... as ImageContainer because we're inside a CSS module, so we...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found