0

In spite of the very application-specific sounding title, I'm hopeful this is really a more generic VITE / SCSS question that some guru can help translate and share some knowledge with me.

We are using PrimeFlex in our project and following the documented pattern that states:


Reuse Classes

Commonly used class blocks can be merged as a single class to be reused using the styleclass mixin. Import the utils from primeflex/utils to be able to create your own classes based on PrimeFlex classes. Here is how to create a class called mybutton with the built-in features.

@import 'primeflex/primeflex.scss';

.mybutton {
    @include styleclass('bg-blue-500 text-white hover:bg-blue-600 border-round transition-colors transition-duration-150 p-3 border-none');
}

So... following my interpretation of that text, our resulting VUE component files look roughly like this:

<script setup lang="ts">
...
</script>

<template>
...
</template>

<style scoped lang="scss">
@import 'primeflex/primeflex.scss';

.header-block {
  @include styleclass('flex flex-column flex-1 px-2 py-0');
}
.header-lbl {
  @include styleclass('text-sm');
}
.header-val {
  @include styleclass('text-lg font-bold');
}
</style>

We like this pattern in theory as it lets us combine and overload PrimeFlex helper classes into block-specific consolidated classes... but we are finding that for each component where we use this pattern, we increase the size of our VITE-built main index.css file by the size of the entire PrimeFlex CSS file (~400KB).

Our main index.css has quickly grown to over 10MB and is obviously out of hand.

Any recommendation on how we can use the suggested @import and @include styleclass(...) pattern without blowing up our build size?

I do notice in the documentation linked above that they then go on in the next section to recommend using something like PurgeCSS to reduce production build size in general. They don't explicitly relate the class reuse pattern with the need to use PurgeCSS, but perhaps one does necessitate the other?

I hate to pull another tool into our stack until I know for sure it's truly required and there is no better answer in pure SCSS syntax or VITE config magic.

slumtrimpet
  • 3,159
  • 2
  • 31
  • 44
  • I notice that the size of [PrimeFlex via CDN](https://unpkg.com/primeflex@3.3.0/primeflex.css) is just 31KB even though it is not minified. So if you get 400KB increase for each CSS file, it must be something else along with PrimeFlex being the culprit. You can try to use scss [`additionalData` option](https://vitejs.dev/config/shared-options.html#css-preprocessoroptions) instead of importing the file in scoped style. – Duannx Apr 25 '23 at 03:21
  • @Duannx Thank you for the suggestion. We've found that using the aditionalData vite hook actually adds the +400KB to EVERY file and jumps our resulting build from 10MB to over 30MB. The file size you are seeing on the CDN URL is compressed. The raw CSS is indeed about 400KB. – slumtrimpet Apr 25 '23 at 04:43
  • Oh, I forgot the compression. I'm afraid that there are no straight solutions to your problem. Since you import an scss file into your scoped style, all the CSS rules inside that file will also be scoped and added to your final build. Here are some suggestions that you can try: 1. Don't split your CSS in this case (turn off build.cssCodeSplit). Because each split CSS file need to import all the CSS rule to stand alone and works correctly. 2. Move import out of the scoped style since it prevents mixing duplicated CSS. 3. Try to import just what you want (use `@import primeflex/src/file.scss`) – Duannx Apr 25 '23 at 07:07

0 Answers0