-
Notifications
You must be signed in to change notification settings - Fork 27.5k
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
css-loader + CSS modules pure selectors #10142
Comments
I am trying out the newly released v9.2 and is facing the exact same issue. I am neither able to make the global CSS work nor the local using the module system. Global CSS doesn't apply but everything compiles correctly. The module-based system throws the "Selector "nav" is not pure (pure selectors must contain at least one local class or id)" error. I'm following the steps as mentioned in the blog article |
I reproduced the problem you're facing by using css modules For it to be supported, it would need to use the Looking at next package.json, I can't see the dependency. |
Warning: Please remove the postcss-modules-values plugin from your PostCSS configuration. This plugin is automatically configured by Next.js. @cedric-marcone It seems to be included |
@Pegase745 : You're right, I added a postcss config file with the module and I'm having the same warning. => Currently css modules in next 9.2 stop working when using |
I've also found the same issue after migrating my project to the latest Next Canary (9.2.1-canary.11) using SCSS modules.
I'm not sure what the error means but would be happy to modify my SCSS to solve the issue Update I solved my issues by moving away from implicit HTML calls (ex: |
Same here, I can push up a demo project that reproduces the error if that's helpful. |
@agconti that would be great if it doesn't take too much time! |
@Pegase745 here's a repo which minimally reproduces the error: https://github.com/agconti/next-js-css-modules-unable-to-use--global-with-css-modules Steps to reproduce:
|
Any update on this? |
I had no time to dig deep into the next.js css-modules implementation, but wanted to share this:
Then simply apply them in your descendants module classes: If you want to use separate file for the variables, put the top level class that contains variables in a separate file and then compose:
|
@agconti I've created a fork of your reproduction to hopefully illustrate the issue more accurately - https://github.com/simonsmith/next-js-css-modules-unable-to-use--global-with-css-modules It's quite correct that an error should be thrown when using Some examples can be seen in the The issue seems to be more when making use of the
So it seems as though the Interestingly if I add the following test to {
should: 'ignore :import statemtents',
input: ':import("~/lol.css") { foo: __foo; }',
+ options: {mode: 'pure'},
expected: ':import("~/lol.css") { foo: __foo; }',
}, I see the exact same error! This makes me think it may be a bug in I can open an issue on that repository to track this cc @evilebottnawi Do you agree? |
I've been attempting to migrate our codebase onto Next, but just today discovered that our SCSS implementation breaks because all of our selectors have to be pure (and plenty are not). While I get why pure selectors would be ideal, it makes it impossible for large, enterprise projects full of "legacy" SCSS to migrate. I'll never get funding for a tech debt effort to update 100+ scss files. There should be a flag to turn off pure mode. |
Same here. At present we can't migrate to this built-in feature because of this problem. |
This comment has been minimized.
This comment has been minimized.
I've traced #11629 back to this issue as well. Our setup is having our colour and breakpoint variables declared in a config which is shared between JS, local and global scss alike. A combination of I'm also worried what will happen when I try to apply keyframes declared in the global CSS in the modules, I'm sure I would need a Finally; I cannot adjust the exported class names that CSS modules generate. This is causing inconsistencies between my regular build and Storybook, which supports my own CSS modules settings. Basically; we need the ability to adjust CSS loader options. |
Overriding the default webpack config worked for me. To override import behaviour for next.config.js/**
* Stolen from https://stackoverflow.com/questions/10776600/testing-for-equality-of-regular-expressions
*/
const regexEqual = (x, y) => {
return (
x instanceof RegExp &&
y instanceof RegExp &&
x.source === y.source &&
x.global === y.global &&
x.ignoreCase === y.ignoreCase &&
x.multiline === y.multiline
);
};
module.exports = {
webpack: (config) => {
const oneOf = config.module.rules.find(
(rule) => typeof rule.oneOf === 'object'
);
if (oneOf) {
const moduleCssRule = oneOf.oneOf.find(
(rule) => regexEqual(rule.test, /\.module\.css$/)
// regexEqual(rule.test, /\.module\.(scss|sass)$/)
);
if (moduleCssRule) {
const cssLoader = moduleCssRule.use.find(({ loader }) =>
loader.includes('css-loader')
);
if (cssLoader) {
cssLoader.options.modules.mode = 'local';
}
}
}
return config;
},
}; To override all CSS import behaviour: next.config.jsmodule.exports = {
webpack: (config) => {
const oneOf = config.module.rules.find(
(rule) => typeof rule.oneOf === 'object'
);
const fixUse = (use) => {
if (use.loader.indexOf('css-loader') >= 0 && use.options.modules) {
use.options.modules.mode = 'local';
}
};
if (oneOf) {
oneOf.oneOf.forEach((rule) => {
if (Array.isArray(rule.use)) {
rule.use.map(fixUse);
} else if (rule.use && rule.use.loader) {
fixUse(rule.use);
}
});
}
return config;
},
}; If you need to import global CSS, you can configure it to use |
@Timer the fix for this issue landed in v3.0.3 of postcss-modules-local-by-default (css-modules/postcss-modules-local-by-default#23). Updating css-loader in |
Yes, need to update deps and it can be closed |
Fixed in |
@Timer sorry for the ignorance, but when will this release (not as prerelease)? |
@Pegase745 @Anupsarode @cedric-marcone @tommyboylab @agconti @cleversprocket @HoraceShmorace File extensions with *.module.(css | scss | sass) are css modules and they can only target elements using classnames or ids and not using tag names. Although this is possible in other frameworks like create-react-app, it is not possible in next-js (as of now). My suggestion is using these html selector css in a separate file that doesn't contain the name like '.module' in it. And after doing that, instead of importing like this import styles from './styles.module.scss'; import like this import './styles.scss'; This will not through any errors. |
Thank you for that clarification @naveen-bharathi. That was exactly my problem. I knew that styling html tags wasn't possible in modules, but ignored the error message because my html tag was styled in a non-module .scss file. What I had forgotten was that I was importing the non-module .scss file in a .module.scss file, obviously causing the error. |
@naveen-bharathi the error you mention is different. CSS Modules can only export classes but you can use tags as part of the selector. I'm doing this in my next.js application and elsewhere and it works fine. For instance: /*
The `a` and `span` are allowed because they come after the class
*/
.myClass a span {
color: red;
}
/*
This is not allowed because only classes are allowed as top level exports
*/
a {
color: blue;
} If this weren't allowed then all sorts of things wouldn't be allowed, like pseudo selectors for instance. |
@chrisvxd I changed the value to I just copied and pasted your code in next.config.js file. and importing my CSS file(main.module.css) in _app.js. |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Bug report
Describe the bug
css-loader
error on build.To Reproduce
After migrating to
9.2
and testing the CSS modules features, the below configuration started erroring.colors.css (containing only values)
Component.module.css
I am having:
Expected behavior
Well it did seem to work when I had
next.config.js
Not sure if there's some tweaking to do some where, not a champ on CSS modules stuff.
I've tried to understand this test but no luck.
System information
The text was updated successfully, but these errors were encountered: