-
-
Notifications
You must be signed in to change notification settings - Fork 26.8k
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
getCSSModuleLocalIdent may cause unexpected behavior when using moduled CSS class names in querySelector #11775
Comments
We've recently run into this issue as well. It seems like you can work around it by using import styles from '../someStyle.css'
-document.querySelector('.' + styles.foo);
+document.querySelector('.' + CSS.escape(styles.foo));
function Component() {
return <div className={styles.foo} />
} |
Good idea. Our solution is copy the original function getLocalIdent(context, localIdentName, localName, options) {
// Use the filename or folder name, based on some uses the index.js / index.module.(css|scss|sass) project style
const fileNameOrFolder = /index\.module\.(css|scss|sass)$/.test(context.resourcePath) ? '[folder]' : '[name]';
// Create a hash based on a the file location and class name. Will be unique across a project, and close to globally unique.
const hash = loaderUtils.getHashDigest(path.posix.relative(context.rootContext, context.resourcePath) + localName, 'md5', 'base62', 5);
// Use loaderUtils to find the file or folder name
const className = loaderUtils.interpolateName(context, `${fileNameOrFolder}_${localName}__${hash}`, options);
// Remove the .module that appears in every classname when based on the file and replace all "." with "_".
return className.replace('.module_', '_').replace(/\./g, '_');
}
// css-loader config
modules: {
getLocalIdent,
} Maybe I should submit a PR for it. |
I recently run into that issue as well when the generated classes had This is my code: // Create a hash based on a the file location and class name. Will be unique across a project, and close to globally unique.
const hash = loaderUtils
.getHashDigest(
path.posix.relative(context.rootContext, context.resourcePath) +
localName,
"md5",
"base64",
5
)
// replace base64 characters with their url-safe counterparts
// https://datatracker.ietf.org/doc/html/rfc4648#section-5
.replace(/\+/g, "-")
.replace(/\//g, "_"); |
I also run into this issue. My solution is to use "base64url" instead of "base64" as digestType parameter in BTW I wonder why this issue just appears now. |
Good one, didn’t know base64url existed. Since my project is stuck using webpack 4, I have to also use react-scripts v4. Noticed that upgrading from 4.0.1 to 4.0.2 causes different hashes to be calculated.
|
Fixed in PR #12013 |
I also run into this issue, used CSS.escape() to resolve, waitting cra fixed |
Describe the bug
Use
getLocalIdent: getCSSModuleLocalIdent
in css-loader option may cause unexpected behavior when using moduled CSS class names inquerySelector
.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:)
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 tychenjiajun@28d10e6
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/MWEmxpZThe text was updated successfully, but these errors were encountered: