SvelteKit Tooling: 7 Tools to Streamline you CI Workflow
Tags: svelte, webdev, javascript, ci
Published: 2021-11-22 13:37:38

---
title: "SvelteKit Tooling:  7 Tools to Streamline you CI Workflow"
published: "true"
description: "SvelteKit tooling: we look at 7 tools you can add to streamline your Svelte continuous integration (CI) and development processes."
tags: "svelte, webdev, javascript, ci"
canonical_url: "https://rodneylab.com/sveltekit-tooling/"
cover_image: "https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4mt6lxfuzst4gjj79r9.png"
---

## 🚀 SvelteKit Tooling: Putting your Continuous Integration Process on Skates

Adding SvelteKit tooling to your continuous integration (CI) process can help you work more efficiently as well as keep your code base more consistent. This is useful when you need to hand your project over to a client, or even get assistance from colleagues or external sources. We look at some tools you might consider adding to your CI process in this article.  We cover checking your code follows best practices, that it has consistent styling as well as how you can create consistent commit messages. I hope you are able to apply at least a couple of the aspects covered to your regular SvelteKit development process.

## 🖥 VS Code Extensions

Since <a aria-label="Open Stack Overflow developer survey findings" href="https://insights.stackoverflow.com/survey/2021/#most-popular-technologies-new-collab-tools-prof">70% of professional developers use Visual Studio Code</a>, let's take a quick look at some extensions you might want to add to your SvelteKit workflow before we get on to the main tooling items. Although the tools we look at later are mostly standalone tools, you will get additional benefits when working with VSCode if you add the corresponding extension. We will look at the dot files as we go along.

- <a aria-label="Open the V S Code E S Lint extension in the V S Code marketplace" href="https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint"><strong>ESLint</strong></a> &mdash; working in conjunction with the main ESList package (see below), this will highlight errors in your code, often helping you realise early that you mistyped a variable name or forgot to import or install a package.

- <a aria-label="Open the V S Code Prettier extension in the V S Code marketplace" href="https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode"><strong>Prettier</strong></a> &mdash; prettier is probably the best known code formatter. Rather than argue over
  whether or not to use it, I think the argument has shifted to whether it should be used to enforce
  tab or space indenting &mdash; I'm not getting into that one!

- <a aria-label="Open the V S Code Style lint extension in the V S Code marketplace" href="https://marketplace.visualstudio.com/items?itemName=stylelint.vscode-stylelint"><strong>stylelint</strong></a> &mdash; this will flag up accessibility as well as CSS code style issues in VSCode.  I run stylelint before committing code, but it's nice to have errors highlighted in the editor so you can fix them individually as they crop up. That is rather than having to tackle a stack of them just before you commit at the end of a long session.

- <a aria-label="Open the V S Code Svelte Code extension in the V S Code marketplace" href="https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode"><strong>SvelteCode</strong></a> &mdash; official Svelte VSCode extension adds syntax highlighting for your Svelte code.

### Base VSCode Configuration

Everyone has their own favourite VSCode settings. Personally I prefer a light-touch approach, so hopefully this might be used as a starting point for anyone. You can set these globally, though typically I add a config file to each project (at `.vscode/settings` in the project folder) so I can tweak settings based on what the project uses.

```json
{
  "editor.formatOnSave": true,
  "editor.codeActionsOnSave": {
    "source.organizeImports": true
  },	
  "[markdown]": {
    "editor.wordWrap": "bounded",
    "editor.wordWrapColumn": 80,
    "editor.quickSuggestions": false
  },
  "[svelte]": {
    "editor.defaultFormatter": "svelte.svelte-vscode"
  }
}
```

`formatOnSave` is my most loved setting! I have mixed feelings about `organizeImports` and omit it on most projects &mdash; it can get a touch annoying when it removes imports which you still need. You can run organise imports manually using the <kbd>Shift</kbd> + <kbd>Alt</kbd> + <kbd>O</kbd> key combination. The markdown options make your content a little easier to read in the editor (you might prefer 100 or 120 character lines instead of 80). I have had a couple of Svelte projects where formatting stopped working and found adding the last setting fixes this. As I say this is just a base and you will probably have your own favourites. I'm keen to hear what I am missing (remember I prefer a minimalist approach though)!

## 🔥 SvelteKit Tooling: 1. pnpm

`pnpm` is a packet management tool like `npm` or `yarn`. I like to look at it as a more modern imagination of a package manager. The main selling points are speed and efficiency. When you install packages in your project, `yarn` and `npm` will download the package and save it to a `node_modules` folder in your project. These folders can get huge and you have to scan though old projects deleting them whenever your machine starts running low on free disk space. In contrast `pnpm` creates a central repository for packages on your machine and just adds a link from the `node_modules` folder of your project to the particular package in the the central repo.

The two main advantages of the central local repo approach (which `pnpm` follows) are that it is quicker to start up new projects as many of the packages you need to install will already be on your machine. Then, on top, you save on disk space. In the screenshot below, you see in the last long line 142 packages were reused in this particular case. That's 142 packages that did not need to be freshly downloaded. You will also see the output is a little more terse and cleanly formatted than with other tools.

![SvelteKit Tooling:  p n p m install command screenshot.  Result shows an ouput line recording 142 reused packages and only 2 downloaded](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oxd530gqcwo283cuahe3.png)

You need a one-off install to get `pnpm` running on your machine. After that, it accepts commands similar to the ones you will be using to with `npm` or `yarn`. If you already have `npm` on your machine just run this command to install `pnpm` globally:

```shell
npm i -g pnpm
```

To check for updates for `pnpm` and other global packages, run:

```shell
pnpm outdated -g
pnpm add -g outdated-package-one outdated-package-two
```

## 🧑🏽 SvelteKit Tooling: 2. Prettier

You can automatically add prettier to a new skeleton Svelte project form the init tool:

```shell
pnpm init svelte@next sveltekit-tooling && cd $_
```

![SvelteKit Tooling:  Prettier screenshot shows output from pnpm init svelte command with Skeleton project, Yes to E S Lint and yes to Prettier answers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o58y8efarwqspn3a7w1z.png)

The default prettier script installed into `package.json` uses the `.gitignore` file to decide which files to ignore for formatting. I like to commit the `pnpm-lock.yaml` file but am not too bothered about how it is formatted so go update the script and add a `.prettierignore` file to my project root folder:

```plaintext
.svelte-kit/**
static/**
build/**
functions/**
node_modules/**
pnpm-lock.yaml
```

```json
{
  "name": "sveltekit-tooling",
  "version": "0.0.1",
  "scripts": {
    "dev": "svelte-kit dev",
    "build": "svelte-kit build",
    "preview": "svelte-kit preview",
    "format": "prettier --write --plugin-search-dir=. .",
    "prettier:check": "prettier --check --plugin-search-dir=. .",
    "lint": "prettier --check --plugin-search-dir=. . && eslint --ignore-path .gitignore .",
  },
```

Notice I add a `prettier:check` script too! I use this for a final check before committing, even though I set `formatOnSave` in VSCode.

If you prefer tabs to spaces, ignore the rest of this paragraph! By the way I'm not saying spaces are better than tabs, just letting you know how to switch if you don't want to go with the default, just saying! Here's my `.prettierrc` file:

```json
{
  "useTabs": false,
  "arrowParens": "always",
  "singleQuote": true,
  "trailingComma": "all",
  "printWidth": 100
}
```

We use default filenames here for the the ignore and config file so we don't need to specify them explicitly in the scripts.

For completeness, here is a typical `.gitignore` file for one of my SvelteKit projects:

```plaintext
# SvelteKit Files
build/
functions/
/.svelte-kit
 
# Dependency directories
node_modules/
/package
 
# Optional eslint cache
.eslintcache
 
# dotenv environment variable files
.env*
!.env.EXAMPLE
 
# Mac files
.DS_Store
 
# Local Netlify folder
.netlify
```

While we're slightly off topic and since this does not fit neatly anywhere else, it is worth adding a `.nvmrc` file to new SvelteKit projects. This sets the node version when your host builds the project. Not setting it can result it builds failing as some hosts use an older node version by default.

```plaintext
14
```

## 👀 SvelteKit Tooling: 3. ESLint

ESLint is a well-know JavaScript linting tool. There are various rules you can set though the defaults set by the Svelte init tool do work quite well. If you have set up your project using another method you can run the ESLint setup tool to get you going:

