See https://www.appsoftware.com/tools/utilities/calculators for a description of the Svelte Custom Element configuration demo maintained here.
Using this configuration you can include these components in any web project. Change the outDir in each vite.config.<component>.js as required depending on where you want to reference <component>.iife.js from.
You can use this project as a template for your own Svelte Custom Element components. If you don't want to inherit global styles (Bulma CSS is used in this sample project), you can enable Shadow DOM encapsulation by removing shadow: "none" from the customElement configuration in the .svelte component files, or by changing it to shadow: "open".
Each component is compiled as an IIFE-format bundle that can be included in any HTML page. The bundle executes immediately when loaded, automatically registering the component as a custom element.
<svelte:options customElement={{ tag: "image-processor", shadow: "none" }} />
Build the components:
npm run build
Serve the demo pages:
npm run serve
Or build and serve in one command:
npm start
Open in browser:
forms.html)<add-calculator>, <multiply-calculator>public/js/forms.iife.jstext-formatter.html)<text-formatter>public/js/text-formatter.iife.jsimage-processor.html)<image-processor>public/js/image-processor.iife.jsAll components use:
To use these components in your own project:
Include the JavaScript bundle:
<script src="path/to/component.iife.js"></script>
Use the custom elements:
<add-calculator></add-calculator>
<multiply-calculator></multiply-calculator>
<text-formatter></text-formatter>
<image-processor></image-processor>
Include Bulma CSS:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">