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

CompileError [ParseError]: :global() must contain a selector #613

Open
lewispham opened this issue Sep 11, 2023 · 2 comments
Open

CompileError [ParseError]: :global() must contain a selector #613

lewispham opened this issue Sep 11, 2023 · 2 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@lewispham
Copy link

lewispham commented Sep 11, 2023

Describe the bug
I'm using stylus as the css preprocessor. This syntax is no longer working and throwing an error instead. It looks like the processed code does not add the necessary brackets for keyword :global.

:global
    div
        display block
    p
         display inline-block

Logs

    '5: \n' +
    '6: <style lang="stylus">:global div,\n' +
    '                        ^\n' +

To Reproduce
Try the above code

Expected behavior
No error occurs.

Stack trace

Stack trace goes here...

Information about your project:

  • Your browser and the version: No need

  • Your operating system: Debian bookworm

  • svelte-preprocess version 5.0.4

  • Rollup

Additional context
It used to work.

@dummdidumm dummdidumm added bug Something isn't working help wanted Extra attention is needed labels Sep 11, 2023
@andy0130tw
Copy link

andy0130tw commented Sep 21, 2023

I also ran into this bug earlier this day. Tried downgrading this plugin (4.x ~ 5.0.3) and Svelte (3.x) but have no success. For anyone looking into this issue, I created a minimal reproduction.

https://stackblitz.com/edit/svelte-preprocess-global-fail

The dependencies are:

  {
    "@sveltejs/vite-plugin-svelte": "^2.4.6",
    "postcss": "^8.4.30",
    "sass": "^1.68.0",
    "svelte": "^4.2.0",
    "svelte-preprocess": "^5.0.4",
    "vite": "^4.4.9"
  }

And the Vite config:

export default defineConfig({
  plugins: [
    svelte({
      preprocess: [
        vitePreprocess({style: false}),
        /* add minimal preprocessors for demonstrating the bug */
        sveltePreprocess.scss(),
        sveltePreprocess.globalStyle(),
      ]
    }),
  ],
})

@andy0130tw
Copy link

andy0130tw commented Sep 23, 2023

I trace down the source code, and the issue is caused by the following lines in src/processors/globalStyle.ts:

if (!attributes.global) {
  return { code: content };
}

Literally, it skips the entire transformation if the style tag is not marked as global. Historically, the global styles feature of this preprocessor only supported global blocks, so it sounded logical. This is no longer correct if we need to support :global selectors inside the style body.

Also notice that the test suite does not cover this processor. Although the transformer behaves correctly, it is not run at all.

A quick solution is to remove the lines listed above. But beware that the transformation then runs through every style tag even if it does not contain any relevant selector, which impacts the performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants