Understanding Svelte: Setting Up Your Project and Mastering .svelte Files
Musab Hussain
Software Developer & Leanpreneur : JavaScript | Svelte & SvelteKit | Shopify | Carrd | Notion | Writer | Lifelong Learner | Embracing Tech, Creativity, and Personal Growth
Introduction
I’m very much in the midst of my Svelte knowledge refresh, and to share my learnings with you effectively it would be most prudent to start by walking through the setup of a Svelte project and understanding the role of a .svelte file - where the magic happens!
A word about SvelteKit…
SvelteKit is the easiest way to start developing with Svelte. SvelteKit is a framework built on top of Svelte that simplifies the development of fast and interactive web applications. It provides tools and features for managing pages, layouts, and routing, ensuring efficient performance. SvelteKit offers a comprehensive set of development tools, including server-side rendering
Setup
Setting up a SvelteKit app is straightforward, and you can get started with just a few commands. Here’s a simple guide to help you set up a new SvelteKit project and an overview of what the repository will contain (note: I highly recommend you check SvelteKit documentation for the latest setup instructions).
Setup Instructions
npm init svelte@next my-sveltekit-app
Replace my-sveltekit-app with whatever you want to name your project.
cd my-sveltekit-app
npm install
npm run dev
This will run your app locally on a server, typically accessible at https://localhost:3000 in your web browser.
What's Inside the App Repository
When you set up a SvelteKit project, your repository will typically include the following:
This structure provides a solid base for developing complex applications with clear routing and organisation, making it easy to scale up and manage as your project grows.
The .svelte file
A .svelte file is a component file used in Svelte applications, and it's where much of the "magic" of Svelte takes place. These files are the building blocks of a Svelte app, allowing you to define structure, style, and behaviour in a single cohesive place. Here’s a breakdown of what goes into a .svelte file:
Here’s a simple example of what a .svelte file might look like:
<script>
let count = 0;
function increment() {
count += 1; // Automatically updates the DOM wherever `count` is used
}
</script>
<h1>Count: {count}</h1>
<button on:click={increment}>Increment</button>
<style>
h1 {
color: purple;
}
</style>
In this example:
Closing Remarks
Svelte opens up a world of possibilities for “easily” creating interactive and efficient web applications. With its intuitive syntax
As I continue to delve into the capabilities and features of Svelte in future posts, I encourage you to experiment with the concepts and examples shared today. Dive into the documentation, tinker with the code, and see firsthand how enjoyable Svelte is to work with. Whether you're building a small personal project or a large-scale application, the simplicity and power of Svelte are sure to make the development process a rewarding experience.
Happy coding, and see you in the next post!