next.js: Turbopack does not compile CSS nesting for styled-jsx

Link to the code that reproduces this issue

https://github.com/vpontis/nextjs-14-turbo-styled-jsx

To Reproduce

The Turbopack docs it says that it supports PostCSS nested which allows you to nest CSS selectors.

This would be very useful for us since we are currently using Sass just to be able to nest selectors. We would like to move off Sass / Babel and use Turbo.

But I think Turbopack doesn’t apply the PostCSS plugins to the CSS generated by Styled JSX.

For example, the following code:

export default function Home() {
  return (
    <>
      <div className={'container'}>
        container (should be blue)

        <div className="inner">
          container + inner (should be yellow)
        </div>
      </div>

      <style jsx>{`
        .container {
          color: blue;
          padding: 3rem;

          .inn {
            &er {
              color: yellow;
            }
          }
        }
      `}</style>
    </>
  )
}

Gives me this CSS:

.container {
    color: blue;
    padding: 3rem;

    .inn {
        &er {
            color: yellow
        }
    }
}

Where I would expect:

.container {
    color: blue;
    padding: 3rem;
}

.container .inner {
    color: yellow;
}

Current vs. Expected behavior

CSS nesting should be compiled but it’s not.

Verify canary release

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

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.1.0: Mon Oct  9 21:27:24 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T6000
Binaries:
  Node: 18.16.1
  npm: 9.5.1
  Yarn: 1.22.19
  pnpm: 8.10.0
Relevant Packages:
  next: 14.0.0
  eslint-config-next: 14.0.0
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.2.2
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

SWC transpilation, Turbopack (–turbo)

Additional context

This would be super super helpful to us to get working 😃. Happy to look into this if you’d like to point me in the right direction 😃

PACK-2106

About this issue

  • Original URL
  • State: closed
  • Created 8 months ago
  • Reactions: 2
  • Comments: 35 (19 by maintainers)

Commits related to this issue

Most upvoted comments

Hey Victor, I did some further digging and you’re right that styled-jsx’s CSS processing does not downlevel currently, however that is a bug. We’ve discussed a fix and the initial work by kdy has been done for that here: https://github.com/swc-project/plugins/pull/275. For Turbopack, which uses https://lightningcss.dev/ what was missing is passing the browserslist target (i.e. the default browserslist target), which we’re also adding. When both those changes land it will downlevel the nested selectors 👍 Will post back here when that is ready.

looked into it, I think one thing that @kdy1 might have overlooked is that the browserlists might not get consumed properly. We’re investigating.

@vpontis, what @kdy1 is trying to articulate, I think, is:

  • some degree of support was added in Turbopack for transpiling nested CSS selectors when the browserlist is specified
  • however, since we don’t support turbopack for builds ATM, it won’t fix production issues for you
  • so @kdy1 suggestion was that you should still use webpack for this (the default ATM)
  • you should use the babel config you linked to, just without running --turbo