As explained in the github repo redradix/svelte-custom-element-template:
Building custom elements with Svelte is really easy but have a lot of limitations, in this template I'm trying to show the way I solve most of these limitations.
3. inject the generated css into the shadowRoot node
To catch the generated css I modified rollup-plugin-css-only locally to push the generated css on each changes (rebuild)
// ./.rollup/css-only.js...generateBundle:functiongenerateBundle(opts,bundle){// Combine all stylesheets, respecting import ordervarcss='';for (varx=0;x<order.length;x++){varid=order[x];css+=styles[id]||'';}// Emit styles through callbackif (typeofoptions.output==='function'){options.output(css,styles,bundle);return;}...
Then inject the css right into the bundle (π± Hack alert!) with one important caveat which is that the wrapper web component has to have a style set π₯.
For my actual understanding is that svelte will inject dynamically computed styles into the head/document, and this won't allow the transition to apply into the shadow dom. That's why we need to replace the document injection by the shadow dom node.
We have a web component that wraps a svelte app and supports typescript and scss out of the box, with a DX (developer experience) that allows you to change the code and rebuild it automatically.
Building custom elements with Svelte is really easy but have a lot of limitations, is this template I'm trying to show the way I solve most of these limitations.
I hope this will help everyone trying to create custom element using all the goodness provided by svelte. I would love to find something less hacky provided by the svelte contributors. But I am still very happy of the result.
Feel free to share your experiences with web components and svelte, ideas for improvement or just say hi π