vitejs / vite

Next generation frontend tooling. It's fast!
http://vitejs.dev
MIT License
65.59k stars 5.84k forks source link

LightningCSS minify issues #16354

Open predaytor opened 2 months ago

predaytor commented 2 months ago

Describe the bug

I noticed an unnecessary duplication of @supports rules when using the Lightning CSS minification, which also differs from the LightningCSS playground output.

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import browserslist from 'browserslist';
import { browserslistToTargets } from 'lightningcss';

// https://vitejs.dev/config/
export default defineConfig({
  css: {
    transformer: 'lightningcss',
    lightningcss: {
      targets: browserslistToTargets(browserslist('>= 0.25%')),
      drafts: {
        customMedia: true,
      },
    },
  },
  build: {
    cssMinify: 'lightningcss',
    // cssMinify: true,
  },
  plugins: [react()],
});

1st test (OKLCH):

Input:

.x {
  --x: oklch(61% 0.016 277.7);
}

Result:

.x {
    --x: #81828d;
}
@supports (color: lab(0% 0 0)) {
    .x {
        --x: #81828d;
    }
    @supports (color: color(display-p3 0 0 0)) {
        .x {
            --x: color(display-p3 0.50541 0.511328 0.549274);
        }
    }
    @supports (color: lab(0% 0 0)) {
        .x {
            --x: lab(54.6419% 0.899702 -5.96);
        }
    }
}

Expected result (LightningCSS playground):

.x {
    --x: #81828d;
}
@supports (color: lab(0% 0 0)) {
    .x {
        --x: lab(54.6419% 0.899702 -5.96);
    }
}

///

2nd case (display-p3):

Input:

.x {
  --x: color(display-p3 0.004 0.278 0.933 / 0.173);
}

Result:

.x {
    --x: #0051e52c;
}
@supports (color: color(display-p3 0 0 0)) {
    .x {
        --x: #0051e52c;
    }
    @supports (color: color(display-p3 0 0 0)) {
        .x {
            --x: color(display-p3 0.004 0.278 0.933/.173);
        }
    }
}

Expected result (LightningCSS playground):

.x {
    --x: rgba(0, 81, 229, 0.173);
}
@supports (color: color(display-p3 0 0 0)) {
    .x {
        --x: color(display-p3 0.004 0.278 0.933/.173);
    }
}

Reproduction

https://stackblitz.com/edit/vitejs-vite-wdjgbn?file=src%2FApp.css,vite.config.ts

Steps to reproduce

Run pnpm build to inspect /dist folder for CSS output

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.3 - /usr/local/bin/pnpm
  npmPackages:
    @vitejs/plugin-react: ^4.2.1 => 4.2.1 
    vite: ^5.2.7 => 5.2.8

Used Package Manager

pnpm

Logs

No response

Validations

stackblitz[bot] commented 2 months ago

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.