您现在的位置是:首页 >技术杂谈 >【Vue】Vue3脚手架网站首页技术杂谈

【Vue】Vue3脚手架

pass night 2023-06-13 04:00:03
简介【Vue】Vue3脚手架

创建项目1

npm init vue@latest

配置代码质量检测工具

安装Commitizen2

pip3 install -U Commitizen

安装eslint

pnpm install lint-staged -D
pnpm install eslint -D
/* eslint-env node */
// .eslintrc.cjs
require("@rushstack/eslint-patch/modern-module-resolution");

module.exports = {
  root: true,
  env: {
    node: true,
  },
  parser: "vue-eslint-parser",
  parserOptions: {
    parser: "@typescript-eslint/parser",
    ecmaVersion: "latest",
    sourceType: "module",
    jsxPragma: "React",
    ecmaFeatures: {
      jsx: true,
    },
  },
  extends: [
    "plugin:vue/vue3-essential",
    "@vue/typescript/recommended",
    "eslint:recommended",
    "@vue/eslint-config-typescript",
    "@vue/eslint-config-prettier",
  ],
};

安装prettier

pnpm install prettier -D
pnpm install pretty-quick -D
// .prettierrc.json
{
  "$schema": "https://json.schemastore.org/prettierrc",
  "semi": false,
  "tabWidth": 2,
  "singleQuote": false,
  "printWidth": 100,
  "trailingComma": "none",
  "bracketSpacing": true
}

添加stylelint3

pnpm install stylelint-order -D
pnpm install stylelint-config-standard -D
pnpm install stylelint-config-prettier -D
pnpm install stylelint-config-recommended 
pnpm install stylelint-config-html -D
pnpm install postcss-html -D
pnpm install stylelint -D
# .stylelintignore
/dist/*
/public/*
public/*
// .stylelintrc.js
module.exports = {
  root: true,
  plugins: ["stylelint-order"],
  customSyntax: "postcss-html",
  extends: ["stylelint-config-standard", "stylelint-config-prettier"],
  rules: {
    "selector-class-pattern": null,
    "selector-pseudo-class-no-unknown": [
      true,
      {
        ignorePseudoClasses: ["global"]
      }
    ],
    "selector-pseudo-element-no-unknown": [
      true,
      {
        ignorePseudoElements: ["v-deep"]
      }
    ],
    "at-rule-no-unknown": [
      true,
      {
        ignoreAtRules: [
          "tailwind",
          "apply",
          "variants",
          "responsive",
          "screen",
          "function",
          "if",
          "each",
          "include",
          "mixin"
        ]
      }
    ],
    "no-empty-source": null,
    "named-grid-areas-no-invalid": null,
    "unicode-bom": "never",
    "no-descending-specificity": null,
    "font-family-no-missing-generic-family-keyword": null,
    "declaration-colon-space-after": "always-single-line",
    "declaration-colon-space-before": "never",
    "rule-empty-line-before": [
      "always",
      {
        ignore: ["after-comment", "first-nested"]
      }
    ],
    "unit-no-unknown": [true, { ignoreUnits: ["rpx"] }],
    "order/order": [
      [
        "dollar-variables",
        "custom-properties",
        "at-rules",
        "declarations",
        {
          type: "at-rule",
          name: "supports"
        },
        {
          type: "at-rule",
          name: "media"
        },
        "rules"
      ],
      { severity: "warning" }
    ]
  },
  ignoreFiles: ["**/*.js", "**/*.jsx", "**/*.tsx", "**/*.ts", "**/*.json"],
  overrides: [
    {
      files: ["*.vue", "**/*.vue", "*.html", "**/*.html"],
      extends: ["stylelint-config-recommended", "stylelint-config-html"],
      rules: {
        "keyframes-name-pattern": null,
        "selector-pseudo-class-no-unknown": [
          true,
          {
            ignorePseudoClasses: ["deep", "global"]
          }
        ],
        "selector-pseudo-element-no-unknown": [
          true,
          {
            ignorePseudoElements: ["v-deep", "v-global", "v-slotted"]
          }
        ]
      }
    }
  ]
};

在package.json中添加lint脚本

// package.json
"scripts":{
    "lint:eslint": "eslint --cache --max-warnings 0  "{src,mock,build}/**/*.{vue,js,ts,tsx}" --fix",
    "lint:prettier": "prettier --write  "src/**/*.{js,ts,json,tsx,css,less,scss,vue,html,md}"",
    "lint:stylelint": "stylelint --cache --fix "**/*.{vue,css,scss,postcss,less}" --cache --cache-location node_modules/.cache/stylelint/",
    "lint:lint-staged": "lint-staged -c ./.husky/lintstagedrc.js",
    "lint:pretty": "pretty-quick --staged",
    "lint": "pnpm lint:eslint && pnpm lint:prettier && pnpm lint:stylelint",
}

安装husky

npx husky-init	// 初始化husky
pnpm install
# .husky/pre-commit
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm test
npm run lint:lint-staged
npm run lint:pretty
// .husky/lintstagedrc.js
// 配置lint-stage
module.exports = {
  "*.{js,jsx,ts,tsx}": ["eslint --fix", "prettier --write"],
  "{!(package)*.json}": ["prettier --write--parser json"],
  "package.json": ["prettier --write"],
  "*.vue": ["eslint --fix", "prettier --write", "stylelint --fix"],
  "*.{vue,css,scss,postcss,less}": ["stylelint --fix", "prettier --write"],
  "*.md": ["prettier --write"]
};

添加commitlint

安装commitlint

pnpm install @commitlint/config-conventional @commitlint/cli -D

配置commitlint

// commitlint.config.js
module.exports = {
  ignores: [(commit) => commit.includes("init")],
  extends: ["@commitlint/config-conventional"],
  rules: {
    "body-leading-blank": [2, "always"],
    "footer-leading-blank": [1, "always"],
    "header-max-length": [2, "always", 108],
    "subject-empty": [2, "never"],
    "type-empty": [2, "never"],
    "type-enum": [
      2,
      "always",
      [
        "feat",
        "fix",
        "perf",
        "style",
        "docs",
        "test",
        "refactor",
        "build",
        "ci",
        "chore",
        "revert",
        "wip",
        "workflow",
        "types",
        "release",
      ],
    ],
  },
};

集成到husky

# commit-msg
#!/bin/sh

# shellcheck source=./_/husky.sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install commitlint --edit "$1"

安装常用工具

安装tailwind

pnpm install -D tailwindcss@latest postcss@latest autoprefixer@latest
npx tailwindcss init
// main.ts
import "tailwindcss/tailwind.css"

配置tailwind4

// tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  darkMode: "class",
  corePlugins: {
    preflight: false
  },
  content: ["./index.html", "./src/**/*.{vue,js,ts,jsx,tsx}"],
  theme: {
    extend: {
      colors: {
        bg_color: "var(--el-bg-color)",
        primary: "var(--el-color-primary)",
        primary_light_9: "var(--el-color-primary-light-9)",
        text_color_primary: "var(--el-text-color-primary)",
        text_color_regular: "var(--el-text-color-regular)",
        text_color_disabled: "var(--el-text-color-disabled)"
      }
    }
  },
  plugins: []
}

安装element-plus5

pnpm install element-plus

配置volar提示

// tsconfig.json
{
  "compilerOptions": {
    "types": ["element-plus/global"]
  },
}

配置按需导入

pnpm install -D unplugin-vue-components unplugin-auto-import
// vite.config.ts
import { defineConfig } from 'vite'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

export default defineConfig({
  // ...
  plugins: [
    // ...
    AutoImport({
      resolvers: [ElementPlusResolver()],
      dts: "types/auto-imports.d.ts"
    }),
    Components({
      resolvers: [ElementPlusResolver()],
      dts: "types/components.d.ts"
    }),
  ],
})
// tsconfig.json
{
      "include": ["types/*.d.ts"],
}

引用


  1. 快速上手 | Vue.js (vuejs.org) ↩︎

  2. Commitizen (commitizen-tools.github.io) ↩︎

  3. Home | Stylelint 中文文档 (bootcss.com) ↩︎

  4. 安装 - Tailwind CSS 中文文档 ↩︎

  5. 快速开始 | Element Plus (gitee.io)↩︎

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。