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.
Follow the video above for full, more detailed instructions.
Decide which components from an existing app you want to add to your component library.
Create a skeleton SvelteKit project.
Add the components you decided on to
lib/index.ts. Import your components then export them as named exports.
package.json, adding this line:
Generate the package using
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`:
Push your package to npm:
Install the npm package on your test site to check it works:
- SvelteKit Packaging Docs ,
- 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.
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.