Skip to content

Instructions to switch from react-css-modules #108

Closed
@apennell

Description

@apennell

It would be great if there was a section with the steps required to switch from using react-css-modules to babel-plugin-css-modules, since this is now recommended over that package.

I built out an app using create-react-app and successfully implemented react-css-modules. There's a note in the react-css-modules Readme about using babel-plugin-react-css-modules instead, but it wasn't clear that it's no longer maintained or recommended by @gajus so I went with the original. After running into problems with Jest testing, I learned in the issue addressing my problem that @gajus doesn't recommend using it, so I'm trying to switch to this one.

I figured that I could just change the setup of my components that worked with react-css-modules, but it doesn't work.

My React components:

// App.js
import './App.css';
import Button from './components/Elements/Button/Button.jsx';

class App extends Component {
    render() {
        return (
            <Button size="large">
                Working Button!
            </Button>
        );
    }
}

export default App;
// src/components/Elements/Button/Button.jsx
import './Button.css';

export const Button = ({children, size, isFullWidth}) => {
    return (
        <button styleName={`${size}Size ${isFullWidth ? "block" : ""}`} >
 	    {children}
 	</button>
    );
}

export default Button;

To enable react-css-modules in my create-react-app, I modified my webpack.config to have:

...
{
    test: /\.(js|jsx)$/,
    include: paths.appSrc,
    loader: require.resolve('babel-loader'),
    options: {
        cacheDirectory: true,
        plugins: [
            'react-css-modules'
        ]
    },
},
{
    test: /\.css$/,
    use: [
        require.resolve('style-loader'),
        {
            loader: require.resolve('css-loader'),
            options: {
                importLoaders: 1,
                modules: true,
                localIdentName: '[name]__[local]___[hash:base64:5]&sourceMap&-minimize',
            },
        },
        ...
    ]
}
...

I've looked at several issues related to this, including issue #5 and issue #82 , and it looks like babel settings need to be updated, but I've been unsuccessful in my attempts so far.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions