#How to import file programmatically?

4 messages · Page 1 of 1 (latest)

marsh skiff
#

Currently, I have this logic in the layout file:

if (x === 1) {
  await import('../assets/scss/style-01.scss')
} else if (x === 2) {
  await import('../assets/scss/style-02.scss')
}

However, both styles is imported while rendering. Do you have any idea to solve this issue?

raw moon
#

no there's not. That's because Vite is smart, parses all imports and turn them to static, at the same time time Vite cannot evaluate your variables state to know what to do. A real dynamic import will also disable Vite's processing path so not usable.
It feels like a dark light mode that's build time fixed.
Why not assign classes to html conditionally ?

marsh skiff
#

That's legacy codes. At this moment, it's not ready to be re-written.

#

Btw, I solved it.