Created
June 30, 2019 09:52
-
-
Save pranay321/c01410c2631c0eb26edb32fa1793d537 to your computer and use it in GitHub Desktop.
Minify Classnames
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const incstr = require('incstr'); | |
const createUniqueIdGenerator = () => { | |
const uniqIds = {}; | |
const generateNextId = incstr.idGenerator({ | |
alphabet: 'abcefghijklmnopqrstuvwxyzABCEFGHJKLMNOPQRSTUVWXYZ', | |
}); | |
return name => { | |
if (!uniqIds[name]) { | |
uniqIds[name] = generateNextId(); | |
} | |
return uniqIds[name]; | |
}; | |
}; | |
const localNameIdGenerator = createUniqueIdGenerator(); | |
const componentNameIdGenerator = createUniqueIdGenerator(); | |
module.exports = (localName, resourcePath) => { | |
const componentName = resourcePath.split('/').slice(-2, -1)[0]; | |
const localId = localNameIdGenerator(localName); | |
const componentId = componentNameIdGenerator(componentName); | |
return `${componentId}_${localId}`; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add incstr npm pakage to generate sequential string ids in node.js or browser | |
"dependencies": { | |
"incstr": "^1.2.3", | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Imported custom module | |
const getScopedName = require('./getScopedName'); | |
//Modified existing CRA webpack config css module code | |
{ | |
test: cssModuleRegex, | |
use: getStyleLoaders({ | |
importLoaders: 1, | |
sourceMap: isEnvProduction && shouldUseSourceMap, | |
modules: true, | |
getLocalIdent: getCSSModuleLocalIdent, | |
}), | |
}, | |
//to | |
// Adds support for CSS Modules (https://github.com/css-modules/css-modules) | |
// using the extension .module.css | |
{ | |
test: cssModuleRegex, | |
use: getStyleLoaders({ | |
importLoaders: 1, | |
sourceMap: isEnvProduction && shouldUseSourceMap, | |
modules: true, | |
...(isEnvProduction && { | |
getLocalIdent: (context, localIdentName, localName) => | |
getScopedName(localName, context.resourcePath), | |
}), | |
}), | |
}, | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment