SvelteKit Project Structure Explained
Published Dec 22, 2022
Table of Contents
Previously
This is part of a SvelteKit series and while each part is meant to be self-contained here are the previous parts in case you want to catch up:
Introduction
I donβt know about you but when Iβm learning something I want at least a high level understanding of what makes it work.
Understanding what makes SvelteKit work is going to give you more confidence using it as you get more familiar with it like getting to know a friend.
In this post Iβm going to show you how to set up SvelteKit yourself without the CLI β donβt worry Iβm going to use off the shelf parts instead of doing SvelteKit from scratch π .
After that Iβm going to walk you through the SvelteKit CLI and explain every file, so you at least understand the purpose of it.
If youβre one of those weird people that want to understand how SvelteKit works under the hood Iβm also the weird type of person that wrote Learn How SvelteKit Works you can read or watch.
SvelteKit From Scratch
Iβm going to initialize an empty package.json
file using the y
flag to skip questions.
npm init -y
SvelteKit needs these development dependencies at least to work.
npm i -D vite @sveltejs/kit @sveltejs/adapter-auto svelte
If you donβt know, SvelteKit is built on top of Vite!
At the top of the package.json
Iβm going to specify the project uses JavaScript modules (ECMAScript modules) using the import
syntax instead of the old CommonJS modules using the require
syntax.
{
type: "module"
}
To run the development mode or build the project and preview it youβre going to need to include some scripts that just runs some scripts from node_modules
on your system.
{
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview"
}
}
This is the finished package.json
file.
{
"type": "module",
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
},
"devDependencies": {
"@sveltejs/adapter-auto": "^1.0.0",
"@sveltejs/kit": "^1.0.0",
"svelte": "^3.54.0",
"vite": "^4.0.0"
}
}
SvelteKit requires a Vite config at the root of the project and in the previous What Is SvelteKit? I mentioned how SvelteKit is a Vite plugin and here it is β the beating heart of SvelteKit.
import { sveltekit } from '@sveltejs/kit/vite'
/** @type {import('vite').UserConfig} */
const config = {
plugins: [sveltekit()]
}
export default config
You can include a Svelte config if you want to use a preprocessor and adapter.
import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/kit/vite';
/** @type {import('@sveltejs/kit').Config} */
const config = {
preprocess: vitePreprocess(),
kit: {
adapter: adapter()
}
};
export default config
A preprocessor transforms your .svelte
files before passing them to the compiler. In this case vitePreprocess
handles TypeScript, PostCSS and SCSS as some of the language flavors which you can read more about in the SvelteKit documentation.
An adapter is used to adapt π₯ your SvelteKit app to the deployment target. You could write your own adapter but the supported adapters include Cloudflare Pages, Netlify, Vercel including Node.js and a static adapter or community adapters for platforms like Deno.
Next Iβm going to add the HTML page template with placeholders SvelteKit uses and replaces for your pages.
<head>
%sveltekit.head%
</head>
<body>
%sveltekit.body%
</body>
The markup might be questionable but the browser is going to construct a proper HTML page despite which always fascinates me.
As the last step Iβm going to add a route thatβs going to be the first page someone sees when they visit the site.
<h1>Hello</h1>
If you run npm run dev
you have a working SvelteKit site! π₯³
SvelteKit creates a special .svelte-kit
folder which you can ignore or delete thatβs going to generate files as you develop and regenerates each time you run dev
or build
β thatβs how the magic sauce works for generating types for your pages which you can find in .svelte-kit/types
.
Using The SvelteKit CLI
Iβm going to create an empty SvelteKit project.
npm create svelte
Iβm going with TypeScript and selecting ESLint, Prettier, Playwright and Vitest from the options.
β Welcome to SvelteKit!
β
β Where should we create your project?
β sveltekit
β
β Which Svelte app template?
β Skeleton project
β
β Add type checking with TypeScript?
β Yes, using TypeScript syntax
β
β Select additional options (use arrow keys/space bar)
β βΌ Add ESLint for code linting
β βΌ Add Prettier for code formatting
β βΌ Add Playwright for browser testing
β βΌ Add Vitest for unit testing
β
You can use JSDoc with regular JavaScript or TypeScript for types but I leave that up to you.
ESLint is like a spell checker for your code that gives you useful warnings in your editor from checking your code for problems like accessibility. Youβre going to discover that ESLint and TypeScript make a great combination ensuring you donβt do something goofy.
Prettier is an opinionated code formatter. You might find it does some things you donβt like but itβs a great trade-off considering you donβt have to think about formatting your code and itβs going to be consistent for everyone else working on the project (I recommend you enable format on save which you can look up how to do for your editor).
Playwright is used for end-to-end testing. You can test how your user might use the site using a real browser and check for example if some content is showing or test your registration or checkout process.
Vitest is used for unit testing. That means youβre testing one unit of your code. For example you can use Playwright to test your site using a real browser and see if your content works but you would use Vite to test the input and output of the function responsible for sorting the content in some order.
After everything is done youβre going to inherit this beautiful tree (you might have to run npm i
and npm run dev
to generate some files).
.
βββ .svelte-kit
βββ node_modules
βββ src
β βββ lib
β βββ routes
β βββ app.d.ts
β βββ app.html
β βββ index.test.ts
βββ static
βββ tests
β βββ test.ts
βββ .eslint.cjs
βββ .npmrc
βββ .prettierrc
βββ package.json
βββ playwright.config.ts
βββ package-lock.json
βββ README.md
βββ svelte.config.js
βββ tsconfig.json
βββ vite.config.ts
Here is the breakdown of the files:
.svelte-kit
folder is generated by SvelteKit and itβs safe to removenode_modules
is where your npm packages and binaries gosrc
folder is the heart of your project:lib
can hold your shared components, utils or library code which can be imported using the$lib
aliasroutes
contain the routes for your appapp.d.ts
is used to add type information for some special SvelteKit objectsapp.html
is a template used by SvelteKit for your pagesindex.test.ts
is an example of a unit test using Vitest
static
is used for static assets (robots.txt
,favicon.png
)tests
test.ts
is an example end-to-end test using Playwright
.eslint.cjs
is the ESLint config.npmrc
is the npm config.prettierrc
is the Prettier configpackage.json
holds your dependencies and uses"type": "module"
for native JavaScript modules withimport
andexport
keywords and legacy CommonJS files need a.cjs
extensionplaywright.config.ts
are the options for Playwright used for testingpackage-lock.json
keeps track of your npm dependencies versiontsconfig.json
are the compiler options for TypeScriptsvelte.config.js
contains your Svelte and SvelteKit configurationvite.config.ts
contains your Vite config
There are some files I ignored π₯ like .gitignore
, .eslintignore
, .prettierignore
as theyβre not meaningful and do what the name implies specifying files to ignore in their config.
If youβre newer to development you might be hearing for the first time about some of these things but you can ignore most of them.
I hope this gave you a better understanding of these configuration files in general because youβre going to see them in every JavaScript project but now youβre a certified SvelteKit connoisseur.
In the next part youβre going to learn everything about SvelteKit routing including how to create pages, layouts and nested routes.