Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v4] light-dark is broken in optimized build #15438

Closed
imjeehoo opened this issue Dec 18, 2024 · 11 comments · Fixed by #15576
Closed

[v4] light-dark is broken in optimized build #15438

imjeehoo opened this issue Dec 18, 2024 · 11 comments · Fixed by #15576

Comments

@imjeehoo
Copy link

imjeehoo commented Dec 18, 2024

What version of Tailwind CSS are you using?

4.0.0-beta.8

What build tool (or framework if it abstracts the build tool) are you using?

Next.js 15.1.1, postcss 8.4.49

What version of Node.js are you using?

Bun 1.1.40

What browser are you using?

Safari

What operating system are you using?

macOS 15.2

Describe your issue

Original description
I defined my own color: `@theme { --color-action-sheet-cancel: light-dark(white, #2c2c2e); }`

Color works fine in dev, but broken in production

bun run dev
image

bun run build && bun run start
image

With optimization enabled, LightningCSS exclusively respects the color-scheme declaration set on the html element, not elsewhere.

<html className="scheme-light dark:scheme-dark">
  <...>
    <div className="text-foreground">Hello</div>
    <div className="text-foreground scheme-only-dark">Hello</div>
  </...>
</html>

Without optimization:
image

With optimization:
image

@imjeehoo imjeehoo changed the title [v4] light-dark is broken [v4] light-dark is broken in optimized build Dec 18, 2024
@imjeehoo
Copy link
Author

imjeehoo commented Dec 18, 2024

The workaround is to explicitly disable optimize option in production by updating postcss.config.mjs:

/** @type {import('postcss-load-config').Config} */
const config = {
  plugins: {
    '@tailwindcss/postcss': {
      optimize: false,
    },
  },
};

export default config;

@adamwathan
Copy link
Member

Hey! I'm guessing this is because you are missing a color-scheme declaration in your CSS somewhere:

https://lightningcss.dev/transpilation.html#light-dark()-color-function

Are you using the <meta> tag instead? Maybe we should configure Lightning to just not transpile light-dark at all.

@imjeehoo
Copy link
Author

imjeehoo commented Dec 19, 2024

Hey! I'm guessing this is because you are missing a color-scheme declaration in your CSS somewhere:

https://lightningcss.dev/transpilation.html#light-dark()-color-function

Are you using the <meta> tag instead? Maybe we should configure Lightning to just not transpile light-dark at all.

Yes, I omitted the color-scheme declaration in my CSS, as I was setting it on specific elements using Tailwind scheme-dark dark:scheme-light and JavaScript.

After experimenting, I discovered that with optimization enabled, LightningCSS exclusively respects the color-scheme declaration set on the html element, not elsewhere.

<html className="scheme-light dark:scheme-dark">
  <...>
    <div className="text-foreground">Hello</div>
    <div className="text-foreground scheme-only-dark">Hello</div>
  </...>
</html>

Without optimization:
image

With optimization:
image

@imjeehoo
Copy link
Author

imjeehoo commented Dec 19, 2024

Following is an example code based on light-dark MDN.

Due to how it transpiles light-dark, we cannot put light-dark in variable and use it later in optimized build.

All of them produce light mode in optimized build.

:root {
  --color-background: light-dark(white, black);
  --color-foreground: light-dark(black, white);
}

* {
  background-color: var(--color-background); /* not working in optimized build */
  color: var(--color-foreground); /* not working in optimized build */

  background-color: light-dark(white, black); /* working */
  color: light-dark(black, white); /* working */
}
{/* Assuming parent html tag has `scheme-light dark:scheme-dark` */}
<h1>
  <code>light-dark()</code> CSS function
</h1>
<section>
  <h2>Automatic</h2>
  <p>
    This section will react to the users system or user agent setting.
  </p>
</section>
<section className="scheme-light">
  <h2>Light</h2>
  <p>
    This section will be light due to the{" "}
    <code>color-scheme: light;</code>.
  </p>
</section>
<section className="scheme-dark">
  <h2>Dark</h2>
  <p>
    This section will be dark due to the <code>color-scheme: dark;</code>.
  </p>
</section>

@danie-ramdhani
Copy link

Is this same case with #15018 ?

@imjeehoo
Copy link
Author

imjeehoo commented Dec 19, 2024

Is this same case with #15018 ?

The issue seems to be that the light-dark setting in @theme relies solely on the color-scheme property of the <html> tag.

I’m not sure if the standard allows omitting color styles when color-scheme isn’t set, although most browsers appear to fallbacks to the system’s preferred color scheme. If the standard doesn’t allow it, your closed issue should be addressed in my opinion.

Personally, I find it frustrating that Lightning CSS introduces random caveats that cause differences between development and production. Since I use Tailwind, I don’t expect to deal with its dependencies’ caveats. I want it to handle things correctly, behave exactly as specified by the supported version of the standard, and remain consistent across development and production environments.

I believe Tailwind team’s already aware, but I want to emphasize again that encountering these silently introduced inconsistencies between development and production is a horrible experience for a developer. In fact, it’s even worse than facing a compile error.

@danie-ramdhani
Copy link

Yeah i know it's like lightningcss behaviour or something, but opening issue in in tailwindcss doesn't do anything. Try do it in lightningcss repo.

@imjeehoo
Copy link
Author

imjeehoo commented Dec 19, 2024

Yeah i know it's like lightningcss behaviour or something, but opening issue in in tailwindcss doesn't do anything. Try do it in lightningcss repo.

If Tailwind doesn’t consider it their bug (which I think is understandable) because the transpilation issues are entirely derived from Lightning CSS, I’m willing to open an issue there.

However, since @adamwathan mentioned he can configure it not to transpile light-dark, which can fix this issue in Tailwind, I’m waiting to hear the Tailwind team’s perspective

@adamwathan
Copy link
Member

Yeah i know it's like lightningcss behaviour or something, but opening issue in in tailwindcss doesn't do anything. Try do it in lightningcss repo.

Nah definitely our problem, working on a solution already.

@adamwathan
Copy link
Member

Update — Lightning is going to expose an option for us to disable transpiling light-dark, has landed in master so will be able to close this one once Lightning tags a release and we can use that flag 👍

parcel-bundler/lightningcss@3043896

@imjeehoo
Copy link
Author

imjeehoo commented Jan 9, 2025

Thank you for keeping Tailwind best, and most correct framework!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants