Cấu hình cơ bản cho dự án Vite + Vue 3 + TypeScript

1. Cấu hình啟 động project và tự động mở trình duyệt

Trong file package.json:


"scripts": {
    "dev": "vite --open",
    "build": "vue-tsc && vite build",
    "preview": "vite preview",
    "lint": "eslint src",
    "fix": "eslint src --fix",
    "format": "prettier --write \"./**/*.{html,vue,ts,json,md}\"",
    "lint:eslit": "eslint src/**/*.{ts,vue} --cache --fix",
    "lint:style": "stylelint src/**/*.{css,scss,vue} --cache --fix",
},
        

2. Cấu hình công cụ kiểm tra mã nguồn ESLint

Thực hiện các bước sau:

  • Đầu tiên, cài đặt ESLint:
  • pnpm i eslint -D
  • Tạo file cấu hình eslint.cjs:
  • npx eslint --init
  • Thực hiện các thay đổi sau trong file eslint.cjs:
  • 
    module.exports = {
        env: {
            browser: true,
            es2021: true,
            node: true,
            jest: true,
        },
        parser: 'vue-eslint-parser',
        parserOptions: {
            ecmaVersion: 'latest',
            sourceType: 'module',
            parser: '@typescript-eslint/parser',
            jsxPragma: 'React',
            ecmaFeatures: {
                jsx: true,
            }
        },
        extends: [
            'eslint:recommended',
            'plugin:vue/vue3-essential',
            'plugin:@typescript-eslint/recommended',
            'plugin:prettier/recommended',
        ],
        plugins: ['vue', '@typescript-eslint'],
        rules: {
            'no-var': 'error',
            'no-multiple-empty-lines': ['warn', { max: 1 }],
            'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
            'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'optional',
            'no-useless-escape': 'off',
            '@typescript-eslint/no-unused-vars': 'error',
            '@typescript-eslint/prefer-ts-expect-error': 'error',
            '@typescript-eslint/no-non-null-assertion': 'off',
            '@typescript-eslint/no-namespace': 'off',
            '@typescript-eslint/semi': 'off',
            'vue/multi-word-component-names': 'off',
            'vue/script-setup-uses-vars': 'error',
            'vue/no-mutating-props': 'off',
            'vue/attribute-hyphenation': 'off',
        }
    }
                
  • Tạo file .eslintignore để bỏ qua các thư mục không cần kiểm tra:
  • 
    dist
    node_modules
                
  • Cập nhật scripts trong package.json:
  • 
    "scripts": {
        "lint": "eslint src",
        "fix": "eslint src --fix",
    },
                

3. Cấu hình công cụ định dạng mã nguồn Prettier

  • Cài đặt các phụ thuộc:
  • pnpm install -D eslint-plugin-prettier eslint-config-prettier
  • Tạo file .prettierrc.json:
  • 
    {
        "singleQuote": true,
        "semi": false,
        "bracketSpacing": true,
        "htmlWhitespaceSensitivity": "ignore",
        "endOfLine": "auto",
        "trailingComma": "all",
        "tabWidth": 2
    }
                
  • Tạo file .prettierignore để bỏ qua các file không cần định dạng:
  • 
    /dist/*
    /html/*
    .local
    /node_modules/**
    **/*.svg
    **/*.sh
    /public/*
                

4. Cấu hình công cụ kiểm tra CSS Stylelint

  • Cài đặt các phụ thuộc cần thiết:
  • 
    pnpm add sass sass-loader stylelint postcss postcss-scss postcss-html stylelint-config-prettier stylelint-config-recess-order stylelint-config-recommended-scss stylelint-config-standard stylelint-config-standard-vue stylelint-scss stylelint-order stylelint-config-standard-scss -D
                
  • Tạo file .stylelintrc.cjs:
  • 
    module.exports = {
        extends: [
            'stylelint-config-standard',
            'stylelint-config-html/vue',
            'stylelint-config-standard-scss',
            'stylelint-config-recess-order',
            'stylelint-config-prettier',
        ],
        overrides: [
            {
                files: ['**/*.(scss|css|vue|html)'],
                customSyntax: 'postcss-scss',
            },
            {
                files: ['**/*.(html|vue)'],
                customSyntax: 'postcss-html',
            }
        ],
        ignoreFiles: [
            '**/*.js',
            '**/*.jsx',
            '**/*.tsx',
            '**/*.ts',
            '**/*.json',
            '**/*.md',
            '**/*.yaml',
        ],
        rules: {
            'value-keyword-case': null,
            'no-descending-specificity': null,
            'function-url-quotes': 'always',
            'no-empty-source': null,
            'selector-class-pattern': null,
            'property-no-unknown': null,
            'block-opening-brace-space-before': 'always',
            'value-no-vendor-prefix': null,
            'property-no-vendor-prefix': null,
            'selector-pseudo-class-no-unknow': [
                true,
                {
                    ignorePseudoClasses: ['global', 'v-deep', 'deep'],
                }
            ]
        }
    }
                
  • Tạo file .stylelintignore:
  • 
    /node_modules/*
    /dist/*
    /html/*
    /public/*
                
  • Cập nhật scripts trong package.json:
  • 
    "scripts": {
        "lint:style": "stylelint src/**/*.{css,scss,vue} --cache --fix"
    }
                

5. Cấu hình Husky để kiểm tra trước khi commit

  • Cài đặt Husky:
  • pnpm install -D husky
  • Chạy lệnh sau để cài đặt các phụ thuộc cần thiết:
  • pnpm run prepare
  • Tạo file pre-commit trong thư mục .husky:
  • 
    #!/usr/bin/env sh
    . "$(dirname -- "$0")/_/husky.sh"
    pnpm run format
                

6. Cấu hình CommitLint cho project

Đang trong quá trình cấu hình...

Thẻ: vite Vue3 typescript ESLint prettier

Đăng vào ngày 4 tháng 6 lúc 05:22