Introduction
Vite is a build tool developed by Evan You, the author of Vue. It uses native ES Module imports to provide a fast running development environment with no bundling required. Vue3, React and Preact are also supported.
In this article, I'll build a Vue3 project environment using Vite.
You can find the template in here.
Things to do
The goal is to get you close to the default vue/cli template, and I'll implement the necessary tools for development. I'm going to walk you through each of these tools so that you can introduce them individually.
Typescript- ESLint
- Prettier
- Stylelint
- husky and lint-staged
- Path Alias
- vue-tsc
Building Environments
Verified in the next version:
First, let's expand the vite template.
Once the development server is up, you'll be impressed by how fast it is.
⤵️ No longer needed.
Typescript
Next, let's make your project Typescripted.
Since Vue3 has Typescript by default, you only need to do the following three things.
1.Add lang="ts"
to the script
tag in all .vue
files.
2.Change main.js
to main.ts
.
3.Change the src of the script tag of index.html
to /src/main.ts
.
Now you can start up the development server and see that it runs without any problem.
It will actually work on its own, but you can add more settings to improve the user experience in the editor.
If you're using VSCode, you should see a main.ts
with a ts(2307)
error.
To fix this, need to create a type declaration file for vue.
Place the tsconfig.json
in your project root. This will tell the editor to recognize the project as a Typescript project.
That's the end of Typescript.
Introducing ESLint
Development without a linter is tough, so be sure to install it.
This will result in an error, so we will modify the type definition.
It is easy to prepare a linting command in the script
of the package.json
for later.
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 see 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.
When ESLint and Prettier are used together, I need to fix the .eslintrc
to avoid duplicate rules.
command to execute the formatter.
I want to apply automatic formatting before committing, so 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 a target for linting as well.
Edit the package.json
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 see here.
That's the end of the basic setup of the linker and formatter.
Configuring Path Alias
The import of the module is relative by default, but you want to set an alias to always refer to the same root.
Also change tsconfig.json
.
Now you can set up alias. I'll use it like this.
Checking template statically with vue-tsc
You can statically check your template tags with vue-tsc
.
It is installed when the template is generated.
However, as of @vue/runtime-dom@3.1.4
, it does not work unless skipLibCheck
in tsconfig.json
is set to true
.
I recommend running static checks in CI rather than before committing, as static checks can take quite a bit of time as the number of Vue files increases.
That's the minimum environment you'll need to build.
Edit this page on GitHub