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.

@emotion/babel-plugin with React 17 and Typescript 4 and webpack does not apply labels

See original GitHub issue

Current behavior:

Using the latest version of @emotion/css, @emotion/babel-plugin and react the labels to classnames using the css function do not apply at all (not even the default ones which should be applied in development).

image

image

.babelrc

"plugins": [
    "react-hot-loader/babel",
    [
      "@emotion",
      {
        "sourceMap": false,
        "autoLabel": "always",
        "labelFormat": "[filename]__[local]"
      }
    ]
  ],

To reproduce:

Used a custom sandbox to be able to edit the .babelrc file to show the plugin not working https://codesandbox.io/s/peaceful-volhard-vhdkp?file=/src/index.tsx

Check the index.tsx to see the usage, and the .babelrc config to see the settings configured as per the documentation.

Any setting in the .babelrc, or even when using inline options in the webpack config, nothing changes. Labels are always just the hashes, not even the default [local] is applied.

Expected behavior:

Components with className set to the @emotion/css css value should have labels

Note that it seems to work with Parcel (without TS) https://codesandbox.io/s/react-optional-chaining-demo-forked-ttuv5?file=/package.json

Environment information:

  • react version: 17.0.2
  • @emotion/css version: 11.1.3
  • @emotion/babel-plugin version: 11.2.0
  • webpack version: 4.41.2

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
nicmosccommented, Apr 6, 2021

Ok found the issue: in the tsconfig.json the jsx field was set to react meaning all <div /> would get converted to React.createElement and thus emotion could not find where to attach the label. (see https://github.com/emotion-js/emotion/issues/1748)

For anyone bumping into this and having issues with getting the compilation going with babel-loader and ts-loader here is what i have in my babel config:

module.exports = {
  presets: [
    ['@babel/preset-env', { useBuiltIns: 'usage', corejs: { version: 3 } }],
    ['@babel/preset-react', { runtime: 'automatic' }],
  ],
  plugins: [
    ['@emotion/babel-plugin', {
      sourceMap: true,
      autoLabel: 'always',
      labelFormat: '[filename]__[local]',
    }],
  ],
}

The automatic is needed otherwise the react babel preset failed to compile (see https://github.com/gatsbyjs/gatsby/issues/27694).

✌️

0reactions
iChenLeicommented, Apr 6, 2021

Sorry I don’t know that you care about build-time type checking. Yep, awesome-typescript-loader can work as well. In my solution you just need try to change the loader config order. [ 'babel-loader', 'awesome-typescript-loader' ] to [ 'awesome-typescript-loader' , 'babel-loader'] , I have no time to digest the deep reason why we need do it, but I assured that emotion’s add label action is work at nodejs side, the babel plugin running order is important. I can’t give u more info about it cause i am not a babel expert. 😄 @nicmosc

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issue with options when trying to serve with webpack - React ...
In the process of making a react-typescript with express/node backend. Having an issue running 'webpack serve' and getting this error. Not ...
Read more >
Introducing the New JSX Transform – React Blog
Although React 17 doesn't contain new features, it will provide support for a new version of the JSX transform. In this post, we...
Read more >
Install - Emotion
Emotion has an optional Babel plugin that optimizes styles by compressing and hoisting them and creates a better developer experience with source maps...
Read more >
@emotion/babel-plugin | Yarn - Package Manager
Babel plugin for the minification and optimization of emotion styles. @emotion/babel-plugin is highly recommended, but not required in version 8 and above ...
Read more >
babel/plugin-transform-react-jsx
React Classic Runtime. If you do not want (or cannot use) auto importing, you can use the classic runtime, which is the default...
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