next.js: Error using Emotion component selectors with SWC

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: x64
      Version: Darwin Kernel Version 22.2.0: Fri Nov 11 02:08:47 PST 2022; root:xnu-8792.61.2~4/RELEASE_X86_64
    Binaries:
      Node: 16.18.1
      npm: 9.1.1
      Yarn: 1.22.19
      pnpm: 7.27.1
    Relevant packages:
      next: 13.1.6
      eslint-config-next: 13.1.6
      react: 18.2.0
      react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

SWC transpilation

Link to the code that reproduces this issue

https://codesandbox.io/p/sandbox/polished-browser-went6z

To Reproduce

The following is a minimal reproduction (used in the reproduction codesandbox):

import styled from "@emotion/styled";

const A = styled.div`
  color: red;
`;

const B = styled.div`
  color: blue;
  ${A} {
    color: green;
  }
`;

export default function Home() {
  return <B><A>A inside B</A></B>;
}

No extra steps are required to reproduce. Simply load the codesandbox and observe the resulting error.

Describe the Bug

I’m getting the error:

Error: Component selectors can only be used in conjunction with @emotion/babel-plugin, the swc Emotion plugin, or another Emotion-aware compiler transform.

When using component selectors as described in the “Targeting another emotion component” section of the emotion docs, I’m getting this error.

Expected Behavior

Since this reproduction repo uses the swc Emotion plugin, I would not expect this syntax to result in an error.

Given the example above, I would expect green text “Test” when rendering A inside B.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 15 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Just ran into this moving from styled components to emotion for MUI. We were using an options object to replace the “transient prop” behavior for “$” props. Passing the object directly causes this error (only when compiling in dev mode). You can avoid the error by supplying a literal instead - but you don’t actually have to rewrite all of the code for shouldForwardProp each time - instead I’ve discovered you can simply spread the object out into an empty object literal, so:

Instead of this:

export const transientOptions: Parameters<CreateStyled>[1] = {
  shouldForwardProp: (propName: string) => !propName.startsWith("$"),
};
....
// This will cause an error with emotion/SWC
export const TimelineEvent = styled(Box, transientOptions)<{ $prop: boolean; }>` ... code which contains component selectors...`

You can just do this, which works fine:

export const TimelineEvent = styled(Box, {...transientOptions})<{ $prop: boolean; }>`... code which contains component selectors...`

Also, I didn’t enable emotion in the compiler options because the documentation claims that it’s enabled by default, at least when running in dev (which is very confusing btw):

I believe the “default is true” comment is referring to the sourceMap setting:

// default is true. It will be disabled when build type is production.
sourceMap?: boolean,

If compiler.emotion is not set in the next.config.js, then the Emotion plugin will not be enabled: https://github.com/vercel/next.js/blob/287ad83399fa6e90deae4ab75a5f9f762adc45e7/packages/next/src/build/swc/options.ts#L153-L155

Now what’s really strange is that if I do set emotion: true in my next.config.js, then this particular feature suddenly starts working. This works even without installing @swc/plugin-emotion.

A few things on this:

  1. The Emotion plugin is only needed if you want to use features like: minification, dead code elimination, source maps, and “components as selectors”
  2. You don’t need to explicitly install @swc/plugin-emotion, as it comes with Next by default. The plugin will be enabled if emotion is configured in next.config.js

Just ran into this moving from styled components to emotion for MUI. We were using an options object to replace the “transient prop” behavior for “$” props. Passing the object directly causes this error (only when compiling in dev mode). You can avoid the error by supplying a literal instead - but you don’t actually have to rewrite all of the code for shouldForwardProp each time - instead I’ve discovered you can simply spread the object out into an empty object literal, so:

Instead of this:

export const transientOptions: Parameters<CreateStyled>[1] = {
  shouldForwardProp: (propName: string) => !propName.startsWith("$"),
};
....
// This will cause an error with emotion/SWC
export const TimelineEvent = styled(Box, transientOptions)<{ $prop: boolean; }>` ... code which contains component selectors...`

You can just do this, which works fine:

export const TimelineEvent = styled(Box, {...transientOptions})<{ $prop: boolean; }>`... code which contains component selectors...`

I just ran into this problem. I’m really happy that I found your reply. In my case it was also caused by transient props and your workaround works for me aswell. Would have never guessed that this was causing the problem. So thank you so much for sharing your solution.

I’m sorry to say it feels like the compiler.emotion config does more damage than good, at present I feel like I have to mark most of my components that do not need for 'use client'; with /** @jsxImportSource react */, I’m not sure if it’s the compiler option fault, or the TSConfig compilerOptions.jsxImportSource: "@emotion/react", I ended up disabling both and everything worked ok, so how are these 2 options supposed to be used then?

I think the transform for component selectors is not implemented in the swc plugin.

https://github.com/swc-project/plugins/tree/main/packages/emotion/transform

It’s totally okay! Not blaming you for anything 😃

The issue is resolved for me. Just might be a good opportunity to improve the docs a bit.