Dynamic imports?
Is there a similar straightforward approach to import modules dynamically in SolidJS?
In Svelte it's as simple as:
It helps with non-ssr-friendly module imports. I was wondering if SolidJS has something similar? :))
8 Replies
Thanks, I couldn't find examples of it being used for just plain javascript modules tho. Not components.
For instance:
A ye u can just use
await import
Vite will resolve relative imports and create a chunk for each import
U get proper typing for relative impors
And u can also use it for external dynamic imports too, it's a web feature
Import esm modules from esm.sh
for example
That's the secret sauce of why u can import external modules with the solid playgroundHmm yeah wtf, it worked.
I cannot confirm if this is actually importing client-side tho lol.
-----------
Alright yup, it does work. The screenshot above is what happens if I do a regular ESM Import (isomorphic):
But when I do the await import (client-side only):
The log on the console (in VSCode) won't be there, which means it's only on client-side (browser).
this is because
onMount
will not be called on the server i think
you should be able to await import
on the server too i think, as long as ur in esm-mode. both dynamic and static imports are part of esm.Didn't know this before. Thanks! I was assuming it was a Svelte-specific feature.
You are welcome 😁