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

Nuxt3 - @apply directives in <style> not correctly parsed in production #1849

Closed
wister opened this issue Nov 7, 2022 · 12 comments · Fixed by #1923 or #1930
Closed

Nuxt3 - @apply directives in <style> not correctly parsed in production #1849

wister opened this issue Nov 7, 2022 · 12 comments · Fixed by #1923 or #1930

Comments

@wister
Copy link

wister commented Nov 7, 2022

Nuxt 3.0.0-rc.13 and unocss 0.46.3
Problem: The @apply directives inside <style> tag in SFC in Nuxt3 are not correctly parsed when in PRODUCTION. For some reason when there are multiple selectors that use @apply, the selectors are grouped and the styles are from the last selector. For example:

.test {
  @apply bg-gray4;
}
.test2 {
  @apply bg-green4;
}

This will generate the MagicString .test,.test2{@apply bg-green4} for the transformerDirectives() function.

When there is another style that does not use @apply between the ones that do. The styles will result correctly parsed. For example:

.test {
  @apply bg-gray4;
}
.test3 {
  background-color: orange;
}
}
.test2 {
  @apply bg-green4;
}

Reproduction: https://stackblitz.com/edit/github-zoupmi?file=components%2FTest.vue (fork of https://github.com/antfu/vitesse-nuxt3)

npm run build
npm run start

The Test.vue SFC is correctly parsed when in DEVELOPMENT

I believe this might be a core bug?

Thank you for your time

@minkoonim
Copy link

yeah! exact same case here for @screen

@zyyv zyyv added bug Something isn't working directives and removed bug Something isn't working directives labels Nov 7, 2022
@zyyv
Copy link
Member

zyyv commented Nov 7, 2022

Hi, i found it works well with CSR, without nuxt.🤔️

@therealokoro
Copy link
Contributor

So I encountered same issue too. My walkaround was to use the --at-apply directive in place of the @apply.
See example below

/** instead of this **/
.test2 {
  @apply bg-green4;
}

/** do this **/
.test2 {
  --at-apply: "bg-green4";
}

This works fine without any issues but of-course you'll have to opt-in to this feature with the transformerDirectives transformer set in your config

import { transformerDirectives() } from "unocss"
export default defineConfig({
    transformers: [transformerDirectives()]
})

@wister
Copy link
Author

wister commented Nov 7, 2022

@therealokoro Thank you that does work.

I tried debugging a little bit more. I am wondering when is the MagicString generated? I commented out '@unocss/nuxt', from nuxt.config.ts. And I got this error from npm run build:

 WARN  warnings when minifying css:                                          14:52:27
 [WARNING] Expected ";" but found "}" [css-syntax-error]

    <stdin>:1:29:
      1 │ .test,.test2{@apply bg-green4}.test3{background-color:orange}.test5...
        │                              ^
        ╵                              ;

As you can see, it is trying to minify .test,.test2{@apply bg-green4}, the selectors are already incorrectly grouped.

Is this a Nuxt3 / bundlers bug?

Thank you

@therealokoro
Copy link
Contributor

@wister i'm glad the walkaround worked for you. Until this is fixed I guess this will be the only way to use it.

@maoosi
Copy link

maoosi commented Nov 10, 2022

Same issue for me, @therealokoro thanks for the temporary workaround!

@therealokoro
Copy link
Contributor

@maoosi my pleasure.

I think it's worth mentioning that this isn't even a nuxt3 issue. I encountered this problem while using plain vue 3

@Saganic
Copy link

Saganic commented Nov 18, 2022

Any news on this?

@ijkml
Copy link

ijkml commented Nov 26, 2022

Ran into this today, too...

There's a workaround! Phew 😅.

ijkml added a commit to ijkml/Voe that referenced this issue Nov 27, 2022
... workaround for unocss weird bug (unocss/unocss#1849)
@ijkml
Copy link

ijkml commented Nov 27, 2022

The docs here: https://github.com/unocss/unocss/tree/main/packages/transformer-directives tells you to quote values that contain :. When moving from @apply to --at-apply, I noticed the values in quotes don't work (with SCSS, btw).

Bug? 🤷‍♂️

@sibbng
Copy link
Member

sibbng commented Nov 27, 2022

You can fix this issue by enforcing pre processing:

  transformers: [
    transformerDirectives({ enforce: 'pre' }),
  ],

Or, you could disable cssnano mergeRules option by adding these lines to your nuxt.config.ts:

  postcss: {
    plugins: {
      cssnano: {
        preset: ['default', {
          mergeRules: false,
        }],
      },
    },
  },

@therealokoro
Copy link
Contributor

Yeah thats true @sibbng I remember using this fix in one of my old projects. Totally forgot about it. Thanks mate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
8 participants