add-on
Community add-ons are currently experimental. The API may change. Don't use them in production yet!
This guide covers how to create, test, and publish community add-ons for sv.
Quick start
The easiest way to create an add-on is using the addon template:
npx sv create --template addon my-addon
cd my-addonAdd-on structure
Typically, an add-on looks like this:
hover keywords in the code to have some more context
import { import transformstransforms, import sveltesvelte } from '@sveltejs/sv-utils';
import { import defineAddondefineAddon, import defineAddonOptionsdefineAddonOptions } from 'sv';
// Define options that will be prompted to the user (or passed as arguments)
const const options: anyoptions = import defineAddonOptionsdefineAddonOptions()
.add('who', {
question: stringquestion: 'To whom should the addon say hello?',
type: stringtype: 'string' // boolean | number | select | multiselect
})
.build();
// your add-on definition, the entry point
export default import defineAddondefineAddon({
id: stringid: 'your-addon-name',
// shortDescription: 'does X', // optional: one-liner shown in prompts
// homepage: 'https://...', // optional: link to docs/repo
options: anyoptions,
// preparing step, check requirements and dependencies
setup: ({ dependsOn }: {
dependsOn: any;
}) => void
setup: ({ dependsOn: anydependsOn }) => {
dependsOn: anydependsOn('tailwindcss');
},
// actual execution of the addon
run: ({ isKit, cancel, sv, options, directory }: {
isKit: any;
cancel: any;
sv: any;
options: any;
directory: any;
}) => any
run: ({ isKit: anyisKit, cancel: anycancel, sv: anysv, options: anyoptions, directory: anydirectory }) => {
if (!isKit: anyisKit) return cancel: anycancel('SvelteKit is required');
// Add "Hello [who]!" to the root page
sv: anysv.file(
directory: anydirectory.routes + '/+page.svelte',
import transformstransforms.svelte((ast: anyast) => {
import sveltesvelte.addFragment(ast: anyast, `<p>Hello ${options: anyoptions.who}!</p>`);
})
);
}
});
svowns the file system —sv.file()resolves the path, reads the file, applies the transform, and writes the result.@sveltejs/sv-utilsowns the content —transforms.svelte()handles parsing, gives you the AST, and serializes back. See sv-utils for the full API.
Development with file: protocol
While developing your add-on, you can test it locally using the file: protocol:
# In your test project
npx sv add file:../path/to/my-addonThis allows you to iterate quickly without publishing to npm.
Testing with sv/testing
The sv/testing module provides utilities for testing your add-on:
import { import setupTestsetupTest } from 'sv/testing';
import { const test: TestAPIDefines a test case with a given name and test function. The test function can optionally be configured with test options.
test, const expect: ExpectStaticexpect } from 'vitest';
import import addonaddon from './index.js';
test<object>(name: string | Function, fn?: TestFunction<object> | undefined, options?: number): void (+1 overload)Defines a test case with a given name and test function. The test function can optionally be configured with test options.
test('adds hello message', async () => {
const { const content: anycontent } = await import setupTestsetupTest({
addon: anyaddon,
options: {
who: string;
}
options: { who: stringwho: 'World' },
files: {
'src/routes/+page.svelte': string;
}
files: {
'src/routes/+page.svelte': '<h1>Welcome</h1>'
}
});
expect<any>(actual: any, message?: string): Assertion<any> (+1 overload)expect(const content: anycontent('src/routes/+page.svelte')).JestAssertion<any>.toContain: <string>(item: string) => voidUsed when you want to check that an item is in a list.
For testing the items in the list, this uses ===, a strict equality check.
toContain('Hello World!');
});Building and publishing
Bundling
Community add-ons are bundled with tsdown into a single file. Everything is bundled except sv (peer dependency, provided at runtime).
npm run buildPackage structure
Your add-on must have sv as a peer dependency and no dependencies in package.json:
{
"name": "@your-org/sv",
"version": "1.0.0",
"type": "module",
"exports": {
".": "./src/index.js"
},
"publishConfig": {
"access": "public",
"exports": {
".": { "default": "./dist/index.js" }
}
},
"peerDependencies": {
"sv": "^0.13.0"
},
"keywords": ["sv-add"]
}exportspoints to./src/index.jsfor local development with thefile:protocol.publishConfig.exportsoverrides exports when publishing, pointing to the bundled./dist/index.js.
Add the
sv-addkeyword so users can discover your add-on on npm.
Export options
Your package can export the add-on in two ways:
Default export (recommended for dedicated add-on packages):
{ "exports": { ".": "./src/index.js" } }/svexport (for packages that have other functionality):{ "exports": { ".": "./src/main.js", "./sv": "./src/addon.js" } }
Publishing
Community add-ons must be scoped packages (e.g. @your-org/sv). Users install with npx sv add @your-org.
npm login
npm publish
prepublishOnlyautomatically runs the build before publishing.
Next steps
You can optionally display guidance after your add-on runs:
export default defineAddon({
// ...
nextSteps: ({ options }: {
options: any;
}) => string[]
nextSteps: ({ options }) => [
`Run ${color.command('npm run dev')} to start developing`,
`Check out the docs at https://...`
]
});Version compatibility
Your add-on should specify the minimum sv version it requires in peerDependencies. If a user's sv version has a different major version than what your add-on was built for, they will see a compatibility warning.