Vue3+TypeScript 项目中,配置 ESLint 和 Prettier
<p>接上篇:从0搭建vite-vue3-ts项目框架:配置less+svg+pinia+vant+axios</p><p><strong>文档同步项目gitee:https://gitee.com/lixin_ajax/vue3-vite-ts-pinia-vant-less.git</strong></p>
<p> </p>
<p><span style="font-size: 18px"><strong>一、Eslint:用于检测代码</strong></span></p>
<p><strong>安装eslint相关依赖</strong></p>
<div class="cnblogs_code">
<pre>yarn add eslint eslint-plugin-vue @typescript-eslint/parser @typescript-eslint/eslint-plugin -D</pre>
</div>
<p>eslint-plugin-vue:仅支持vue,提供的规则可以支持 .vue\js\jsx\ts\tsx 文件校验</p>
<p>@typescript-eslint/parser:解析器,让ESLint拥有规范TypeScript代码的能力</p>
<p>@typescript-eslint/eslint-plugin:插件,包含一系列TypeScript的ESint规则</p>
<p> </p>
<p>初始化eslint</p>
<div class="cnblogs_code">
<pre>npx eslint --init</pre>
</div>
<p>选择项目eslint配置,回车确认,空格多选</p>
<div class="cnblogs_code">
<pre>√ How would you like to use ESLint?<span style="color: rgba(0, 0, 0, 1)"> · style 你希望怎样使用eslint
√ What type of modules does your project use</span>?<span style="color: rgba(0, 0, 0, 1)"> · esm 你的项目使用什么模块
√ Which framework does your project use</span>?<span style="color: rgba(0, 0, 0, 1)"> · vue 项目框架
√ Does your project use TypeScript</span>? · No /<span style="color: rgba(0, 0, 0, 1)"> Yes是否使用typescript
√ Where does your code run</span>?<span style="color: rgba(0, 0, 0, 1)"> · browser, node代码运行在哪
√ How would you like to define a style </span><span style="color: rgba(0, 0, 255, 1)">for</span> your project?<span style="color: rgba(0, 0, 0, 1)"> ·guide 项目样式
√ Which style guide </span><span style="color: rgba(0, 0, 255, 1)">do</span> you want to follow? · standard-<span style="color: rgba(0, 0, 255, 1)">with</span>-<span style="color: rgba(0, 0, 0, 1)">typescript项目风格
√ What format </span><span style="color: rgba(0, 0, 255, 1)">do</span> you want your config file to be <span style="color: rgba(0, 0, 255, 1)">in</span>?<span style="color: rgba(0, 0, 0, 1)"> · JavaScript配置文件格式
√ Would you like to install them now</span>? · No /<span style="color: rgba(0, 0, 0, 1)"> Yes确认是否安装
√ Which package manager </span><span style="color: rgba(0, 0, 255, 1)">do</span> you want to use? · yarn 安装方式</pre>
</div>
<p>安装完成,项目根目录生成.eslintrc.cjs文件</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> .eslintrc.cjs</span>
<span style="color: rgba(0, 0, 0, 1)">
module.exports </span>=<span style="color: rgba(0, 0, 0, 1)"> {
env: {
browser: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
es2021: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
node: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">
},
extends: [
</span>'plugin:vue/vue3-essential'<span style="color: rgba(0, 0, 0, 1)">,
</span>'standard-with-typescript'<span style="color: rgba(0, 0, 0, 1)">
],
overrides: [
],
parserOptions: {
ecmaVersion: </span>'latest'<span style="color: rgba(0, 0, 0, 1)">,
sourceType: </span>'module'<span style="color: rgba(0, 0, 0, 1)">
},
plugins: [
</span>'vue'<span style="color: rgba(0, 0, 0, 1)">
],
rules: {
}
}</span></pre>
</div>
<p>在.eslintrc.cjs rules中配置eslint规则细节</p>
<p>rules配置eslint官网:https://eslint.org/docs/latest/rules/ </p>
<p>腾讯云开发社区中文文档:https://cloud.tencent.com/developer/doc/1078</p>
<p>常用规则,参考:https://blog.csdn.net/ivenqin/article/details/104673237/</p>
<p>eslint-plugin-vue rules:https://eslint.vuejs.org/rules/</p>
<p>我的rules:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:javascript;gutter:true;">rules: {
"vue/no-v-html": "off",
"vue/script-setup-uses-vars": "off",
"@typescript-eslint/ban-ts-ignore": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-empty-function": "off",
"vue/custom-event-name-casing": "off",
"no-use-before-define": "off",
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
},
],
"no-unused-vars": [
"error",
{
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
},
],
"space-before-function-paren": "off",
"vue/attributes-order": "off",
"vue/one-component-per-file": "off",
"vue/html-closing-bracket-newline": "off",
"vue/max-attributes-per-line": "off",
"vue/multiline-html-element-content-newline": "off",
"vue/singleline-html-element-content-newline": "off",
"vue/attribute-hyphenation": "off",
"vue/require-default-prop": "off",
"vue/require-explicit-emits": "off",
"vue/html-self-closing": [
"error",
{
html: {
void: "always",
normal: "never",
component: "always",
},
svg: "always",
math: "always",
},
],
"vue/multi-word-component-names": "off",
"vue/no-parsing-error": ["off"],
"eol-last": "off",
},
</pre>
</div>
<p> </p>
<p> </p>
<p><span style="font-size: 18px"><strong>二、Prettier:用于格式化代码</strong></span></p>
<p> <strong>安装prettier相关依赖</strong></p>
<div class="cnblogs_code">
<pre>yarn add prettier eslint-config-prettier eslint-plugin-prettier stylelint-config-prettier -D</pre>
</div>
<p>eslint-config-prettier:解决eslint和prettier冲突</p>
<p>eslint-config-prettier:将prettier作为eslint规则</p>
<p>stylelint-config-prettier:关闭所有不必要的或者有可能与Prettier冲突的规则</p>
<p> </p>
<p>修改.eslintrc.cjs,配置prettier</p>
<div class="cnblogs_Highlighter">
<pre class="brush:javascript;gutter:true;">extends: [
"plugin:prettier/recommended",
],
plugins: ["prettier"],
rules: {
"prettier/prettier": [
"error",
{
endOfLine: "auto",
},
],
}
</pre>
</div>
<p> </p>
<p>修改package.json,配置修复项目格式命令</p>
<div class="cnblogs_Highlighter">
<pre class="brush:javascript;gutter:true;">"scripts": {
"lint-fix": "eslint . --fix --ext .json,.d.ts,.ts,.vue src/"
},
</pre>
</div>
<p>执行命令,yarn lint-fix,项目按照eslint-prettier规则进行代码格式化</p>
<p>修改prettier规则:根目录创建.prettierrc文件,示例如下,更多规则参考官网</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">{
printWidth: </span>120, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 超过最大值换行</span>
tabWidth: 2, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 缩进字节数</span>
useTabs: <span style="color: rgba(0, 0, 255, 1)">false</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 缩进不使用tab,使用空格</span>
semi: <span style="color: rgba(0, 0, 255, 1)">false</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 句尾添加分号</span>
singleQuote: <span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 使用单引号代替双引号</span>
arrowParens: "avoid", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (x) => {} 箭头函数参数只有一个时是否要有小括号。avoid:省略括号</span>
ignorePath: ".prettierignore", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 不使用prettier格式化的文件填写在项目的.prettierignore文件中</span>
jsxBracketSameLine: <span style="color: rgba(0, 0, 255, 1)">false</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 在jsx中把'>' 是否单独放一行</span>
jsxSingleQuote: <span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 在jsx中使用单引号代替双引号</span>
trailingComma: "all" <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 在对象或数组最后一个元素后面是否加逗号(在ES5中加尾逗号)</span>
}</pre>
</div>
<p>配置完成!</p><br><br>
来源:https://www.cnblogs.com/jing-zhe/p/16934493.html
頁:
[1]