I am making a web component in Vanilla JS that use a hidden select in the background and a div and ul>li in front. It became a bit complex with fetching data from an api, ect, so I transitioned to Svelte to simplify it and make it more readable.
Now I've tried for 2 days to export the component as an IIFE. I just can't seem to figure out how. I might be mistaken, but I thought that was one of the main features of Svelte - to make reusable components that can be used anywhere. Making it was the easy part, but now I want to load it and use it directly in the browser (with <script src=""></script>). I thought that should be easy?
I use Svelte 3 (3.57.0) with Vite 4 (4.2.1), and I have tried both npm create svelte to create a library project with SvelteKit and npm init vite with svelte as framework.
I've read quite a lot of the documentation for Vite and Svelte, but it feels overwhelming and I can't seem to find a configuration that works.
Does anyone know how to compile components to IIFEs in Svelte?
By default, SvelteKit only pre-processes components so they can be used by any Svelte project. The Svelte files are then only compiled to plain JS in the projects that use the component library.
You could add a separate Vite config to fully compile the components to JS files, e.g.
This build operates on the output of SvelteKit which is written to
distand creates a separate JS output indist-js.To run Vite with a specific config you can use the
-cargument:This could e.g. be added as a script in
package.json.Vite in library mode automatically outputs an ES module file that requires an
importand a UMD file which, when no other loaders are in scope, will define the components globally on an object with the name given inbuild.lib.name.So in the above example, if you export
Componentfrom theindex.jsfile, you could construct it vianew Components.Component({ ... })with the client-side API.