swc icon indicating copy to clipboard operation
swc copied to clipboard

Error when using Emotion Component Selector feature

Open yvuong opened this issue 3 years ago • 7 comments

Describe the bug

We're currently attempting to switch from Babel to SWC. We also use Emotion to style our components in our React project. One of Emotion's feature's we heavily use is Component Selectors. This seems to cause a runtime error with SWC.

To use this feature with babel, it requires a plugin so this may require porting it over. However I'm fairly new to this so it's possible this may have already been handled and there could just be some configuration I'm missing. This is a blocker from having our organization switch over from babel to SWC though, so any help is greatly appreciated.

Input code

import React from 'react';
import * as ReactDOM from 'react-dom';
import styled from '@emotion/styled';

const Inner = styled.div`
  font-weight: bold;
`;

const Outer = styled.div`
  color: blue;

  ${Inner} {
    color: red;
  }
`;

const App = () => (
  <Outer>
    This should be blue.
    <Inner>
      This should be bold and red.
    </Inner>
  </Outer>
);

ReactDOM.render(<App />, document.getElementById('root'));

Config

{
  "exclude": ["/\/node_modules\//"],
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": true,
      "decorators": true
    },
    "transform": {
      "react": {
        "pragma": "React.createElement",
        "pragmaFrag": "React.Fragment",
        "importSource": "@emotion/react"
      }
    },
    "target": "es2015",
    "loose": false,
    "externalHelpers": false,
    "keepClassNames": false
  },
  "module": {
    "type": "commonjs"
  }
}

Playground link

No response

Expected behavior

The components should render as expected.

Actual behavior

Getting a runtime error: Uncaught Error: Component selectors can only be used in conjunction with @emotion/babel-plugin.

Screen Shot 2022-03-17 at 10 49 56 AM

Version

@swc/cli: "^0.1.55", @swc/core: "^1.2.157", swc-loader: "^0.1.15",

Additional context

No response

yvuong avatar Mar 17 '22 18:03 yvuong

Hi @kdy1, thanks for taking a look. I'm sure you guys are very busy already, but I was wondering if there's an ETA for this since this is a blocker for our organization. An ETA would help with our planning and expectations. Thank you.

yvuong avatar Mar 17 '22 19:03 yvuong

This is not a bug of swc.

You need AST transform, and it can done via a plugin.

There's code for emotion transform in next.js repo, but I'm not sure when it will be released to npm

kdy1 avatar Mar 17 '22 19:03 kdy1

I asked, but I'm not sure at the moment.

kdy1 avatar Mar 18 '22 06:03 kdy1

@kdy1, thanks for pointing me to the next.js repo. I was wondering is there a reason why this is in next.js and not directly in SWC? Are there any plans to move it into SWC in the future, as my organization does not use next.js?

yvuong avatar Mar 28 '22 17:03 yvuong

why this is in next.js and not directly in SWC

It is due to those were external contributions into next.js, as well as it is not directly feasible to be included in @swc/core. If contributor (or core team member) willing to separate it out from next.js and potentially make it as swc core's plugin it'll be available.

kwonoj avatar Mar 28 '22 17:03 kwonoj

I see. Thank you @kwonoj

yvuong avatar Mar 28 '22 17:03 yvuong

Try this : import styled from '@emotion/styled/macro'; instead of import styled from '@emotion/styled';

glowindadark avatar Sep 19 '22 18:09 glowindadark

afaik we published emotion plugin. if problem persists with it, may need to provide repro against plugin repo.

kwonoj avatar Jul 24 '23 19:07 kwonoj

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

swc-bot avatar Aug 24 '23 00:08 swc-bot