question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Reactions:6
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
wistercommented, 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

3reactions
sibbngcommented, 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,
        }],
      },
    },
  },
Read more comments on GitHub >

github_iconTop Results From Across the Web

nuxt styles won't be applied in production mode - Stack Overflow
i solved my issue by adding this paragraph to nuxt.config.js build configuration. that's actually what i figured out about it. i've had ......
Read more >
apply can only be used for classes in the same CSS tree ...
This is happening because Nuxt treats style tags on SFC's as a different CSS file/block and they only have whatever comes from PostCSS...
Read more >
Creating Server-side Rendered Vue.js Apps Using Nuxt.js
When using a JavaScript framework, your browser doesn't receive a complete page to display. Instead it gets a bunch of pieces and instructions...
Read more >
Server-Side Rendering (SSR) - Vue.js
Vue.js is a framework for building client-side applications. By default, Vue components produce and manipulate DOM in the browser as output.
Read more >
Nuxt3/Nest/GraphQL authentication from scratch - Teamhood
We are building full-stack Nuxt.JS/Nest.JS/GraphQL application with authentication, http-only cookies, validation and permissions.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found