MAIS AMOR POR FAVOR

Create a SvelteKit Component Library

Create a SvelteKit Component Library

Create a SvelteKit Component Library

SHARE:

🌟 Creating your own Custom Component Library for SvelteKit

You can create a SvelteKit component library to level up your workflow. There are possibly a few components you use over and over in new SvelteKit apps you create. You might be copy-pasting the component into each new project. If you want a maintainability boost, how about you create a SvelteKit component library? With that done all you have to do is import components into your new app like you do any external dependency. The bonus is you make changes once, in you component library and they are propagated through to all your apps via the famous npm update command.

If that sounds like a productivity boost you'd sign up for, get your typing fingers out then hit play on the video.

πŸ“Ή Create a SvelteKit Component Library: Video

Create a SvelteKit Component Library

πŸ—³οΈ Poll

What's the component you find yourself creating in every new app?
Voting reveals latest results.

πŸ–₯ The 10 Steps

Follow the video above for full, more detailed instructions.

Create a SvelteKit Component Library in 10 Steps

  1. Decide which components from an existing app you want to add to your component library.
  2. Create a skeleton SvelteKit project.
    pnpm init [email protected] my-sveltekit-component-library && cd $_
    pnpm install
  3. Add the components you decided on to src/lib folder.
  4. Create a lib/index.ts. Import your components then export them as named exports.
    import Map from '$lib/components/Map.svelte';
    export { Map };
  5. Edit package.json, adding this line:
    "svelte": "index.js"
  6. Install the svelte2tsx package:
    pnpm install -D svelte2tsx
  7. Generate the package using svelte-kit package:
    npx svelte-kit package
  8. Test locally, by creating a new skeleton project which uses the created package. Link using the relative path of the created package in the test site's package.json:
    "@rodneylab/sveltekit-components": "link:../../components/package",
  9. Push your package to npm:
    npm whoami
    npm login
    npm publish --access public ./package
  10. Install the npm package on your test site to check it works:
    pnpm install -D @you/yourpackage

πŸ™πŸ½ Feedback

If you have found this video useful, see links below for further related content on this site. I do hope you learned one new thing from the video. Let me know if there are any ways I can improve on it. I hope you will use the code or starter in your own projects. Be sure to share your work on Twitter, giving me a mention so I can see what you did. Finally be sure to let me know ideas for other short videos you would like to see. Read on to find ways to get in touch, further below. If you have found this post useful, even though you can only afford even a tiny contribution, please consider supporting me through Buy me a Coffee.

Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on Twitter and also askRodney on Telegram . Also, see further ways to get in touch with Rodney Lab. I post regularly on SvelteKit as well as Search Engine Optimisation among other topics. Also subscribe to the newsletter to keep up-to-date with our latest projects.