When working on our Vue.js project, there are many configurations we can make to ease our development experience.
We have seen that many tools and configuration files are involved in a project that we can tweak to adjust the project to our tastes and needs.
For example, we will have to “deal” with tools like VSCode, Vite, ESLint, and Prettier, or configuration files like jsconfig.json or tsconfig.json.
Of course, each of them could be discussed at length. It will also depend on the needs of your project, and even your tastes and style.
In this article, we will review some of them to at least have an idea and a starting point for some of them 👇.
Installing the Visual Studio Code Extension
If we use Visual Studio Code (VSCode) for development, the first thing we need to do is install the official VS Code extension for Vue.js.
To install it,
- Open VS Code
- Go to the Extensions panel (Ctrl+Shift+X)
- Search for “Vue”
- Click Install
Alternatively, we could also do it from the command line by running,
code --install-extension vue.volar
The new official Vue extension (sometimes called Volar, as it is based on it) replaces the previous one called Vetur
If you find information out there about Vetur, it is deprecated
Configuring VSCode for Vue.js
Customize your VSCode settings to work better with Vue.js by adding these configurations to your settings.json
:
- Open the VSCode settings (Ctrl+,)
- Click on the “Open Settings (JSON)” icon in the top right corner.
- Add the following configurations:
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.validate": ["javascript", "javascriptreact", "vue"],
"volar.takeOverMode.enabled": true,
"volar.completion.autoImportComponent": true,
"[vue]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"javascript.updateImportsOnFileMove.enabled": "always",
"editor.tabSize": 2,
"files.associations": {
"*.vue": "vue"
},
"emmet.includeLanguages": {
"vue": "html"
},
"emmet.syntaxProfiles": {
"vue-html": "html",
"vue": "html"
}
}
These configurations:
- Enable automatic formatting on save
- Set Prettier as the default formatter
- Enable ESLint auto-fixing on save
- Activate Volar’s “Take Over Mode” for better TypeScript support
- Configure autocomplete for Vue components
- Set indentation to 2 spaces
- Configure file associations for Vue
- Enable Emmet for Vue files
Configuring jsconfig.json/tsconfig.json
The jsconfig.json
and tsconfig.json
files are necessary for the IDE to detect the language correctly in our files.
- Path Resolution:
@/*
allows importing from thesrc
folder using the alias@
, which simplifies imports. - Improved Autocompletion: VSCode will provide better suggestions and type checking.
- Code Navigation: You will be able to easily navigate between your files, see where they are used, etc.
Depending on whether you are using JavaScript or TypeScript, you will need to configure one or the other.
Create a jsconfig.json
file at the root of your project:
{
"compilerOptions": {
"target": "es2022",
"module": "esnext",
"baseUrl": ".",
"moduleResolution": "node",
"paths": {
"@/*": ["src/*"]
},
"jsx": "preserve",
"lib": ["es2022", "dom", "dom.iterable", "scripthost"]
},
"exclude": ["node_modules", "dist"],
"vueCompilerOptions": {
"target": 3.3
}
}
Create a tsconfig.json
file at the root of your project:
{
"compilerOptions": {
"target": "es2022",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"moduleResolution": "node",
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"useDefineForClassFields": true,
"sourceMap": true,
"baseUrl": ".",
"types": ["vite/client"],
"paths": {
"@/*": ["src/*"]
},
"lib": ["es2022", "dom", "dom.iterable", "scripthost"]
},
"include": [
"src/**/*.ts",
"src/**/*.d.ts",
"src/**/*.tsx",
"src/**/*.vue"
],
"exclude": ["node_modules", "dist"],
"vueCompilerOptions": {
"target": 3.3
}
}
Configuring the Vue Project with Vite
Vite is a modern and extremely fast bundler that we have already seen, and we use it to develop our project.
If we need to customize the Vite configuration, we can do so by editing the vite.config.js
file. For example,
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
vue(),
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
},
server: {
port: 3000,
open: true
},
build: {
outDir: 'dist',
minify: 'terser',
sourcemap: true
}
})
This configuration:
- Sets the
@
alias for thesrc
folder - Sets the development server port to 3000
- Automatically opens the browser when the server starts
- Configures build options such as output directory, minification, and sourcemaps
Configuring Prettier for Vue.js
Prettier is a code formatting tool that ensures a consistent style throughout your project. If we haven’t installed it yet, we can do so with,
npm install --save-dev prettier @prettier/plugin-vue
Create a .prettierrc
file at the root of your project:
{
"semi": false,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "es5",
"printWidth": 100,
"bracketSpacing": true,
"arrowParens": "avoid",
"vueIndentScriptAndStyle": true,
"endOfLine": "lf",
"htmlWhitespaceSensitivity": "ignore"
}
This configuration:
- Removes semicolons at the end of lines
- Sets indentation to 2 spaces
- Uses single quotes
- Adds trailing commas according to the ES5 standard
- Limits line width to 100 characters
- Maintains spacing between braces in objects
- Avoids unnecessary parentheses in single-parameter arrow functions
- Correctly indents
<script>
and<style>
sections in Vue components - Normalizes line endings
- Ignores whitespace sensitivity in HTML
If we need to ignore files for Prettier, we can create a .prettierignore
file to exclude certain files from formatting.
Configuring the Linter for Vue.js
ESLint is an essential tool for maintaining code quality and following best practices in Vue.js projects. If you did not select ESLint during project creation, you can install it manually:
npm install --save-dev eslint eslint-plugin-vue @vue/eslint-config-prettier
Create a .eslintrc.js
file at the root of your project:
module.exports = {
root: true,
env: {
node: true,
'vue/setup-compiler-macros': true
},
extends: [
'plugin:vue/vue3-essential', // Fewer rules, good for beginners
'plugin:vue/vue3-recommended', // Stricter, recommended for professional development
'eslint:recommended',
'@vue/eslint-config-prettier'
],
parserOptions: {
ecmaVersion: 2022
},
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'vue/multi-word-component-names': 'off', // For beginners
'vue/require-default-prop': 'off', // For beginners
'vue/script-setup-uses-vars': 'error',
'vue/no-unused-vars': 'error'
}
}
If we need to ignore files for ESLint, we can create a .eslintignore
file to exclude files from linting.