目标:javascript
首先确认了 Prettier 对 TypeScript 有良好的支持.html
https://github.com/prettier/p...java
固然这个依赖 VS Code 的插件, https://github.com/prettier/p...
在 VS Code 中安装以后, 经过 .prettierrc
文件配置, 临时的配置好比(虽然我不喜欢 Tab 但公司用啊):react
{ "tabWidth": 4, "useTabs": true }
配置文件也能够是下面几种:git
package.json
- prettier
key.prettierrc
.prettier.config.js
不过这样还不够, 须要在 VS Code 打开 editor.formatOnSave
选项.
这个选项在 User Settings 当中能够找到, 把值设置为 true
.github
默认状况下, Prettier 会将 JSON 文件也作格式化,
找到的一个方案是在 User Settings 里改为这样的设置:typescript
{ "[javascript]": { "editor.formatOnSave": true, "editor.formatOnPaste": false }, "[javascriptreact]": { "editor.formatOnSave": true, "editor.formatOnPaste": false }, "[typescript]": { "editor.formatOnSave": true, "editor.formatOnPaste": false }, "[typescriptreact]": { "editor.formatOnSave": true, "editor.formatOnPaste": false } }
参考 https://code.visualstudio.com...npm
其中 [typescriptreact]
对应的是 tsx
文件, 须要单独声明.json
为了方便迁移已有的代码, 也就是直接用 prettier
命令行进行格式化, 好比:bash
prettier --write --config .prettierrc src/*.ts
大体上是经过 npm script 结合模块来完成的,
参考 https://prettier.io/docs/en/u...