🌟 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 #
Please enable JavaScript to watch the video 📼
🗳 Poll #
🖥 The 10 Steps #
Follow the video above for full, more detailed instructions.
Create a SvelteKit Component Library in 10 Steps #
- Decide which components from an existing app you want to add to your component library.
-
Create a skeleton SvelteKit project.
pnpm install
- src/lib folder.
lib/index.ts
. Import your components then export them as named exports.
import Map from '$lib/components/Map.svelte';export { Map };
package.json
, adding this line:
"svelte": "index.js"
svelte2tsx
package:
pnpm add -D svelte2tsx @sveltejs/package
svelte-kit package
:
pnpm svelte-package
"@rodneylab/sveltekit-components": "link:../../components/package",
pnpm whoamipnpm loginpnpm publish --access public ./package
pnpm install -D @you/yourpackage
🔗 Links #
-
@rodneylab/sveltekit-components
, - SvelteKit Packaging Docs ,
-
svelte2tsx
- Mapbox
- OpenStreetMap
- LeafletJS
🏁 Create a SvelteKit Component Library: Summary #
How can you share code between SvelteKit projects? #
- A SvelteKit component library provides a convenient way to share code between different SvelteKit projects. The easiest way to do this is probably to create a new SvelteKit project, placing the components you want to share in the `src/lib`. You can set up any unit tests you want on the components, using uvu for example. Once you are happy you can run `svelte-kit package` to create you component library. Now you can upload it to npm as a public or private package. You other projects can now access the shared components via your new library. It doesn’t end there though. As you discover a need for new components, add them to the package and push the updated package to npm.
🙏🏽 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.