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.

getCSSModuleLocalIdent may cause unexpected behavior when using moduled CSS class names in querySelector

See original GitHub issue

Describe the bug

Use getLocalIdent: getCSSModuleLocalIdent in css-loader option may cause unexpected behavior when using moduled CSS class names in querySelector.

Did you try recovering your dependencies?

Yes

Which terms did you search for in User Guide?

getCSSModuleLocalIdent

Environment

webpack

Steps to reproduce

(Write your steps here:)

  1. Use exactly the same webpack config in readme.
const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent');

// In your webpack config:
// ...
module: {
  rules: [
    {
      test: /\.module\.css$/,
      use: [
        require.resolve('style-loader'),
        {
          loader: require.resolve('css-loader'),
          options: {
            importLoaders: 1,
            modules: {
              getLocalIdent: getCSSModuleLocalIdent,
            },
          },
        },
        {
          loader: require.resolve('postcss-loader'),
          options: postCSSLoaderOptions,
        },
      ],
    },
  ];
}
  1. Write the following codes in app
import styles from '../someStyle.css'

document.querySelector('.' + styles.foo);

function Component() { 
  return <div className={styles.foo} />
}
  1. Hopes for the luck. In some rare cases, styles.foo may includes character + and querySelector will throw error or return null.

Expected behavior

return value of getCSSModuleLocalIdent doesn’t includes +.

Actual behavior

return value of getCSSModuleLocalIdent may includes +.

Reproducible demo

Checkout my forks https://github.com/tychenjiajun/create-react-app/commit/28d10e6c3c8e71d33c4b6c5ed3c23e1600b1daf9 In the test cases, if input class name is ‘class1020’, the output class name will be file_class1020__V+98r. Using this class name inquerySelector causes error, see https://codepen.io/chankcccc/pen/MWEmxpZ

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:7

github_iconTop GitHub Comments

1reaction
pstrhcommented, Feb 2, 2022

I also run into this issue. My solution is to use “base64url” instead of “base64” as digestType parameter in getHashDigest. base64url was introduced with nodejs 14. As I just raised #12013 regarding css modules hash collisions I can offer to fix the issue there.

BTW I wonder why this issue just appears now.

0reactions
Hew007commented, May 7, 2022

I also run into this issue, used CSS.escape() to resolve, waitting cra fixed

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I select my CSS module class using document ...
You need to select it as a class, document.querySelector(`.${styles.scrollValue}`). Calling styles.property returns a value such as the ...
Read more >
CSS Modules - Best of JS
This is recommended because the common alternative, kebab-casing may cause unexpected behavior when trying to access style.class-name as a dot notation.
Read more >
The evolution of scalable CSS, Part 5: Styles Encapsulation
Styles encapsulation and CSS Modules elegantly solve all the CSS naming collisions problems, while keeping specificity low at the same time.
Read more >
Discussion of Why We're Breaking Up with CSS-in-JS
I mean same can be said for any tool. ... At least with SASS Modules and conditionally applying classNames I can immediately see...
Read more >
CSS module styles - how to write styles for Javascript application
Prior to using it, you have to make a link to the Bootstrap CSS file location of course. You can also explore helper...
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