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.

Create React App + craco + @emotion/babel-preset-css-prop

See original GitHub issue
  • emotion version: 10.0.5
  • react version: 16.6.3

Relevant code:

<div css={{color: 'red'}}>hello</div>

What you did:

Use craco with create-react-app. craco is the new version of create-react-app-rewired

# craco.config.js

module.exports = {
  babel: {
    presets: ['@emotion/babel-preset-css-prop']
  }
}

What happened:

It showed in DOM as <div css=[Object]>

Reproduction:

Problem description:

Preset @emotion/babel-preset-css-prop does not work. I still need to add /** @jsx jsx/, import { jsx } from '@emotion/core' to get it work.

Suggested solution:

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

24reactions
PutziSancommented, Jan 19, 2019

TL;DR it works using this craco-config:

// craco.config.js
const emotionPresetOptions = {};

const emotionBabelPreset = require("@emotion/babel-preset-css-prop").default(
  undefined,
  emotionPresetOptions
);

module.exports = {
  babel: {
    plugins: [
      ...emotionBabelPreset.plugins
      // your other plugins
    ]
  }
};

Using the preset directly with craco does not work, because craco inserts the presets from craco.config.js before the presets from CRA.

CRA uses a preset for the babel configuration and the order in which babel presets/plugins executes is:

  • Plugins run before Presets.
  • Plugin ordering is first to last.
  • Preset ordering is reversed (last to first).

The plugins of @emotion/babel-preset-css-prop must be executed before the plugins of CRA are executed, otherwise the JSX code will have been transformed to React.createElement before emotions plugin had a chance to do so.

For these reasons it works if we set the plugins directly instead of using the preset. Happy coding 😄

4reactions
ruanodendaalcommented, Jan 14, 2019

This will not work with ‘Create React App’ as per the documentation: https://emotion.sh/docs/css-prop#babel-preset

You need to use the JSX Pragma instead: https://emotion.sh/docs/css-prop#jsx-pragma

Read more comments on GitHub >

github_iconTop Results From Across the Web

emotion/babel-preset-css-prop
This preset enables the css prop for an entire project via a single entry to the babel configuration. After adding the preset, compiled...
Read more >
emotion/babel-plugin examples
Learn how to use @emotion/babel-plugin by viewing and forking example apps that make use of @emotion/babel-plugin on CodeSandbox. ; @react-select/monorepoA ...
Read more >
How to setup create-react-app with twin.macro and emotion
Check the twin emotion + react docs for the babel config to use. This is where the documentation leaves room for your own...
Read more >
Quick How-to: Use the emotion `css` prop with create-react ...
yarn add @emotion/core react-app-rewired customize-cra --save yarn add @emotion/babel-preset-css-prop --dev. Replace scripts in package.json
Read more >
i got a problem when i use MUI with CRA + TS
Uncaught TypeError: Cannot read properties of undefined (reading ... at emotion-element-cbed451f.browser.esm.js:36:1 at renderWithHooks ...
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