Replies: 12 comments 15 replies
-
This is already being worked on: #14197 so it does not warrant having an extra configuration option.
This seems more like a personal preference than a reason to add an extra configuration option to Next.js which has to be maintained forever. |
Beta Was this translation helpful? Give feedback.
-
It's great that Next.js includes solid default settings for build tools like However, what's even better, is if you allow developers full unobstructed access to the configuration of the build tools for when their individual organization's workflows are different than what Next.js defaults to (or has configuration abstractions for). This is important to me to align a few Next.js projects with many other projects running on other JS/TS frameworks where we've already customized I'd suggest building upon the recent work to allow customization of
|
Beta Was this translation helpful? Give feedback.
-
Nuxtjs can do it so easily, but nextjs can't do it. Optimizing className in production is a great feature, people need a config option to config it. |
Beta Was this translation helpful? Give feedback.
-
Hello guys, Thanks |
Beta Was this translation helpful? Give feedback.
-
While we all are waiting that feature, may be it would be useful for somebody -
It's awful, I know( but now it is the best solution I could invent. |
Beta Was this translation helpful? Give feedback.
-
This code works fine for me!
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Any updates on that? That comes as an issue in a monorepo environment. We have a separate package for all of the UI components that is built using css-module local convention for camelCase; I can obviously use compiled version of |
Beta Was this translation helpful? Give feedback.
-
For Next 13, here's the config to change CSS Modules Scoped Name: const path = require('node:path');
const { createHash } = require('node:crypto');
const getHash = (source, length) =>
createHash('shake256', { outputLength: length }).update(source).digest('hex');
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
// ... your existing config here ...
webpack: (config, { dev }) => {
// https://github.com/vercel/next.js/discussions/15818
let rule, moduleRules, cssLoader;
if ((rule = config.module.rules.find((rule) => Object.keys(rule).includes('oneOf')))) {
if (
(moduleRules = rule.oneOf.filter(
(r) =>
('test.module.scss'.match(r.test) || 'test.module.css'.match(r.test)) &&
Array.isArray(r.use),
))
) {
for (const moduleRule of moduleRules) {
if ((cssLoader = moduleRule.use.find((u) => u.loader.match('css-loader')))) {
delete cssLoader.options.modules.getLocalIdent;
cssLoader.options = {
...cssLoader.options,
modules: {
...cssLoader.options.modules,
getLocalIdent: ({ resourcePath }, localIdentName, localName) => {
const { name } = path.parse(resourcePath);
const moduleName = name
.replace(/\.module/g, '')
.replace(/\./g, '-');
return dev
? `${moduleName}--${localName}--${getHash(resourcePath, 2)}`
: `_${getHash(`${resourcePath}${localName}`, 4)}`;
},
},
};
}
}
}
}
return config;
},
};
module.exports = nextConfig; This gives you class names like |
Beta Was this translation helpful? Give feedback.
-
@timneutkens with next13 can we now revisit what options are useful to add to Next.js directly ? |
Beta Was this translation helpful? Give feedback.
-
This works for me in Next.js 14.2.4: import {createHash} from 'node:crypto'
/** @type {import('next').NextConfig} */
const getHash = (source, length) =>
createHash('shake256', {outputLength: length}).update(source).digest('hex')
const updateCssLoader = rule => {
rule.use.forEach(moduleLoader => {
if (
moduleLoader.loader?.includes('/css-loader') &&
typeof moduleLoader.options.modules === 'object'
) {
// Add a custom localIdentName to the css-loader options
moduleLoader.options.modules.getLocalIdent = ({resourcePath}, _, localName) =>
process.env.NODE_ENV === 'development'
? `${localName}--${getHash(resourcePath, 2)}`
: `_${getHash(`${resourcePath}${localName}`, 3)}`
}
})
}
const nextConfig = {
webpack: config => {
const rules = config.module.rules
.find(rule => typeof rule.oneOf === 'object')
.oneOf.filter(rule => Array.isArray(rule.use))
rules.forEach(updateCssLoader)
return config
},
}
export default nextConfig |
Beta Was this translation helpful? Give feedback.
-
Summary
How about allow cssOption like sassOptions?
Issues: #10584 #10339 (comment) #14295
Background
Optimizing className in production.
Now we cannot cutomize className.
But I wanna use shorter className in production.
I wanna transfer export name.
As you know,
css
recommends to use kebab-case.But I cannot use hyphen in JS.
Of course, I can use like this:
styles['active-header']
(I think this syntax is ugly... 😞 )Solution
Above two things are already supported in css-loader.
https://github.com/webpack-contrib/css-loader#localidentcontext
https://github.com/webpack-contrib/css-loader#namedexport
Conclusion
So I hope next.js supports cssOptions.
And then we can customize cssLoaderOptions in
next.config.js
:Beta Was this translation helpful? Give feedback.
All reactions