getCSSModuleLocalIdent may cause unexpected behavior when using moduled CSS class names in querySelector
See original GitHub issueDescribe 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:)
- 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,
},
],
},
];
}
- Write the following codes in app
import styles from '../someStyle.css'
document.querySelector('.' + styles.foo);
function Component() {
return <div className={styles.foo} />
}
- Hopes for the luck. In some rare cases,
styles.foo
may includes character+
andquerySelector
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:
- Created 2 years ago
- Reactions:4
- Comments:7
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
I also run into this issue, used CSS.escape() to resolve, waitting cra fixed