Skip to content

Bug: Styled-components are unstyled using React Server Components (the component contains all the necessary props)  #23126

Closed
@Thisisjuke

Description

@Thisisjuke

Context

Its seems that React Server Components do not Support CSS-in-JS at the moment.

Related to an issue in the Hydrogen Framework by Shopify : Shopify/hydrogen#398

Describe the bug
Hello, it seems I have encountered a bug using styled-components from external library.

  • Inside the demo of React Server Components:

The basic setup from the React Server Components can't compile, because of the package extension (see spoiler with full log below). This error seems weird :

Instead rename taggedTemplateLiteral.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/[...]/hydrogen-styled-components-error/apps/react-server-components-app/node_modules/@babel/runtime/helpers/esm/package.json.
  • Inside the default repo of Hydrogen App (see more in original issue):

Once the component is loaded in front-end, it's not flagged as "component" in react dev tools (other Hydrogen elements are) and has no CSS applied.

To Reproduce

Reproduction Repository

  1. Create a styled component like this one :
export const TestButton = styled.button`
  background: red;
  ${props => props.primary && css`
    color: blue;
  `}
`
  1. Build it with ES / UMD (using viteJS or rollup). The compiled version looks like this:
const en=Qr.button.withConfig({displayName:"TestButton",componentId:"sc-levoo9-0"})((({variant:e="primary"})=>[{"--tw-bg-opacity":"1",backgroundColor:"rgba(239, 68, 68, var(--tw-bg-opacity))"},e==="primary"&&{"--tw-text-opacity":"1",color:"rgba(59, 130, 246, var(--tw-text-opacity))"}]));export{en as TestButton};
  1. Add it to your application using yarn : yarn workspace react-server-components-app add @test/ui
  2. Import the component in your App : <TestButton>Yo !</TestButton>
  3. Launch your application => Impossible to compile

Expected behaviour
The site should compile and the button should have CSS props and shoudn't be unstyled.

Logs

Inside React Server Components application with button : error starting with yarn start-rsc
internal/modules/cjs/loader.js:1102
    throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
    ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/[...]/hydrogen-styled-components-error/apps/react-server-components-app/node_modules/@babel/runtime/helpers/esm/taggedTemplateLiteral.js
require() of ES modules is not supported.
require() of /home/[...]/hydrogen-styled-components-error/apps/react-server-components-app/node_modules/@babel/runtime/helpers/esm/taggedTemplateLiteral.js from /home/[...]/hydrogen-styled-components-error/libs/ui/dist/ui.umd.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename taggedTemplateLiteral.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/[...]/hydrogen-styled-components-error/apps/react-server-components-app/node_modules/@babel/runtime/helpers/esm/package.json.

  at new NodeError (internal/errors.js:322:7)
  at Module._extensions..js (internal/modules/cjs/loader.js:1102:13)
  at Object.newLoader [as .js] (/home/[...]/hydrogen-styled-components-error/node_modules/pirates/lib/index.js:141:7)
  at Module.load (internal/modules/cjs/loader.js:950:32)
  at Function.Module._load (internal/modules/cjs/loader.js:790:12)
  at Module.require (internal/modules/cjs/loader.js:974:19)
  at require (internal/modules/cjs/helpers.js:93:18)
  at Object.<anonymous> (/home/[...]/hydrogen-styled-components-error/libs/ui/dist/ui.umd.js:5:54)
  at Module._compile (internal/modules/cjs/loader.js:1085:14)
  at Module._compile (/home/[...]/hydrogen-styled-components-error/node_modules/pirates/lib/index.js:136:24) {
code: 'ERR_REQUIRE_ESM'
}
Inside hydrogen app : console.log(TestButton.componentStyle.rules[0]({variant: 'primary'}))
[
{
  transform: 'var(--tw-transform)',
  paddingLeft: '2rem',
  paddingRight: '2rem',
  paddingTop: '0.5rem',
  paddingBottom: '0.5rem',
  borderRadius: '0.25rem',
  ':focus': { outline: '2px solid transparent', outlineOffset: '2px' },
  transitionDuration: '75ms'
},
{
  ':hover, :focus': {
    '--tw-scale-x': '1.05',
    '--tw-scale-y': '1.05',
    transform: 'var(--tw-transform)',
    '--tw-text-opacity': '1',
    color: 'rgba(251, 191, 36, var(--tw-text-opacity))'
  }
},
{
  '--tw-bg-opacity': '1',
  backgroundColor: 'rgba(0, 0, 0, var(--tw-bg-opacity))',
  '--tw-text-opacity': '1',
  color: 'rgba(255, 255, 255, var(--tw-text-opacity))',
  '--tw-border-opacity': '1',
  borderColor: 'rgba(0, 0, 0, var(--tw-border-opacity))'
},
false,
{ fontSize: '0.875rem', lineHeight: '1.25rem' },
[ '\n    color: ', '#fff', ';\n  ', isCss: true ]
]

Additional context
In the reproduction repo, the same component from same build is used in:

  • React Server Component app : application can't compile
  • Hydrogen App : component is unstyled
  • create-react-app : component is styled
  • react with viteJS as bundler : component is styled

Versions & System

  • Node version : v14.18.1
  • Device details :
    OS: Linux 5.4 Ubuntu 20.04.3 LTS (Focal Fossa)
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Memory: 393.64 MB / 15.32 GB
    Container: Yes
    Shell: 5.8 - /usr/bin/zsh

Thx for your time !

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: UnconfirmedA potential issue that we haven't yet confirmed as a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions