question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use babel-plugin-react-css-modules without ejecting?

See original GitHub issue

TL;DR; Asking for pointers on using babel-plugin-react-css-modules without ejecting.

Hi fellow create-react-app users,

I’m a new user to this amazing tool, having only used React on Rails projects using Webpacker (I’m mostly a Ruby developer myself). I’ve been following courses on several resources and I got caught up on the whole styling React thing; so far choosing CSS modules as my favorite approach.

Previously, I configured CSS modules by ejecting, but now that create-react-app 2 is on the way I see CSS modules are coming with just an extension change. Great!

However, I also saw that babel-plugin-react-css-modules is being favored over react-css-modules, in order to be able to use styleName and gaining all the performance benefits.

So I read about having to use Babel Macros to use this without ejecting. But if I don’t know how to create a plugin/macro, do I need to stick with importing styles as an object and using className?

Sources I’ve read: 1, 2, 3, 4, 5, 6

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
transitive-bullshitcommented, Sep 27, 2018

It seems that you want too use babel-plugin-react-css-modules over react-css-modules, but CRA v2 doesn’t use either. It’s using postcss under the hood (relevant PR). It’s probably useful to understand why CRA is using postcss at compile-time because it negates all the worry about runtime costs and bloat that the babel-plugin-react-css-modules talks about.

If you want to use babel-plugin-react-css-modules, then this isn’t supported by CRA and imho likely won’t be since its semantics deviate pretty heavily from the straightforward, opt-in version of CSS modules that CRA v2 currently supports.

My recommendation would be either to eject or use react-app-rewired@next to override the portions of the webpack / babel config that you need, but be aware that this essentially “voids the warranty” provided by base CRA in terms of guaranteeing how things work.

With that being said, my 95% recommendation would be to just use the built-in CRA support for CSS modules and not complicate things any further 😉

0reactions
ervwaltercommented, Dec 31, 2018

Thank you @patricklafrance, that was super helpful.

FYI for all, just to be clear, babel-plugin-react-css-modules doesn’t replace postcss for CSS Modules. It doesn’t really do anything with processing the CSS files themselves at all. It’s purely a React helper that lets you use an alternate syntax. So instead of this:

<nav className={`navbar ${styles["masthead"]}`}

You can use this:

<nav className="navbar" styleName="masthead">

The alternate syntax is nice, but the main benefit for me is that with the babel plugin in place, you get error messages when you fat finger a local class name and spell it wrong.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to setup babel-plugin-react-css-modules in create-react ...
1. add the plugin to .babelrc first. "plugins": [ ["babel-plugin-react-css-modules", { "webpackHotModuleReloading": true, ...
Read more >
babel-plugin-react-css-modules/Lobby - Gitter
Hey. I'm trying to use react-css-modules-babel-plugin with scss. It looked pretty simple to setup. It compiles, and it assigns the class names correctly,...
Read more >
Babel Plugin: React CSS Modules - npm
Start using @dr.pogodin/babel-plugin-react-css-modules in your project by running `npm i @dr.pogodin/babel-plugin-react-css-modules`.
Read more >
E CSS in JS | Client-Side Web Development
This will apply the plugin whenever the JavaScript files are transpiled, allowing you to utilize the styleName without explicitly using the react-css-modules ......
Read more >
Using CSS Modules in React - Programming with Mosh
Additionally, if you really like to customize things or see what happens under the hood, it's also possible to eject your application or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found