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.

babel-plugin-emotion to support more packages

See original GitHub issue

Hey emotion dev-team. Storybook maintainer here. Got a weird request… Concerning https://github.com/emotion-js/emotion/tree/master/packages/babel-plugin-emotion

Storybook uses emotion, and it’s great, but we’ve got a conflict: If a user of storybook is also using emotion (happening more and more) potential conflicts of versions happen. When multiple version of emotion are in 1 frame, it mostly works, but theme contexts get “confused”.

So I came up with a solution so storybook would get control of the version inside itself (and addons) and users are in control of the emotion they are using. And the install of the user’s version would happen where they’d expect.

The solution was to wrap emotion packages by a package called @storybook/theming. It also happens to include the default themes.

So when I’m creating emotion components it looks like this:

import { styled } from '@storybook/theming';

Obviously the babel-plugin-emotion doesn’t detect this, and so it doesn’t do anything. Sad. For the most part it’s OK, except the autoLabel feature is sweeet. But the babel-plugin is a requirement for certain features it seems. I’m getting this:

Component selectors can only be used in conjunction with babel-plugin-emotion

So sorry for the long intro… here’s my question: What can I do to get babel-plugin-emotion to work on import { styled } from '@storybook/theming'; ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
Nitivecommented, Jun 13, 2019

I’m currently use the following workaround to resolve this problem.

I have added paths option to compilerOptions

"compilerOptions": {
  "paths": {
    // Workaround for https://github.com/emotion-js/emotion/issues/1203
    // Remove after https://github.com/emotion-js/emotion/pull/1220 gets merged
    "@original-emotion/styled": ["./node_modules/@emotion/styled"],
    "@emotion/styled": ["src/utils/emotion/styled.ts"]
  }
}

Then, I have created styled file like documentation said. I have used @original-emotion/styled alias to get original @emotion/styled

import styled, { CreateStyled } from '@original-emotion/styled'
import { Theme } from '../../themes/types'

export default styled as CreateStyled<Theme>

So now we can import styled from @emotion/styled, it will be typed

import styled from '@emotion/styled'

Not a perfect solution, but it works

1reaction
Andaristcommented, Aug 25, 2020

Yes, you should use it in combination with @emotion/react@11.0.0-next.15 and @emotion/styled@11.0.0-next.15

Read more comments on GitHub >

github_iconTop Results From Across the Web

Emotion 11
Emotion 11 is a slight evolution over the Emotion 10. It focuses mainly on the developer experience, TS types improvements, switches internals to...
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 >
@emotion/babel-plugin - npm
A recommended babel preprocessing plugin for emotion, The Next Generation of CSS-in-JS.. Latest version: 11.10.5, last published: 2 months ...
Read more >
babel-plugin-emotion - npm Package Health Analysis - Snyk
The npm package babel-plugin-emotion receives a total of 4,167,222 weekly downloads. As such, babel-plugin-emotion popularity was classified as a key ecosystem ...
Read more >
babel-plugin-emotion examples - CodeSandbox
Learn how to use babel-plugin-emotion by viewing and forking example apps that make use of babel-plugin-emotion on CodeSandbox.
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