I have a svelte component that someone else wants to use on their own website, which is pure HTML/JavaScript.
My plan is for them to use a script tag referencing a cdn I host, like this:
<script src="http://examplecdn.com/myfile.js"></script>
Do I need to setup the project in a certain way to facilitate this? How do I find the file for the specific component? I can create a new project with just this component, but don't want to have to convert the code away from svelte.
For some context, for testing I have set up a basic svelte project with:
npm create svelte@latest my-example-module
And have just added this code to test if I can get something simple working:
<script lang="ts">
let text = "IT IS WORKING"
</script>
<h1>{text}</h1>
<style>
h1 {font-size: 3rem}
</style>
I ran npm run build, and have a lot of compiled javascript, but am not sure what a next step would be.
The svelte team is really pushing users to use sveltekit, and for a certain type of project that might be the way to go, but IMHO in many cases another approach might be better suited and much simpler.
You can try these steps instead:
start the project using vite and select svelete template
I selected these options
Then you need to edit the
vite.config.jsfile and add build optionsI set to IIFE, but you can use any other format (es, cjs, umd etc) note that the default is setup to use modules, so you need to import it using
type="module":