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.

Using a custom .babelrc in create-react-app

See original GitHub issue

Describe the bug I am using customize-cra to add a specific babel plugin for development and I have also added a .babelrc in the .storybook folder which has the following:

{
  "presets": ["react-app"],
  "plugins": ["@babel/plugin-proposal-optional-chaining"]
}

The issue is it doesn’t read the babel config even though the terminal says: image

But in the end it shows the following issue: image

Expected behavior It should run the plugins and presets from the custom .babelrc file

System:

  • OS: MacOS
  • Device: Macbook
  • Browser: Chrome
  • Framework: React
  • Version: @storybook/react: 5.0.6 react-scripts: 2.1.8

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:17
  • Comments:28 (10 by maintainers)

github_iconTop GitHub Comments

13reactions
slorbercommented, Feb 21, 2020

Hi,

If you are using CRA and you need a custom config (like a babel plugin) you can use react-app-rewired with customize-cra.

Here’s how you can add the emotion css props plugin to both CRA + Storybook.

(This is useful for Emotion because the babel macro can’t setup the emotion css pragma, so really the babel plugin is required)

Step 1

Add customize CRA + React-app-rewired

yarn add customize-cra react-app-rewired --dev
  /* ./package.json */

  "scripts": {
-   "start": "react-scripts start",
+   "start": "react-app-rewired start",
-   "build": "react-scripts build",
+   "build": "react-app-rewired build",
-   "test": "react-scripts test --env=jsdom",
+   "test": "react-app-rewired test --env=jsdom",
    "eject": "react-scripts eject"
}

Step 2: customize the CRA config:

/* ./config-overrides.js */

const {
  override,
  addBabelPreset
} = require("customize-cra");
const path = require("path");

module.exports = override(
  addBabelPreset("@emotion/babel-preset-css-prop")
);

Step 3: customize the Storybook config:

Note, you can keep using the CRA storybook preset

/* ./storybook/webpack.config.js */

module.exports = ({ config }) => {
  return require("../config-overrides")(config);
};

This way you don’t have to duplicate the config overrides, and can use the same overrides for both CRA and Storybook to keep them in sync.

4reactions
mrmckebcommented, Nov 21, 2019

Not yet @shilman, I’m checking now to see why the release is still held up.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Simplest Way to Install Babel Plugins in Create React App
The traditional way of installing Babel plugins in a Create React App project requires you to eject the app via running npm run...
Read more >
How to customize babel config? · Issue #167 - GitHub
In the project created by create-react-app , I want to add some code with ES Next decorators. So, I installed babel-plugin-transform-decorators- ...
Read more >
Modify Create React App's Babel Configuration Without Ejecting
Learn how to modify the underlying Create React App configuration with customize-cra to include new babel plugins. All without having to ...
Read more >
How to find .babelrc? - reactjs - Stack Overflow
If you have created React app using create-react-app, first you need to eject from the app so that you can use custom configuration....
Read more >
Introducing new plugins in Babel to create react app
For using babel plugins without ejecting, you need to install react-app-rewired and customize-cra. ... Create a config-overrides.js file in the root directory: ...
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