Prettier - Code formatter使用
更多VSCode插件使用请访问:VSCode常用插件汇总
Prettier - Code formatter这是一款 格式化js、css代码插件,暂不解释。
Prettier是什么?
Prettier的中文意思是“漂亮的、机灵的”,也是一个流行的代码格式化工具的名称,它能够解析代码,使用你自己设定的规则来重新打印出格式规范的代码。
Prettier具有以下几个有优点:
1.可配置化
2.支持多种语言
3.集成多数的编辑器
4.简洁的配置项
使用Prettier在code review时不需要再讨论代码样式,节省了时间与精力。
如何对Prettier进行配置
一共有三种方式支持对Prettier进行配置:
1.根目录创建 .prettierrc 文件,能够写入YML、JSON的配置格式,并且支持.yaml、.yml、.json、.js后缀;
2.根目录创建 .prettier.config.js 文件,并对外export一个对象;
3.在 package.json 中新建prettier属性。
下面我们使用 prettierrc.js 的方式对prettier进行配置,同时讲解下各个配置的作用。
module.exports = {
// 使用 2 个空格缩进
tabWidth: 2,
// 不使用缩进符,而使用空格
useTabs: false,
// 行尾需要有分号
semi: true,
// 使用单引号
singleQuote: true,
// 对象的 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'
}
配置大概列出了这些,还有一些其他配置可以在官方文档进行查阅。
注意一点,parser的配置项官网列出了如下可选项:
babylonflowtypescript Since v1.4.0postcss Since v1.4.0json Since v1.5.0graphql Since v1.5.0markdown Since v1.8.0
但是如果你使用了vue的单文件组件形式,记得将 parser 配置为vue,目前官方文档没有列出来。当然如果你自己写过AST的解析器,也可以用你自己的写的 parser: require("./my-parser") 。
Prettier + editorConfig 搭配使用处理旧项目文件
如果你有一个旧项目想通过Prettier + editorConfig来简单规范一下格式也可以使用这个工具:
1.分别安装两个工具不必多说了
2.创建 .prettierrc 配置文件
该配置文件会继承 editorConfig 的配置
3.向 package.json 添加可执行脚本: prettier": "prettier --write ./*
例如:
prettier": "prettier --write './src/**/*.js' './src/**/*.jsx' './src/**/*.css' './src/**/*.scss'
4.创建 .prettierignore 文件,用来忽略一些文件 && 目录
5.一切准备就绪之后 !!!! npm run prettier 就可以看到提示文件更改记录了
.prettierrc.js配置文件:
# .prettierrc.js 配置文件
// https://prettier.io/docs/en/options.html
module.exports = {
trailingComma: "none", // 数组 对象尾随不需要逗号
bracketSpacing: true, // 对象空格 { foo: bar }, false=> {foo:bar}
jsxBracketSameLine: false, // jsx闭标签是否另起一行 <Test></Test>
arrowParens: 'always', // 箭头函数,只有一个参数的时候,也需要括号
singleQuote: true, // 单引号
semi: true, //是否在语句末尾就分号
};
.prettierignore 文件配置
# .prettierignore 文件配置
/node_modules
/dist
/src/lib/
/src/global/fonts
/docs
/.vscode
node_modules
.eslintrc.js
package.json
package-lock.json
Prettier - Code formatter使用的更多相关文章
- Android Studio Eclipse Code Formatter
在从Eclipse转到Android Studio上开发后,如果还想继续使用在Eclipse上制定的自定义的Code Formatter的话,需要按如下步骤操作:1.进入Settings界面,如果能看 ...
- 【Idea】idea code style配置eclipse code formatter
在eclipse中有自动配置code style的功能 ,但是idea上却没有,这个时候需要自己手工配置 1. 在idea中找到Preference->Plugins->Browse re ...
- idea中Eclipse Code Formatter插件设置和使用,以及注释模板的修改
在settings里面找到plugins这个选项,搜索Eclipse Code Formatter,点击安装,重启idea即可进行配置: 首先,先安装Eclipse Code Formatter插件: ...
- Book Contents Reviews Notes Errata Articles Talks Downloads Resources Code Formatter Cover of C# in Depth Order now (3rd edition) Implementing the Singleton Pattern in C#
原文链接地址: http://csharpindepth.com/Articles/General/Singleton.aspx#unsafe Implementing the Singleton P ...
- Visual Studio Code扩展:
Auto Close TagAuto Rename TagBeautifyChinese (Simplified) Language Pack for Visual Studio CodeClass ...
- 在vscode中使用eslint+prettier格式化vue项目代码 (转载)
ESlint:javascript代码检测工具,可以配置每次保存时格式化js,但每次保存只格式化一点点,你得连续按住Ctrl+S好几次,才格式化好,自行体会~~ vetur:可以格式化html.标准c ...
- 高效开发者是如何个性化VS Code插件与配置的?
2年之前,我放弃了Sublime Text,选择了Visual Studio Code作为代码编辑器. 我每天花在VS Code上的时间长达5~6个小时,因此按照我的需求优化VS Code配置十分必要 ...
- 作为JavaScript开发人员,这些必备的VS Code插件你都用过吗?
本文翻译自:https://www.sitepoint.com/vs-code-extensions-javascript-developers/ 转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的 ...
- eslint prettier editrorconfig - 写出干净的前端代码
FConfidence 关注 2018.12.30 02:38* 字数 2912 阅读 195评论 0喜欢 0 VSCode 插件安装 Prettier - Code Formatter ESLint ...
随机推荐
- Load_file 常用路径
load_file 常用路径 WINDOWS下: c:/boot.ini //查看系统版本 c:/windows/php.ini //php配置信息 c:/windows/my.ini //MYSQL ...
- linux内核编译 fatal error: openssl/opensslv.h: No such file or directory
ubuntu下缺少了的组件,安装即可: sudo apt-get install libssl-dev
- JavaScript 箭头函数(Lambda表达式)
Lambda表达式(箭头函数)用于表示一个函数,所以它和函数一样,也拥有参数.返回值.函数体,但它没有函数名,所以Lambda表达式相当于一个匿名函数. 使用方法: ()=>{} 小括号里放参数 ...
- RabbitMQ工作模式
------------恢复内容开始------------ RabbitMQ基本概念: Producer:生产者(消息的提供者) Consumer:消费者(消息的使用者) Message:消息(程序 ...
- 如何用apply实现一个bind?
面试题:如何用apply实现一个bind? Function.prototype._bind = function(target) { // 保留调用_bind方法的对象 let _this = th ...
- 【HDU - 1029】Ignatius and the Princess IV (水题)
Ignatius and the Princess IV 先搬中文 Descriptions: 给你n个数字,你需要找出出现至少(n+1)/2次的数字 现在需要你找出这个数字是多少? Input ...
- Mac下搭建selenium环境
1,安装selenium 打开terminal,使用以下命令安装selenium: pip install -U selenium 2,下载chromedriver,并放在python的安装根目录 ...
- 【已解决】pyinstaller UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xce in position 110: invalid continuation byte
转载自勤奋的小青蛙本文链接地址: [已解决]pyinstaller UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xce in positi ...
- js的reduce累加器
reduce为数组中每一个元素执行回调函数,不包括被删除或未被赋值的 https://www.jianshu.com/p/e375ba1cfc47
- 重启防火墙(iptables)命令#service iptable restart失效
Redirecting to /bin/systemctl restart iptables.ser linux下执行防火墙相关指令报错: Redirecting to /bin/systemctl ...