动态导入 Vite 的 CSS

huangapple go评论61阅读模式
英文:

Dynamic import vite css

问题

有一个通过API调用捕获的theme选项。我希望这个主题与我在Vue 3应用程序(使用vite)中创建的css文件(或它们的组合)相对应。

这些主题文件我想要“层叠”,这样我就可以有base.cssstyle5.css,它们将追加到其中。

我也在使用tailwind。

main.pcss

/* tailwind base */

@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';

我目前的做法(下面的代码)我不喜欢它有好几个原因,但这是我能够使其正常工作的唯一方法,使CSS能够正确导入并且不会导致FOUC。我希望组件和CSS能够一起加载。

所以这是我希望实现的事情:

  1. 在我指定的顺序加载样式表时没有FOUC
  2. 使用全局类样式(而不是作用域化),由tailwind用@apply等编译。
  3. 我希望完全移除组件,只导入CSS文件,以消除这一层间接性,但当我尝试在yarn build中使用它时,布局没有应用任何CSS。
  4. 如果我想对base.css, style1.css进行一些调整,我应该能够组合多个单独的CSS文件,如style1.css, style2.css。如你所见,我不能对它们进行层叠,它们只能完全复制粘贴,这显然不是理想的情况。
  5. 我希望确保每个“主题”(一组CSS)都被捆绑,而不是所有主题都被捆绑在一起(出于文件大小的原因)。这就是为什么我使用异步组件的原因。
英文:

I have a theme option I capture via API call. I want this theme to correspond with css files (or combinations of them) I have created in the Vue 3 app (using vite).

These theme files I would like to 'layer' so I can have base.css, and style5.css which would append to that.

I'm also using tailwind.

main.pcss

/* tailwind base */

@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';

The current way I'm doing this (below) I don't like it for several reasons, but it was the only way I could get it working where the CSS would import properly, and would not cause a FOUC. I want the component and the css to load together.

So these are the things I'm hoping to achieve

  1. No FOUC as the sheets load in the order I specify
  2. They use global class styles (not scoped), compiled by tailwind with @apply, etc.
  3. I would love to remove the component entirely and just import CSS files, to remove this layer of indirection, but when I tried that with yarn build the layout had none of css applied.
  4. Should multiple separate css files I can combine, like base.css, style1.css style1.css, style2.css (if I want to make 1 tweak to style 1). As you see below I can't layer them, they just have to be copy pasted in full which is obviously not ideal
  5. I want to make sure each 'theme' (group of css) is bundled and not all themes are bundled together (for file size reasons). This is why im using the async component

My layout component

<template>
  <component v-if="theme" :is="themeComponent">
    <slot />
  </component>
</template>

<script>
import { computed, defineAsyncComponent } from 'vue';
import { useStore } from '@/store';

export default {
  setup() {
    const store = useStore();
    const theme = computed(() => store.theme);

    const themeComponent = defineAsyncComponent(() => {
      if (theme.value === 'STYLE_1') return import('@/themes/Style1.vue');
      if (theme.value === 'STYLE_2') return import('@/themes/Style2.vue');
      return import('@/themes/StyleDefault.vue');
    });

    return {
      theme,
      themeComponent,
    };
  },
};
</script>

Here is my Style1.vue component:

<template>
  <slot />
</template>

<style>
/* font */

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

/* base */

body {
  @apply text-slate-800 bg-white;
  font-family: 'Inter', sans-serif;
}
.link {
  @apply hover:underline;
}
.wrapper {}
.container {
  @apply px-4 mx-auto max-w-[750px];
}
.header {
  @apply bg-gray-50;
}
.content {
  @apply pb-20 border-t-2 border-b-2 border-blue-300 ;
}
...

答案1

得分: 2

你可以直接导入你的CSS文件:

if (theme.value === 'STYLE_1') {
   import('@/assets/styles/style1.scss')
}

但是,由于你动态导入CSS,无法避免FOUC(无样式内容闪烁问题)。因为CSS是在JS和DOM加载之后才加载的。

英文:

You can import your CSS file directly:

if (theme.value === 'STYLE_1') {
   import('@/assets/styles/style1.scss')
}

But, since you import your CSS dynamically, there is no way to avoid FOUC. Because the CSS is loaded after JS and after the DOM loaded

huangapple
  • 本文由 发表于 2023年2月7日 02:09:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定