Introduction to Svelte Project Structure
In the last video, we saw how simple it is to create and run a basic Svelte application. Now, it's also really important that you understand the files and folders present in the generated project and how the control flows when you run the application. Let's take a look at that in this video.
Exploring package.json
I have opened the Hello World project in VS Code, and you can see that at the root level, we have four folders and five files to begin with.
Let's start with the important bits in package.json. This file contains the dependencies and the scripts required for the project. You can see that we are using Svelte version 3, and that is listed as a dev dependency. Svelte is only used during the compilation phase and never bundled into the code that is sent to the browser.
We then have Rollup, which is a module bundler, and a whole load of Rollup plugins listed as dev dependencies. Rollup is responsible for transforming the Svelte code we write into JavaScript code that the browsers can understand. There is one dependency, which is the sirv-cli. It allows us to run a static file server. We also have three scripts: dev for development, build to create a production-ready version of the application, and start for the sirv-cli to serve the built application. A straightforward package.json file, as you can see.
Configuration and Lock Files
Next, we have the configuration file for Rollup: rollup.config.js. This is the config file that is used when we run the command yarn dev or yarn build. If you're interested in the various configurations, I recommend you take a look at Rollup documentation.
Next, we have the yarn.lock file. Based on whether you prefer npm or yarn as a package manager, you're going to see yarn.lock or package-lock.json files. They simply ensure consistent installation of your dependencies, and you don't really have to worry about them. We also have a .gitignore and a README file.
The node_modules and public Folders
Alright, next, let's talk about the folders. The first one is node_modules. This is the folder in which all the dependencies are installed. It is generated when you run the yarn command or npm install command.
The next folder is the public folder. This folder contains static assets that are published when you want to go live with your application. It contains three files. We have a favicon which you see in the browser tab and is nothing Svelte-specific. We also have a global.css file which includes styles that are applied to our entire application. And finally, we have an index.html file, which is the only HTML file you're going to have in your application. We are building single page applications, and this is it. The view might dynamically change in the browser, but it is this HTML file that gets served up. Typically, you are not going to add any code in this file, maybe some changes in the head tag, but definitely not in the body tag. You want Svelte to control the UI.
And for that purpose, we have a CSS and JS file: /build/bundle.css, /build/bundle.js. These files are from the build folder, which get generated when you run or build your application. Now, please make a note of this empty body tag, as we will come back to it shortly.
The scripts and src Folders
The next folder is the scripts folder, which contains a file pertaining to TypeScript setup. Since we are going to learn Svelte with just JavaScript, we don't have to worry about this for now.
The final folder is the src folder, which is the folder we will be working with the most during development. The starting point for our Svelte application is main.js. In main.js, we import the root component, which is App component, and invoke it as a function, specifying the document body as the target element. And if you can recollect, we have an empty body element in our index.html file. So everything inside this body tag will be managed by Svelte. For the Hello World application, the App component is rendered inside the body element.
That brings us to the App component, which is present in App.svelte. Now, the .svelte file extension, I bet, is something new to you if you are learning about Svelte for the first time. We will talk about the .svelte files in the next video, but for now, let me tell you that it is a file where you specify the HTML, CSS, and JavaScript corresponding to a portion of the UI you see in the browser. You can see here we have a script block with a variable called name. For the HTML, we have the main HTML tag with an h1 tag and a paragraph tag. This is the text we see in the browser. Below the HTML, we also have some styles specified within a style block. The App.svelte file pretty much represents the UI you see in the browser.
Application Control Flow Explained
So that is the folder structure of a Svelte application created using the degit command.
Next, let's understand the control flow when you run this application in the terminal. When you run the command yarn dev, the index.html file is served in the browser. index.html contains a reference to bundle.js. The bundle.js file is your main.js file compiled to a JavaScript format that the browser can understand. The Svelte code renders itself within the body tag. So if I inspect the heading element, you can see the main tag as a child of the body tag. This is nothing but our App component. The name, of course, is the name from main.js, which gets replaced in the HTML because of the Svelte compiler. So Hello {name}... replace name with world, which is Hello world. And that is what you see in the browser.
We will, of course, learn more about this Svelte magic in the videos to come, but this is pretty much the control flow from package.json dev script to index.html, bundle.js script, to main.js, and finally App.svelte.
Now, I'm pretty sure this file extension, which is .svelte, is new to you. So let's discuss more about that in the next video. Thank you guys for watching. Make sure to subscribe to the channel. I'll see you guys in the next one.