-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
Allow users to configure options of css-loader #10584
Comments
Looks like a duplicate of #10339 |
Yes! |
the same problem for me. I'm migrating CRA project to nextjs, I have encountered the camelCase of css module problem |
|
I open a discussion about this: #15818 |
Closing in favor of above RFC. Thanks! |
@loteoo Can you post an example you are using to do that ? |
Did you found any solution for that? |
I also need a solution for |
Again you'll want to do something like this just modify your webpack config in next.config.js to whatever you want, this is my personal one I use to modify exportLocalsConvention so i can use kebab-case in .scss files and camelCase in my components. This version should work on the latest version of NextJS, the previous version will only work on versions below 10 I believe.
|
This StackOverflow answer works for |
@AnonOnr Be wary that that this could break any time if next.js decides to change their Webpack config. |
For the Webpack 5 config (with Next.js 10.1.3), I had to use Better yet, to make it more future-proof:
|
Unfortunately the workarounds did not work for me with function cssLoaderOptions(modules) {
const { getLocalIdent, ...others } = modules; // have to remove getLocalIdent otherwise localIdentName doesn't work 🤷🏽
return {
...others,
localIdentName: "[hash:base64:6]",
exportLocalsConvention: "camelCaseOnly",
};
}
module.exports = {
basePath: process.env.NEXT_PUBLIC_BASE_PATH,
webpack: (config, options) => {
config.module.rules[1].oneOf.forEach((moduleLoader) => {
Array.isArray(moduleLoader.use) &&
moduleLoader.use.forEach((l) => {
if (
l.loader.includes("css-loader") &&
!l.loader.includes("sass-loader") &&
!l.loader.includes("postcss-loader")
) {
l.options = {
...l.options,
modules: cssLoaderOptions(l.options.modules),
};
}
});
});
return config;
},
}; |
Hello, config.module.rules[1] is for webpack 4 and config.module.rules[2] for webpack 5, rigth ? |
Trying this for nextjs with styled-components. Its simply aint working! Any idea on what i would be missing?
Doing something like this. |
webpack: (config) => {
config.module.rules[2].oneOf.forEach((moduleLoader) => {
if (Array.isArray(moduleLoader.use)) {
moduleLoader.use.forEach((item) => {
if (item.loader.includes('css-loader') && !item.loader.includes('postcss-loader')) {
item.options.modules.exportLocalsConvention = 'camelCaseOnly';
}
});
}
});
return config;
}, it works |
Unfortunately doesn't work anymore with next v11 |
@MickCoelho sometimes the position of "oneOf" changes, that code is by no means a robust solution but normally just means every few versions or so if oneOf's index within the config changes you'll have to update it. To find where oneOf is i normally just go console.log(config.module) and look for the index. The new code is.... config.module.rules[3].oneOf.forEach((moduleLoader, i) => {
Array.isArray(moduleLoader.use) &&
moduleLoader.use.forEach((l) => {
if (l.loader.includes("css-loader") && !l.loader.includes("postcss-loader")) {
l.options = {
...l.options,
modules: {
...l.options.modules,
exportLocalsConvention: "camelCaseOnly",
},
};
}
});
}); |
Yeah, I did find that out too. |
@ShanonJackson : Maybe not depend on the index? Something like this: module.exports = {
webpack: (config) => {
const oneOf = config.module.rules.find(
(rule) => typeof rule.oneOf === 'object'
);
if (oneOf) {
const moduleSassRule = oneOf.oneOf.find((rule) =>
regexEqual(rule.test, /\.module\.(scss|sass)$/)
);
if (moduleSassRule) {
const cssLoader = moduleSassRule.use.find(({ loader }) =>
loader.includes('css-loader')
);
if (cssLoader) {
// Use the default CSS modules mode. Next.js use 'pure'. Not sure of all implications
cssLoader.options = {
...cssLoader.options,
modules: cssLoaderOptions(cssLoader.options.modules),
};
}
}
}
return config;
},
}; P.S: This is for Oh and the /**
* 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
);
}; |
Does anyone have an updated solution? This works for me, but fails to apply styles from global.css with class name selectors:
Any tips? This is making me pull my hair lol |
This one works for Nextjs version 11: (https://stackoverflow.com/questions/66744765/how-to-hash-css-class-names-in-nextjs)
|
It's broken with Next.js 12 again config.module.rules[2].oneOf.forEach((moduleLoader) => {
Array.isArray(moduleLoader.use) &&
moduleLoader.use.forEach((l) => {
if(!l.loader) return;
if (l.loader.includes('css-loader') && !l.loader.includes('postcss-loader')) {
const { getLocalIdent, ...others } = l.options.modules;
l.options = {
...l.options,
modules: {
...others,
localIdentName: "[hash:base64:8]",
},
};
}
});
}); should work but i somehow get an error that EDIT: Using this function as |
@dunklesToast try adding a
|
Feature request
Allow users to configure options of
css-loader
or override options ofpostcss-modules
.Is your feature request related to a problem? Please describe.
With the previous release of Next.js (9.1), I was using the
next-css
plugin to use CSS modules and thecssLoaderOptions
as seen here.My config looks like this:
Moving to 9.2, I would like to stop using the
next-css
plugin and instead use the out-of-the-box support from the new release, but I have not found a way to set thecamelCase
option to true, which breaks my app without it. (I use the camelCase transform everywhere)The localIdentName would also be nice to be able to configure, since it gives users opportunity to change the resulting CSS class names that end users see, but it's more of a "nice to have" at this point. Not a deal breaker.
Describe the solution you'd like
Doing something like what Gatsby.js does would be great:
https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-postcss#postcss-plugins (scroll down at the end)
In Next.js, this could be by supporting a
cssLoaderOptions
object to thepostcss.config.js
file.Example:
postcss.config.js
Describe alternatives you've considered
Being able to change the configuration of
postcss-modules
in the postcss.config.js file would also cover most cases. It is currently "already configured by next.js", but being able to override it could give users many more options to play with, such ascamelCase
,generateScopedName
,globalModulePaths
which are all very sensible options with real use cases.More context and motivation behind this request
I know Next.js is trying to make life easier for it's users by making a lot of choices for them, but it should always be possible for the users to override those choices if needed. Going too far in this direction will be like CRA all over again. I don't want to start "ejecting" out of parts of Next.js.
I will keep using
next-css
until these configurations are supported, but dropping it from my app would be very appreciated as it sounds like it is going to be deprecated in the future.Bonus: why do I care about the camelCase option
Using dashes in CSS class names is a solid convention that has been around since the beginning of time in the land of CSS. Using camelCase properties in javascript is also the way to go (common style-guides and linters will enforce this). Since CSS modules bridges the gap between the two worlds, it makes a lot of sense to give this option.
The text was updated successfully, but these errors were encountered: