Skip to content

Empty CSS variables are incorrectly minified #7288

@adamwathan

Description

@adamwathan

Describe the bug

This is valid CSS:

.my-class {
  --my-var: ;
}

Importantly, the value of the variable is a single space character. SvelteKit is minifying this CSS (in development and production) to this:

.my-class {
  --my-var:;
}

This is syntactically invalid and breaks the semantics of the original CSS.

Unfortunately this breaks compatibility with Tailwind CSS, as we rely on empty variables to make certain types of styles composable, which you can read about in this blog post if would like a deeper explanation: https://adamwathan.me/composing-the-uncomposable-with-css-variables/

esbuild used to have the same bug but it was fixed a long time ago: evanw/esbuild#760

I've confirmed that esbuild is minifying this code safely:

/* Input */
.a {
  --blur: ;
}

/* Output */
.a{--blur: }

...so I'm not sure where in the chain this is happening.

Reproduction

Here's a basic SvelteKit project that demonstrates the issue:

https://github.com/adamwathan/sveltekit-css-minification-bug/blob/main/src/routes/index.svelte

Logs

No response

System Info

System:
    OS: macOS 12.0
    CPU: (10) arm64 Apple M1 Max
    Memory: 1.34 GB / 64.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.0 - /var/folders/7p/9j_d6ys502scd371yx3nwfkm0000gn/T/fnm_multishells/57296_1645277079681/bin/node
    Yarn: 1.22.17 - /var/folders/7p/9j_d6ys502scd371yx3nwfkm0000gn/T/fnm_multishells/57296_1645277079681/bin/yarn
    npm: 8.1.0 - /var/folders/7p/9j_d6ys502scd371yx3nwfkm0000gn/T/fnm_multishells/57296_1645277079681/bin/npm
  Browsers:
    Brave Browser: 98.1.35.100
    Chrome: 98.0.4758.102
    Chrome Canary: 101.0.4897.0
    Firefox: 94.0.2
    Safari: 15.0
    Safari Technology Preview: 15.4
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.17
    @sveltejs/kit: next => 1.0.0-next.278
    svelte: ^3.46.0 => 3.46.4

Severity

blocking all usage of SvelteKit

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcompilerChanges relating to the compiler

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions