.editorconfig

创建一个 .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

VSCode需要安装一个插件:EditorConfig for VS Code

  1. prettier(保存时)

    ctrl + shift + p 选择默认 为 prettier

    vscode => format on save : true

    根目录 创建 .prettierrc

    # http://editorconfig.org
    
    root = true
    
    [*] # 表示所有文件适用
    charset = utf-8 # 设置文件字符集为 utf-8
    indent_style = space # 缩进风格(tab | space)
    indent_size = 4 # 缩进大小
    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
    
    

    在项目根目录中创建忽略文件 .prettierignore,文件格式使用 gitignore syntax

    /dist/*
    .local
    .output.js
    /node_modules/**
    
    **/*.svg
    **/*.sh
    
    /public/*
    

    推荐

格式化代码风格的前端工具

在 CLI 中使用

本地安装 Prettier

npm i -D prettier

在项目根目录中创建配置文件

  • .prettierrc.jsprettier.config.js
  • .prettierrc.json
  • package.json 中的 prettier 字段
// prettier.config.js or .prettierrc.js
module.exports = {
  // 是否使用尾逗号
  trailingComma: "none",
  // 按 Tab 键的时候的缩进方式,true 使用 tab 缩进,false 将 tab 转换为空格缩进
  useTabs: false,
  // useTabs: false 的时候,使用空格缩进缩进几个空格
  tabWidth: 4,
  // 语句结尾是否加分号
  semi: true,
  // 字符串是否使用单引号
  singleQuote: true
};

在项目根目录中创建 .prettierignore,这样就可以在项目根目录下执行 prettier --write . 来格式化所有文件。

执行 CLI 命令

# 格式化当前目录中的所有文件(除了 .prettierignore 里忽略的文件)
npx prettier --write .
# 格式化指定目录中的所有文件
npx prettier --write src/
# 格式化某个具体文件
npx prettier --write src/index.js
# 格式化与 glob 模式匹配的所有文件
npx prettier --write src/**/*.test.js

配置文件

// prettier.config.js or .prettierrc.js
module.exports = {
    // 一行最多 100 字符
    printWidth: 100,
    // 使用 4 个空格缩进
    tabWidth: 4,
    // 不使用缩进符,而使用空格
    useTabs: false,
    // 尾逗号规则
    trailingComma: "es5",
    // 行尾需要有分号
    semi: false,
    // 使用单引号
    singleQuote: false,
    // 对象的 key 仅在必要时用引号
    quoteProps: 'as-needed',
    // jsx 不使用单引号,而使用双引号
    jsxSingleQuote: false,
    // 末尾不需要逗号
    trailingComma: 'none',
    // 大括号内的首尾需要空格
    bracketSpacing: true,
    // jsx 标签的反尖括号需要换行
    jsxBracketSameLine: false,
    // 箭头函数,只有一个参数的时候,也需要括号
    arrowParens: 'always',
    // 每个文件格式化的范围是文件的全部内容
    rangeStart: 0,
    rangeEnd: Infinity,
    // 不需要写文件开头的 @prettier
    requirePragma: false,
    // 不需要自动在文件开头插入 @prettier
    insertPragma: false,
    // 使用默认的折行标准
    proseWrap: 'preserve',
    // 根据显示样式决定 html 要不要折行
    htmlWhitespaceSensitivity: 'css',
    // 换行符使用 lf
    endOfLine: 'lf'
};

在 VSCode 中使用

  1. 本地安装 Prettier npm i -D prettier
  2. 项目根目录中创建配置文件 .prettierrc.js
  3. 项目根目录中创建忽略文件 .prettierignore
  4. 在 VSCode 中安装 Prettier 插件:shift command x 搜索并安装 Prettier 插件
  5. 通过以下任意一种方式设置保存时自动使用 Prettier 格式化:
    • 控制面板设置:command + , 搜索 format on save,打钩
    • 修改 vscode 配置文件:"editor.formatOnSave": true

新版本的 vscode 里面自带一个 javascript 和 typescript 的 format 工具,默认使用该工具进行格式化。因此在保存文件的时候,会使用自带的 format 工具格式化,而不会使用 Prettier 格式化。如果我们希望保存的时候使用 Prettier 来格式化,要么关闭 vscode 自带的 format 工具,要么设置优先使用 Prettier 来 格式化。

关闭 vscode 自带的 format 工具

command + , 打开控制面板,搜索 TypeScript Format Enable 和 Javascript Format Enable,选项不要打钩。

设置完之后,就会自动在工作区或者用户区的 vscode 配置文件里添加如下配置:

{
    "javascript.format.enable": false,
    "typescript.format.enable": false
}

你也可以直接在对应的配置文件里手动添加这两行来关闭 vscode 自带的 format 工具。

设置优先使用 Prettier 来格式化

这种方式不需要关闭 vscode 自带的 format 工具,你打开一个 .js 文件,按 shift command P 打开命令行面板,搜索 Format Document,点击它,会提示“文件有多个格式化程序,选择默认格式化程序以继续”。点击配置,选择 Prettier 即可。

设置完之后,就会自动在用户区的 vscode 配置文件里面添加如下配置:

"[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
}

你也可以直接在用户区或者工作区的配置文件里面手动添加上面的配置。

如果你希望在 .ts 类型的文件里面也能够优先使用 Prettier 格式化,你需要打开一个 .ts 类型的文件,然后重复以上的步骤。就会自动在用户区的 vscode 配置文件里面添加如下配置:

"[typescript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
}

你也可以直接在用户区或者工作区的配置文件里面手动添加上面的配置。

其他类型的文件也如此,例如 .vue

.editorconfig 文件一起使用

如果存在 .editorconfig 文件,Prettier 会根据 .editorconfig 文件中的以下选项自动转换为对应的 Prettier 配置信息:

  • max_line_length => printWidth
  • tab_width => tabWidth
  • indent_style => useTabs
  • end_of_line => endOfLine

自定义的 Prettier 配置文件中的对应项可以覆盖从 .editorconfig 文件自动转换过来的对应项。

其实有了 Prettier 就不需要 .editorconfig 文件了
3. 使用ESLint检测
1.如果前面创建项目的时候,我们就选择了ESLint,Vue会默认帮助我们配置需要的ESLint环境。

2.VSCode需要安装ESLint插件:ESLint

3.解决eslint和prettier冲突的问题:安装插件:(vue在创建项目时,如果选择prettier,那么这两个插件会自动安装)

npm i eslint-plugin-prettier eslint-config-prettier -D
.eslintrc.js中添加prettier插件:

  extends: [
    "plugin:vue/vue3-essential",
    "eslint:recommended",
    "@vue/typescript/recommended",
    "@vue/prettier",
    "@vue/prettier/@typescript-eslint",
    'plugin:prettier/recommended'
  ],

重启一下vs code

如果.eslintrc.js文件缩进有问题, eslint和prettier冲突,可以试试在settings.json中配置一下

// 配置全局 eslint 配置
“eslint.options”: {
“configFile”: “F:\devFiles\devToolsSettings\.eslintrc.js”
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值