```shell
pnpm add -D eslint
./node_modules/.bin/eslint --init
```

Here is the `.eslint.cjs` file that I go with (default for JavaScript skeleton project from init tool):

```javascript
module.exports = {
	root: true,
	extends: ['eslint:recommended', 'prettier'],
	plugins: ['svelte3'],
	overrides: [{ files: ['*.svelte'], processor: 'svelte3/svelte3' }],
	parserOptions: {
		sourceType: 'module',
		ecmaVersion: 2019
	},
	env: {
		browser: true,
		es2017: true,
		node: true
	}
};
```

Here's the `lint` `package.json` script I typically use:

```json
{
  "name": "sveltekit-tooling",
  "version": "0.0.1",
  "scripts": {
    "dev": "svelte-kit dev",
    "build": "svelte-kit build",
    "preview": "svelte-kit preview",
    "format": "prettier --write --plugin-search-dir=. .",
    "prettier:check": "prettier --check --plugin-search-dir=. .",
    "lint": "prettier --check --plugin-search-dir=. . && eslint --ignore-path .gitignore .",
  },
```

## 😇 SvelteKit Tooling: 4. svelte-check

<a aria-label="Open svelte-check docs"  href="https://github.com/sveltejs/language-tools/tree/master/packages/svelte-check">svelte-check</a> is a handy tool for finding TypeScript errors in your code, though I also like to use it on JavaScript projects. You might find it spits out a lot of errors. If you are just starting out, you will not understand all of them or know which ones are safe to ignore. In this case, if your code works, just fix the accessibility errors and tackle the others one by one as you gain more experience.

Anyway to set it up run:

```shell
pnpm add -D svelte-check
```

Then add a script to `package.json` to run it when you need to:

```json
{
  "name": "sveltekit-tooling",
  "version": "0.0.1",
  "scripts": {
    "dev": "svelte-kit dev",
    "build": "svelte-kit build",
    "preview": "svelte-kit preview",
    "format": "prettier --write --plugin-search-dir=. .",
    "prettier:check": "prettier --check --plugin-search-dir=. .",
    "lint": "prettier --ignore-path .gitignore --check --plugin-search-dir=. . && eslint --ignore-path .gitignore .",
    "svelte:check": "svelte-check --fail-on-warnings",
  },
```

You can set a stricter `--fail-on-hints` flag as an alternative.

## 💄 SvelteKit Tooling: 5. stylelint

`stylelint` is great for checking accessibility issues in your CSS code.  You can also use it to prevent colour drift and to keep your code more maintainable.  As an example, setting a rule for no named colours will flag up an error if add something like `border-color: red` for debugging and forget to remove it. More likely though, you might set a hex or HSL value while fixing or setting something up instead of using a named variable like `--theme-colour`.  When you later need to tweak the theme colour, the manually added hex value will persist creating inconsistencies in the site.

`stylelint` is one way to fix this problem.  Another, if you're a TypeScript fan is to be super strict and <Link aria-label="Open post on using vanilla-extract with Svelte Kit" to="/using-vanilla-extract-sveltekit/">use vanilla-extract with contracts</Link>.  For <Link to="/sveltekit-css-stylelint/">stylelint there's a whole video on vanilla CSS linting in SvelteKit</Link>.  Also follow that link for another video which looks at SCSS linting in SvelteKit.

I add this script to `package.json` for vanilla CSS projects:

```json
    "lint:css": "stylelint \"src/**/*.{css,svelte}\"",
```

this is the SCSS alternative:

```json
    "lint:scss": "stylelint \"src/**/*.{css,scss,svelte}\"",
```

## ⛔️ SvelteKit Tooling: 6. precommit

When working in a team, your colleagues will probably appreciate consistent and concise commit messages.  There is a whole <a aria-label="Learn more about conventional commits" href="https://www.conventionalcommits.org/en/v1.0.0/">system for commit message etiquette named conventional commits</a>.  That is just one option and you might prefer one of the other various options.  With conventional commits, your commit messages take a particular format.   In this example our commit type is `refactor` (we could also choose fix, style or feature among others).

```plaintext
refactor(services/narcissus-api): 🏄🏽‍♂️ add Supabase client 
```

Following the type of commit in brackets we have a description for the part of the project affected. Then the commit message itself.  The emoji is not required!  If you want to try out conventional commits, <a aria-label="Open the commit-i-zen Git Hub repo" href="https://github.com/commitizen-tools/commitizen">you might like the commitizen command line tool</a>. As well as holding your hand as you write commit messages, it can handle version bumping and generate changelogs for you.  We won't go into details here, but definitely try it on a new side project to see if it suits you.

I like to include the `commitlint` tool in all my SvelteKit projects to enforce the conventional commit syntax.  If you want to try it, install a couple of packages and then add the config file:

```shell
pnpm add -g commitlint
pnpm add -D @commitlint/config-conventional
```

Next, create a `commitlint.config.cjs` file in your project's root folder:

```javascript
module.exports = { extends: ['@commitlint/config-conventional'] };
```

Because SvelteKit uses ES modules by default, it is important that the file extension is `.cjs` rather than `.js`.

To test it out run a command form the terminal like:

```shell
echo 'nonsense non-valid commit message' | pnpx commitlint
```

This is asking `commitlint` to consider `nonsense non-valid commit message` to be a commit message and to check it for us.

![SvelteKit Tooling:  Commitlint screenshot show output from commitlint lint with non-valid commit message. Response says subject mustnot be empty and type must not be empty](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wu2p65f5o4766vd9r7ve.png)

We will see how to integrate `commitlint` into the continuous integration process next.

## 🏋🏽 SvelteKit Tooling: 7. Husky

Husky pulls together a few of the other tools we have already seen.  Essentially it runs git hooks locally, before committing your code.  If you have ever pushed code to an upstream repo only to realise you forgot to format it or didn't save a file with an error in it before committing Husky will get your back.  So, as an example, you can make sure you pass `svelte-check` and a host of other things before pushing to your remote repo.  Here's my setup but you can go to town and add a whole lot more.

To get going install Husky as a dev dependency:

```shell
pnpm add -D husky
```

Next you can add config files to run at different stages in the continuous integration process:

```shell
pnpx --no-install commitlint --edit "$1"
```

```shell
pnpm run prettier:check && pnpm run lint:css
#pnpm run prettier:check && pnpm run lint:scss # scss alternative
```

```shell
pnpm run svelte:check
```

Finally install your Husky configuration:

```shell
pnpx husky install
```

## 🙌🏽 SvelteKit Tooling: What we Learned

**In this post we looked at**:

- how tooling can be used to streamline the continuous integration process,

- configuration of seven continuous integration tools to work with SvelteKit,

- how Husky can be used ultimately to enforce all of the coding conventions and rules created by other tools.

I do hope there is at least one thing in this article which you can use in your work or a side project. I'm keen to hear what tools you use in your own process and any further recommendations you might have. Drop a comment below with your thoughts.

You can see an <a aria-label="Open the Rodney Lab Git Hub repo" href="https://github.com/rodneylab/sveltekit-tooling">example project with all of this set up on the Rodney Lab Git Hub repo</a>.

## 🙏🏽 SvelteKit Tooling: Feedback

Have you found the post useful? Would you prefer to see posts on another topic instead? Get in touch with ideas for new posts. Also if you like my writing style, get in touch if I can write some posts for your company site on a consultancy basis. Read on to find ways to get in touch, further below. If you want to support posts similar to this one and can spare a few dollars, euros or pounds, please <a aria-label="Support Rodney Lab via Buy me a Coffee" href="https://rodneylab.com/giving/">consider supporting me through Buy me a Coffee</a>.

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 <a href="https://twitter.com/messages/compose?recipient_id=1323579817258831875">@askRodney</a> on Twitter and also <a aria-label="Contact Rodney Lab via Telegram" href="https://t.me/askRodney">askRodney on Telegram</a>. Also, see <a aria-label="Get in touch with Rodney Lab" href="https://rodneylab.com/contact">further ways to get in touch with Rodney Lab</a>. I post regularly on <a aria-label="See posts on svelte kit" href="https://rodneylab.com/tags/sveltekit/">SvelteKit</a> as well as other topics. Also <a aria-label="Subscribe to the Rodney Lab newsletter" href="https://rodneylab.com/about/#newsletter">subscribe to the newsletter to keep up-to-date</a> with our latest projects.