Skip to content

Instantly share code, notes, and snippets.

@pranay321
Created June 30, 2019 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pranay321/c01410c2631c0eb26edb32fa1793d537 to your computer and use it in GitHub Desktop.
Save pranay321/c01410c2631c0eb26edb32fa1793d537 to your computer and use it in GitHub Desktop.
Minify Classnames
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}`;
};
Add incstr npm pakage to generate sequential string ids in node.js or browser
"dependencies": {
"incstr": "^1.2.3",
},
//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