Skip to content

vite-plugin-svelte chokes on custom media syntax when using vitePreprocess and Lightning CSS #733

Closed
@knpwrs

Description

@knpwrs

Describe the bug

I just spun up a new skeleton sveltekit project which by default uses vitePreprocess. I installed lightning css (npm i -D lightningcss) I have modified vite.config.ts to contain the following:

import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [sveltekit()],
  css: {
    transformer: 'lightningcss',
    lightningcss: {
      drafts: {
        nesting: true,
        customMedia: true,
      },
    },
  },
});

Given the following +page.svelte file, I can see the nesting configuration works:

<h1>Welcome to SvelteKit</h1>

<style>
  /* @custom-media --small-viewport (max-width: 30em); */

  h1 {
    color: blue;

    &:hover {
      color: red;
    }

    /* @media (--small-viewport) { */
    /*   color: green; */
    /* } */
  }

</style>

With the configuration set as in my first code block, hovering the <h1> turns it red. If I comment out the nesting configuration, hovering the <h1> does nothing.

However, if I uncomment the custom media, I get a syntax error:

Internal server error: /Users/knpwrs/Downloads/my-app/src/routes/+page.svelte:4:43 ")" is expected
  Plugin: vite-plugin-svelte
  File: .../my-app/src/routes/+page.svelte:4:43
   2 |
   3 |  <style>
   4 |    @custom-media --small-viewport (max-width: 30em);
                                                   ^

Reproduction URL

https://github.com/knpwrs/sveltekit-lightningcss-custom-media-issue

Reproduction

  1. npm create svelte@latest my-app
  2. cd my-app
  3. npm install -D lightningcss
  4. Update configuration as shown above
  5. Observe bug

Logs

logs.txt

System Info

System:
    OS: macOS 13.4
    CPU: (12) arm64 Apple M2 Max
    Memory: 1.26 GB / 64.00 GB
    Shell: 5.9 - /opt/homebrew/bin/zsh
  Binaries:
    Node: 20.3.1 - /opt/homebrew/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 9.6.7 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 116.0.5845.140
    Chrome Canary: 117.0.5863.0
    Safari: 16.5
  npmPackages:
    @sveltejs/adapter-auto: ^2.0.0 => 2.1.0
    @sveltejs/kit: ^1.20.4 => 1.24.0
    svelte: ^4.0.5 => 4.2.0
    vite: ^4.4.2 => 4.4.9

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions