Replies: 1 comment 1 reply
-
Hey @epavletic, I have pretty much the same issue and am trying to figure out the best path forward -- Did you end up solving this? Edit: to clarify, I think you could technically solve your problem by using |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This problem might be more about js than specifically about vanilla-extract, but since the gist of the issue concerns fetching one out of several available VE themes dynamically, I thought it made sense to ask here.
Today we’re using
vanilla-extract
in our internal design system, and has been doing so for a year and a half by now. We make extensive use of the theming capabilities, as we export several (20+) global themes from the component library, which we then use in our SPA. Anyways, our setup in the component library is pretty basic and adheres mostly to the examples in the current documentation:We define a theme contract:
Then, each theme implements the contract:
So far, so good. The way we’ve been handling this in our SPA up until now, is to export a
ThemeProvider
component from the library, which only has one job: make sure that the correct theme is loaded.We’d then import it from the component library, and place it at the top of the react tree in the SPA. It’s not super elegant, but is has worked quite nice up until this point and has enabled us to only ship one set of theme variables to the end user’s browser.
Fast forward to today, where I’m stuck trying to figure out how to use this setup in a NextJS server side context using
@vanilla-extract/next-plugin
.The new application will also handle the same set of multiple themes, so there’s a requirement that we don’t hardcode the theme, but resolve it in one way or another (server side request, environment variable, etc). And I have significant problems with getting just one theme to load, given the above setup in this new environment.
First attempt was to use the above ThemeProvider as is, similarly to how we do in the SPA. I got the
Critical dependency: the request of a dependency is an expression
warning as long as the export was part of the design-system component library. So as a first test, I removed it from the export. With that out of the way I tested to just brute forceimport
a specific theme, which worked, albeit with the issue of having a static import.As soon as I switched to a dynamic import though, things started breaking:
The end result being that all of the themes would be imported, one after another, and the result in the browser would be the look of the last imported theme, and x amount of duplicated sets of variables in the css file. This more or less in line with how I expect the bundler to treat dynamic imports, and is sort of reflected in the webpack docs about dynamic imports:
Which makes sense in a way, but it’s the total opposite of what I’m trying to do here 😅
From here I’ve tried a bunch of different approaches, but I’ve come up short in each case. So I’m hoping that someone other than me has found themselves with multiple themes and the need to resolve just one of them, perhaps even in NextJS (since its CSS resolving thingamajig probably plays a part here as well).
Happy to provide some details on the above if something is unclear – sadly, the component library/consumer application is closed-source, so I can’t provide a code sandbox :/
Extremely grateful for any pointers, at this point 🙏
Beta Was this translation helpful? Give feedback.
All reactions