Getting Started
The recommended starting point is the official template repo:
pnpx degit svebcomponents/template my-projectcd my-projectpnpm installpnpm devThe template gives you a small pnpm workspace:
components/example-component: a Svelte custom element packageapps/svelte-kit: a SvelteKit app that consumes the custom elementconfigs/*: shared linting, formatting, and TypeScript config packages
Example Component
Section titled “Example Component”Take a look at the example component in
components/example-component/src/ExampleComponent.svelte:
<script lang="ts"> let { increments = 1, }: { increments: number; } = $props();
let count = $state(0);</script>
<button onclick={() => (count += increments)}> add {increments} to {count}</button>If you know Svelte 5, there is nothing unfamiliar here. It is a regular Svelte component with typed props.
increments: number;The type annotation is enough information for @svebcomponents/auto-options
to generate the prop metadata Svelte needs to convert the increments
attribute from a string to a number.
Component Entrypoint
Section titled “Component Entrypoint”The package entrypoint imports the Svelte component and registers the generated custom element constructor:
import ExampleComponent from "./ExampleComponent.svelte";
export default ExampleComponent;
if (!customElements.get("example-component") && "element" in ExampleComponent) { customElements.define( "example-component", ExampleComponent.element as CustomElementConstructor, );}Package Exports
Section titled “Package Exports”The exports field of your component’s package.json tells consumers where the custom element lives:
{ "exports": { ".": { "types": "./dist/client/index.d.ts", "svelte": "./dist/client-svelte/index.js", "default": "./dist/client/index.js" } }}This declaration also doubles as build configuration. The svebcomponents
CLI maps dist/client/index.js back to src/index.ts and builds that
entrypoint. It also creates a lighter Svelte-aware build at
dist/client-svelte/index.js, which Svelte tooling can select through the
svelte condition.
From the component package, run:
pnpm buildSee Build for the complete inference rules, multiple entrypoints, and manual configuration.
Using the Component
Section titled “Using the Component”You use the generated custom element just like any other web component:
<script lang="ts"> import "@svebcomponents/example-component";</script>
<example-component increments={5}></example-component>What Next?
Section titled “What Next?”- Read Build to understand entrypoints and package export inference.
- Read Auto-options to understand prop and attribute conversion.
- Read SSR when you want to explore server rendering.