Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better alternative #122

Open
felixcatto opened this issue Mar 18, 2023 · 0 comments
Open

Better alternative #122

felixcatto opened this issue Mar 18, 2023 · 0 comments

Comments

@felixcatto
Copy link

felixcatto commented Mar 18, 2023

There is a better maintained version - babel-plugin-react-css-modules. With replaceImport: true option it does exactly what this plugin does. Also you can pass transform function to it and apply any postcss transforms.

babel.config.js

import postcss from 'postcss';
import nested from 'postcss-nested';
import crypto from 'crypto';

const generateScopedName = (localName, resourcePath, x) => {
  const getHash = value => crypto.createHash('sha256').update(value).digest('hex');
  const hash = getHash(`${resourcePath}${localName}`).slice(0, 4);
  return `${localName}--${hash}`;
};

const processPostcss = (cssSource, cssSourceFilePath) => {
  if (!cssSourceFilePath.endsWith('.module.css')) return '';
  return postcss([nested]).process(cssSource, { from: cssSourceFilePath });
};

export default {
  plugins: [
    [
      '@dr.pogodin/react-css-modules',
      {
        replaceImport: true,
        generateScopedName,
        transform: processPostcss,
      },
    ],
  ],
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant