Introduction
Vite is a build tool developed by Evan You, the author of Vue. It uses native ES Module imports and provide a fast running development environment with no bundling required. Vue3, React and Preact are also supported. In this article, I'll use Vite to build a Preact project environment.
You can find the result template in here.
To do
I will introduce the minimum tools necessary for development. The goal is making it close to the default preact/cli template. The following is a step-by-step explanation of each tool, so that you can introduce them individually.
- Typescript
- ESLint
- Prettier
- Stylelint
- husky and lint-staged
- Path Alias
Building Environments
First, let's expand the vite template.
Once the development server is up, you'll be impressed by how fast it is.
Typescript
Then, let's typescript the project. In a minimal configuration, you only need to do two things.
1.Change all .jsx
files to .tsx
.
2.Change the src of the script tag of index.html
to /src/main.tsx
.
Now you can start up the development server and see that it runs without any problems.
It should work, but I'll add a few more settings to improve the user experience in the editor.
Place the tsconfig.json
in your project root. This will tell the editor to recognize the project as a Typescript project.
VSCode shows an error in the .tsx
file at this point, so fix it. Add this sentence to all the .tsx
files.
If you are using Fragment
, import it as well.
Next, fix the entry point, main.tsx
.
Now that it's in Typescript, a type error has been detected.
The document.getElementById
returns HTMLElement
or null
, give it a null check.
Then make some changes to vite.config.js
.
I was able to make Typescript with minimal configuration. You don't have to do the following.
Change vite.config.js
to .ts
to eliminate .js
files.
Also, change it to the ES Module format to make the whole project more consistent.
The vite.config.ts
should look like this
That's the end of Typescript.
Introducing ESLint
Development without a linter is tough, so be sure to install it.
It is easy to prepare a linting command in the script
of the package.json
for later.
It will be easier later on if you have a command for linting in the package.json
script of the package.json
.
Personally, I don't want to fix some situations, so I use --fix
from outside.
Now let's run this.
VSCode users can also set up the following settings to make the automatic formatting work. An extension to ESLint is required, so if you don't have it, please install it here.
This allowed me to format the file on save.
Configuring husky and lint-staged
Before committing, let's run a static check to make sure you can't commit the error code.
Add the following to package.json
.
This causes ESLint to run against any files with the appropriate extensions in the commit file before you commit.
Of course, on a linting error, the commit is canceled.
Configuring Prettier
Let Prettier do the formatting for your entire project. Also, let Prettier automatically remove semicolons in Typescript code, as they are less visible.
When ESLint and Prettier are used together, I need to fix the .eslintrc
to avoid duplicate rules.
command to execute the formatter.
We want to apply automatic formatting before committing, so we add the setting to lint-staged
.
VSCode users can format it automatically with the following settings. Also, an extension is required, so if it is not available, please install it here.
Configuring Stylelint
Let's make the style file a target for linting as well.
Edit the package.jsoon
and set the commands and lint-staged.
VSCode users can format it automatically with the following settings. Extensions are required, so if you don't have them, install them here.
That's the end of the basic setup of the linker and formatter.
Configuring Path Alias
Module import is relative by default, but we want to set alias to always refer to the same root.
Change the vite.config.ts
and tsconfig.json
to set the alias.
Now you can set up alias. We'll use it like this.
It's a little strange that it has to start from /
, but it seems to combine with the alias of the package name.
For more information, please refer to here.
That's the minimum environment you can build.
Edit this page on GitHub