Skip to content

Instantly share code, notes, and snippets.

@herlon214
Created July 3, 2022 02:47
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save herlon214/7da6a39b885014a5d5f51716f6530ce4 to your computer and use it in GitHub Desktop.
Save herlon214/7da6a39b885014a5d5f51716f6530ce4 to your computer and use it in GitHub Desktop.
NextJS 12 Hash Class Names
const path = require('path')
const loaderUtils = require('loader-utils')
const hashOnlyIdent = (context, _, exportName) => {
const result = loaderUtils
.getHashDigest(
Buffer.from(
`filePath:${path
.relative(context.rootContext, context.resourcePath)
.replace(/\\+/g, '/')}#className:${exportName}`
),
'md4',
'base64',
6
)
.replace(/^(-?\d|--)/, '_$1')
.replaceAll('+', '_')
.replaceAll('/', '_')
return result
}
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
sassOptions: {
includePaths: [path.join(__dirname, 'styles')],
},
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
const rules = config.module.rules
.find((rule) => typeof rule.oneOf === 'object')
.oneOf.filter((rule) => Array.isArray(rule.use))
if (!dev)
rules.forEach((rule) => {
rule.use.forEach((moduleLoader) => {
if (
moduleLoader.loader?.includes('css-loader') &&
!moduleLoader.loader?.includes('postcss-loader')
)
moduleLoader.options.modules.getLocalIdent = hashOnlyIdent
})
})
return config
},
}
module.exports = nextConfig
@gggglglglg
Copy link

does not work with tailwind css

@Shair17
Copy link

Shair17 commented Oct 25, 2022

I've got an error
image

@h-evers
Copy link

h-evers commented Mar 15, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment