Skip to content

Deduplication swallows variable when name and value are identical (custom PostCSS plugin) #11473

Closed
@peterhirn

Description

@peterhirn

What version of Tailwind CSS are you using?

v3.3.2

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

postcss 8.4.24, vite 4.3.9

What version of Node.js are you using?

v20.3.0

What browser are you using?

Chrome

What operating system are you using?

Windows 10

Reproduction

tailwind.config.js

/** @type {import('tailwindcss').Config} */
export default {
  content: ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"],
  plugins: []
}

postcss.config.js

import { basename } from "node:path"
import tailwindcss from "tailwindcss"
import autoprefixer from "autoprefixer"

const appendMode = () => ({
  postcssPlugin: "variables-append-mode",
  Declaration(decl) {
    if (
      decl.variable &&
      decl.prop.startsWith("--md-") &&
      !decl.prop.endsWith("-light") &&
      !decl.prop.endsWith("-dark")
    ) {
      switch (basename(decl.source.input.file)) {
        case "tokens.light.css":
          decl.prop = `${decl.prop}-light`
          break

        case "tokens.dark.css":
          decl.prop = `${decl.prop}-dark`
          break
      }
    }
  }
})

appendMode.postcss = true

export default {
  plugins: [appendMode, tailwindcss, autoprefixer]
}

tokens.light.css

:root,
:host {
  --md-sys-color-primary: #005079;
  --md-sys-color-secondary: #4b6174;
  --md-sys-color-outline: #6e757d;
}

tokens.dark.css

:root,
:host {
  --md-sys-color-primary: #8fcdff;
  --md-sys-color-secondary: #b2c9df;
  --md-sys-color-outline: #6e757d;
}

index.css

@import "./tokens.light.css";
@import "./tokens.dark.css";

@tailwind base;
@tailwind components;
@tailwind utilities;

Describe your issue

I have a set of two css files which declare variables with identical variable names (tokens.light.css and tokens.dark.css). I'm using this minimal PostCSS plugin to automatically append -light/-dark to the variable names depending on which file they were declared in. Additionally I also convert the hex colors to rgb but this is not relevant here.

This works perfectly until a variable has not only the same name but also the same value, see --md-sys-color-outline above. In this case one of the variants is not created, here --md-sys-color-outline-light is missing.

Expected

:root, :host {
    --md-sys-color-primary-light: #005079;
    --md-sys-color-secondary-light: #4b6174;
    --md-sys-color-outline-light: #6e757d;
    --md-sys-color-primary-dark: #8fcdff;
    --md-sys-color-secondary-dark: #b2c9df;
    --md-sys-color-outline-dark: #6e757d;
}

Actual

:root, :host {
    --md-sys-color-primary-light: #005079;
    --md-sys-color-secondary-light: #4b6174;
    --md-sys-color-primary-dark: #8fcdff;
    --md-sys-color-secondary-dark: #b2c9df;
    --md-sys-color-outline-dark: #6e757d;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions