前端项目搭建

113 阅读1分钟

1.Vscode安装EditorConfig for VS Code

创建 .editorconfig 配置文件

# http://editorconfig.org

root = true

[*] # 表示所有文件适用
charset = utf-8 # 设置文件字符集为 utf-8
indent_style = space # 缩进风格(tab | space)
indent_size = 2 # 缩进大小
end_of_line = lf # 控制换行类型(lf | cr | crlf)
trim_trailing_whitespace = true # 去除行首的任意空白字符
insert_final_newline = true # 始终在文件末尾插入一个新行

[*.md] # 表示仅 md 文件适用以下规则
max_line_length = off
trim_trailing_whitespace = false

2.安装prettier

创建 .prettierrc 配置文件

npm install prettier -D
{
  "useTabs": false,
  "tabWidth": 2,
  "printWidth": 80,
  "singleQuote": true,
  "trailingComma": "none",
  "semi": false
}

2.1 配置.prettierrc文件

  • useTabs:使用tab缩进还是空格缩进,选择false;
  • tabWidth:tab是空格的情况下,是几个空格,选择2个;
  • printWidth:当行字符的长度,推荐80,也有人喜欢100或者120;
  • singleQuote:使用单引号还是双引号,选择true,使用单引号;
  • trailingComma:在多行输入的尾逗号是否添加,设置为 none
  • semi:语句末尾是否要加分号,默认值true,选择false表示不加;

2.2 创建.prettierignore忽略文件

/dist/*
.local
.output.js
/node_modules/**

**/*.svg
**/*.sh

/public/*

2.3 测试prettier是否生效

测试一:在代码中保存代码;

测试二:配置一次性修改的命令(package.js中);

"prettier": "prettier --write ."

3.创建项目使用ESLint检测(如果创建项目已经安装了,则无需重复安装)

npm i eslint-plugin-prettier eslint-config-prettier -D

添加prettier插件,在 .eslintrc.js文件中

  parserOptions: {
    // parser: "babel-eslint" 这个属性需要注释掉,不然格式化会很慢(估计是有冲突)
    ecmaVersion: 2020
  },
  extends: [
    "plugin:vue/vue3-essential",
    "eslint:recommended",
    "@vue/typescript/recommended",
    "@vue/prettier",
    "@vue/prettier/@typescript-eslint",
    'plugin:prettier/recommended' //使用插件的规则
  ],