Closed bitttttten closed 5 years ago
Whats the definition of generateScopedName
?
Oh sorry, I tried to include as much as I can. I took this function from this medium article and changed it slightly.
const incstr = require('incstr')
const path = require('path')
const createUniqueIdGenerator = () => {
const index = {}
const generateNextId = incstr.idGenerator({
alphabet: 'abcdefghijklmnopqrstuvwxyz0123456789'
})
return (name) => {
if (index[name]) {
return index[name]
}
let nextId
// class name cannot start with a number
// and it cannot start with 'ad' otherwise adblocks will hide the element
do {
nextId = generateNextId()
} while (/^[0-9]/.test(nextId) || /^ad/.test(nextId))
index[name] = process.env.NODE_ENV === 'development'
? `${name}-${nextId}`
: nextId
return index[name]
}
}
const uniqueIdGenerator = createUniqueIdGenerator()
const generateScopedName = (localName, resourcePath) => {
let componentName = resourcePath.split(path.sep).slice(-2, -1)
if (componentName === "theme") {
componentName = `${componentName}.${resourcePath.split(path.sep).slice(-1, -1)}`
}
return `${uniqueIdGenerator(componentName)}_${uniqueIdGenerator(localName)}`
}
and my .babelrc
{
"presets": [
["env", {
"modules": false,
"targets": {
"browsers": ["> 0.5%"]
}
}],
"stage-2",
"react"
],
"env": {
"server": {
"presets": [["env", {
"targets": {
"node": "8.9.1"
}
}], "react"],
"plugins": [
"transform-decorators-legacy",
"transform-class-properties",
"react-loadable/babel"
]
}
}
}
And also my css-loader config:
{
test: /\.css$/,
use: [
{
loader: require.resolve('style-loader'),
options: {
insertInto: 'body',
},
},
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
modules: true,
getLocalIdent: (context, localIdentName, localName) => {
return generateScopedName(localName, context.resourcePath)
},
},
},
{
loader: require.resolve('postcss-loader'),
},
],
},
Perhaps it is my local set up that is incorrect. If I remove getLocalIdent
from the css-loader and generateScopedName
from react-css-modules config composes
is still not working.
I see in the DOM:
<h2 class="components-PageTitle-___PageTitle__root___3fA2n theme-___type__h2___266vk theme-___type__heading___1anDN">Shopping</h2>
But in the CSS I see:
/* PageTitle.css */._3fA2nPANa1DNO1QapNXW1v{color:#147c3f;padding:25.6px 0;padding:1.6rem 0}
/* theme/type.css */._1anDNxYVFvlSql6C1Bryx5{...}
If you are still interested, I can give you a repo to check out?
Edit: I see you need to match them so I understand why this doesn't work lol. Although my first question and the offer to share a reproduceable repo still stands.
My class name is correct:
<h2 class="PageTitle_root theme_h2 theme_heading">Shopping</h2>
. However the CSS is not:As you can see, the PageTitle CSS is generating the class name correctly, but the theme/type.css file is not. What's up with that?
My config is this: