Logo

dev-resources.site

for different kinds of informations.

Building Vue3 Component Library from Scratch #11 ESlint + Prettier + Stylelint

Published at
5/29/2024
Categories
vue
eslint
prettier
stylelint
Author
markliu2013
Categories
4 categories in total
vue
open
eslint
open
prettier
open
stylelint
open
Author
11 person written this
markliu2013
open
Building Vue3 Component Library from Scratch #11 ESlint + Prettier + Stylelint

If you want to do something well, you must first sharpen your tools. A good project must have a unified standard, such as code standards, style standards, and code submission standards, etc. The purpose of a unified code standard is to enhance team development cooperation, improve code quality, and establish a foundation for development, so everyone must strictly adhere to it.

This article will introduce ESLint + Prettier + Stylelint to standardize the code.

ESlint

ESLint statically analyzes your code to quickly find problems. Its goal is to ensure code consistency and avoid errors. Let's see how to use it in our project.

Firstly, install ESLint.

pnpm add eslint -D -w
Enter fullscreen mode Exit fullscreen mode

Initialize ESLint

pnpm create @eslint/config
Enter fullscreen mode Exit fullscreen mode

At this point, some options will appear for us to choose.

Now we will find that the ESlint configuration file .eslintrc.cjs has appeared in the root directory. After some configuration changes to this file, it is as follows.

module.exports = {
  env: {
    browser: true,
    es2021: true,
    node: true
  },
  extends: [
    'eslint:recommended',
    'plugin:vue/vue3-essential',
    'plugin:@typescript-eslint/recommended'
  ],
  globals: {
    defineOptions: true
  },
  parser: 'vue-eslint-parser',
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'module',
    parser: '@typescript-eslint/parser'
  },
  plugins: ['vue', '@typescript-eslint'],
  rules: {
    '@typescript-eslint/ban-ts-comment': 'off',
    'vue/multi-word-component-names': 'off'
  }
};
Enter fullscreen mode Exit fullscreen mode

Create .eslintignore to ignore some files.

**.d.ts
/packages/stellarnovaui
dist
node_modules
Enter fullscreen mode Exit fullscreen mode

Then add the command 'lint:script' in the script section of package.json.

  "scripts": {
    "lint:script": "eslint --ext .js,.jsx,.vue,.ts,.tsx --fix --quiet ./"
  },
Enter fullscreen mode Exit fullscreen mode

Then, run 'pnpm run lint:script', you can see report of the code.

Integrate Prettier

Actually, using ESLint alone is not enough. ESLint is often combined with Prettier to fully utilize their capabilities. Prettier is mainly used for code formatting. Next, let's see how to combine the two.

Similarly, install Prettier.

pnpm add prettier -D -w
Enter fullscreen mode Exit fullscreen mode

Create .prettierrc.cjs

module.exports = {
    printWidth: 80,
    tabWidth: 2,
    useTabs: false, //Whether to use tabs for indentation. The default is false, which means spaces are used for indentation.
    singleQuote: true, // Whether to use single quotes for strings. The default is false, which means double quotes are used.
    semi: true, // Whether to use semicolons at the end of lines. The default is true.
    trailingComma: "none", // Whether to use trailing commas
    bracketSpacing: true // Whether to have spaces between the braces of objects. The default is true, resulting in: { a: 1 }.
}
Enter fullscreen mode Exit fullscreen mode

Install eslint-config-prettier (to override the rules of eslint itself) and eslint-plugin-prettier (to allow Prettier to take over the ability of eslint --fix, which is to fix the code).

pnpm add eslint-config-prettier eslint-plugin-prettier -D -w
Enter fullscreen mode Exit fullscreen mode

Change .eslintrc.cjs

module.exports = {
  env: {
    browser: true,
    es2021: true,
    node: true
  },
  extends: [
    'eslint:recommended',
    'plugin:vue/vue3-essential',
    'plugin:@typescript-eslint/recommended',
    'prettier',
    'plugin:prettier/recommended'
  ],
  globals: {
    defineOptions: true
  },
  parser: 'vue-eslint-parser',
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'module',
    parser: '@typescript-eslint/parser'
  },
  plugins: ['vue', '@typescript-eslint'],
  rules: {
    'prettier/prettier': 'error',
    '@typescript-eslint/ban-ts-comment': 'off',
    'vue/multi-word-component-names': 'off'
  }
};
Enter fullscreen mode Exit fullscreen mode

Finally, by executing 'pnpm run lint:script', you can complete the ESLint rule validation check and automatic fix by Prettier.

By now, the configuration of ESLint+Prettier is complete. Next, we will introduce Stylelint (a style specification tool) to the project.

Stylelint

Firstly, install tool chain.

pnpm add stylelint stylelint-prettier stylelint-config-standard stylelint-config-recommended-less postcss-html stylelint-config-recommended-vue stylelint-config-recess-order stylelint-config-prettier -D -w
Enter fullscreen mode Exit fullscreen mode

Create .stylelintrc.cjs

module.exports = {
  // Register the prettier plugin for stylelint.
  plugins: ['stylelint-prettier'],
  // Inherit a set of rule collections.
  extends: [
    // standard rule set
    'stylelint-config-standard',
    'stylelint-config-recommended-less',
    // Style property order rules
    'stylelint-config-recess-order',
    // Incorporate Prettier rules
    'stylelint-config-prettier',
    'stylelint-prettier/recommended'
  ],
  // Configure rules
  rules: {
    // Enable Prettier auto-formatting
    'prettier/prettier': true
  }
};
Enter fullscreen mode Exit fullscreen mode

Add script command in the package.json file.

{
  "scripts": {
    // stylelint command
    "lint:style": "stylelint --fix \"*.{css,less}\""
  }
}
Enter fullscreen mode Exit fullscreen mode

By running 'pnpm run lint:style', you can complete the style formatting.

Now we have completed the configuration of Stylelint.

The final source code: https://github.com/markliu2013/StellarNovaUI

prettier Article's
30 articles in total
Favicon
VSCode Prettier format on save
Favicon
Beyond Spellcheck: How Static Analysis Tools Enhance Collaboration in Coding
Favicon
Setup Eslint Prettier in a TypeScript project with mongoose ODM
Favicon
Vue3 + ESLint 9.13.0 + Prettier +TypeScript and VSCode Autoformat on Save
Favicon
Configurando Prettier, ESLint y Husky en Angular
Favicon
Setup Prettier Pada Nextjs Tailwind Project
Favicon
Regras customizáveis para Prettier + Eslint em React
Favicon
Useful VS Code Extensions for JS
Favicon
ESLint x Prettier: The Right Way To Start A JavaScript Project
Favicon
How to Set Up ESLint and Prettier in a TypeScript Project
Favicon
Customizable rules for Prettier + Eslint in React
Favicon
Tired of Messy Code?🥴
Favicon
Streamline Your Tailwind CSS Workflow with Prettier Plugin Enhancements
Favicon
Why Do I Love Code Formatters?
Favicon
How to set up Eslint and prettier
Favicon
Configure Eslint, Prettier and show eslint warning into running console vite react typescript project
Favicon
Building Vue3 Component Library from Scratch #11 ESlint + Prettier + Stylelint
Favicon
Setup Eslint + Prettier for code standardization in React
Favicon
Setup Eslint + Prettier para padronização de código em React
Favicon
How to prevent Prettier putting a full stop on a new line after a link
Favicon
Moving "server-only" to the top of the imports
Favicon
My opinion about opinionated Prettier: 👎
Favicon
Setting Up Express development environment (Typescript, Eslint, Prettier)
Favicon
Biome.js : Prettier+ESLint killer ?
Favicon
Most elemental code formatting for Typescript/Javascript: .editorconfig vs prettier
Favicon
Setup Next.js 14 project with Eslint + Prettier + Tailwind CSS
Favicon
Prettier for PHP File Formats in VSCode
Favicon
Angular 14 + Prettier + Husky Setup
Favicon
แชร์ไฟล์ Prettier ง่ายๆ แค่ปลายนิ้ว!
Favicon
NextJS 14'te Kod Kalitesini Artırma: ESLint, Prettier ve Husky Kullanımı

Featured ones: