【Uni-App】vscode 开发uni-app 配置eslint、prettier 实现代码检查和代码自动格式化
创始人
2024-04-23 20:35:27
0

目录

    • 一:前言
    • 二、利用HBuilderX创建uni-app项目
    • 三、配置代码检查和代码自动格式化
        • 1. 在vscode中打开项目
        • 2. 创建package.json
        • 3. 添加eslint、prettier相关依赖
        • 4. 配置.eslintrc.js
        • 5. 配置.prettierrc.json
        • 6. 配置.editorconfig
        • 7. 配置.eslintignore
    • 如果以上还不生效:
        • 1.根目录下创建.vscode文件夹
        • 2..vscode文件夹下创建settings.json文件
        • 3 .prettierrc文件配置

一:前言

本来很早之前就想总结一下关于uni-app的一些代码自动格式化方面的配置
因为公司项目原因,拆分了新老项目,需要带新人,每个人的开发方式不一样
所以为了项目的代码风格统一性,以及为了避免后期的git分支操作冲突

我自己的 HbuiiderX开发uni-app时已经搞好了代码检查和自动格式化,奈何其他同事都不会设置,导致了很多次代码提交,显示有很多改动,导致代码覆盖问题。
于是有了这篇文章。

本文实现的方式是:利用vscode进行编码,用HBuilderX进行开发

二、利用HBuilderX创建uni-app项目

在这里插入图片描述

三、配置代码检查和代码自动格式化

1. 在vscode中打开项目

在这里插入图片描述

2. 创建package.json

npm init

一路回车即可
在这里插入图片描述

3. 添加eslint、prettier相关依赖

npm install @vue/cli-plugin-eslint @vue/eslint-config-prettier babel-eslint eslint eslint-plugin-prettier eslint-plugin-vue prettier --save-dev
// or
cnpm install @vue/cli-plugin-eslint @vue/eslint-config-prettier babel-eslint eslint eslint-plugin-prettier eslint-plugin-vue prettier --save-dev
//or
yarn add @vue/cli-plugin-eslint @vue/eslint-config-prettier babel-eslint eslint eslint-plugin-prettier eslint-plugin-vue prettier --save-dev

4. 配置.eslintrc.js

根目录下创建.eslintrc.js文件,并填入以下代码

module.exports = {root: true,parserOptions: {parser: 'babel-eslint',sourceType: 'module'},env: {browser: true,node: true,es6: true},extends: ['plugin:vue/essential', 'eslint:recommended', '@vue/prettier'],rules: {'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off','no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off','no-unused-vars':'off',},
}

5. 配置.prettierrc.json

根目录下创建.prettierrc.json文件,并填入以下代码

这里可以根据个人代码风格喜好来配置

{"semi": false,"singleQuote": true,"endOfLine": "auto","printWidth": 100
}

此时已实现代码检查和代码自动格式化,如果存在其他格式化问题,重启vscode即可解决。

6. 配置.editorconfig

# http://editorconfig.org
root = true[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true[*]
charset=utf-8
end_of_line=lf
insert_final_newline=false
indent_style=space
indent_size=2[{*.ng,*.sht,*.html,*.shtm,*.shtml,*.htm}]
indent_style=space
indent_size=2[{*.jhm,*.xslt,*.xul,*.rng,*.xsl,*.xsd,*.ant,*.tld,*.fxml,*.jrxml,*.xml,*.jnlp,*.wsdl}]
indent_style=space
indent_size=2[{.babelrc,.stylelintrc,jest.config,.eslintrc,.prettierrc,*.json,*.jsb3,*.jsb2,*.bowerrc}]
indent_style=space
indent_size=2[*.svg]
indent_style=space
indent_size=2[*.js.map]
indent_style=space
indent_size=2[*.less]
indent_style=space
indent_size=2[*.vue]
indent_style=space
indent_size=2[{.analysis_options,*.yml,*.yaml}]
indent_style=space
indent_size=2[*.md]
insert_final_newline = false
trim_trailing_whitespace = false

7. 配置.eslintignore

后续有其他第三方插件不想被eslint影响,可以添加到下面

build/*.js
src/assets
public
dist
/node_modules

以上,就可以在vscode上码代码,在HBuilderX上运行项目。先做个笔记记录一下,后续有问题得话再补充。

如果以上还不生效:

1.根目录下创建.vscode文件夹

在这里插入图片描述

2…vscode文件夹下创建settings.json文件

{"editor.formatOnSave": true,"eslint.validate": ["javascript", "javascriptreact", "typescriptreact", "typescript", "vue"],"path-autocomplete.pathMappings": {"@": "${folder}/src"},"path-autocomplete.extensionOnImport": true,"npm-intellisense.importES6": true,"npm-intellisense.importDeclarationType": "const","npm-intellisense.importQuotes": "'","npm-intellisense.importLinebreak": ";\r\n","typescript.tsdk": "./node_modules/typescript/lib","typescript.enablePromptUseWorkspaceTsdk": true,"volar.tsPlugin": true,"volar.tsPluginStatus": false,//===========================================//============= Editor ======================//==========================================="explorer.openEditors.visible": 0,"editor.tabSize": 2,"editor.defaultFormatter": "esbenp.prettier-vscode","diffEditor.ignoreTrimWhitespace": false,//===========================================//============= Other =======================//==========================================="breadcrumbs.enabled": true,"open-in-browser.default": "chrome",//===========================================//============= files =======================//==========================================="files.eol": "\n","search.exclude": {"**/node_modules": true,"**/*.log": true,"**/*.log*": true,"**/bower_components": true,"**/dist": true,"**/elehukouben": true,"**/.git": true,"**/.gitignore": true,"**/.svn": true,"**/.DS_Store": true,"**/.idea": true,"**/.vscode": false,"**/yarn.lock": true,"**/tmp": true,"out": true,"dist": true,"node_modules": true,"CHANGELOG.md": true,"examples": true,"res": true,"screenshots": true,"yarn-error.log": true,"**/.yarn": true},"files.exclude": {"**/.cache": true,"**/.editorconfig": true,"**/.eslintcache": true,"**/bower_components": true,"**/.idea": true,"**/tmp": true,"**/.git": true,"**/.svn": true,"**/.hg": true,"**/CVS": true,"**/.DS_Store": true},"files.watcherExclude": {"**/.git/objects/**": true,"**/.git/subtree-cache/**": true,"**/.vscode/**": true,"**/node_modules/**": true,"**/tmp/**": true,"**/bower_components/**": true,"**/dist/**": true,"**/yarn.lock": true},"stylelint.enable": true,"stylelint.packageManager": "yarn","liveServer.settings.donotShowInfoMsg": true,"telemetry.enableCrashReporter": false,"workbench.settings.enableNaturalLanguageSearch": false,"path-intellisense.mappings": {"/@/": "${workspaceRoot}/src"},"prettier.requireConfig": true,"typescript.updateImportsOnFileMove.enabled": "always","workbench.sideBar.location": "left","[javascriptreact]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[typescript]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[typescriptreact]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[html]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[css]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[less]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[scss]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[markdown]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"editor.codeActionsOnSave": {"source.fixAll.eslint": true,"source.fixAll.stylelint": true // 自动格式化vue的template},"[vue]": {"editor.codeActionsOnSave": {"source.fixAll.eslint": true, // 开启格式化eslint"source.fixAll.stylelint": true // 自动格式化stylelint}},"i18n-ally.localesPaths": ["src/locales/lang"],"i18n-ally.keystyle": "nested","i18n-ally.sortKeys": true,"i18n-ally.namespace": true,"i18n-ally.pathMatcher": "{locale}/{namespaces}.{ext}","i18n-ally.enabledParsers": ["ts"],"i18n-ally.sourceLanguage": "en","i18n-ally.enabledFrameworks": ["vue", "react"],"cSpell.words": ["vben","windi","browserslist","tailwindcss","esnext","antv","tinymce","qrcode","sider","pinia","sider","nprogress","INTLIFY","stylelint","esno","vitejs","sortablejs","mockjs","codemirror","iconify","commitlint","vditor","echarts","cropperjs","logicflow","vueuse","zxcvbn","lintstagedrc","brotli"]
}

3 .prettierrc文件配置

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

相关内容

热门资讯

请重新安装完美系统,重新安装系... 电脑系统崩溃?完美解决方案:重新安装系统随着电脑使用时间的增长,系统可能会出现各种问题,如运行缓慢、...
安装新风系统有灰尘吗,安装新风... 安装新风系统,告别室内灰尘困扰?随着生活水平的提高,人们对居住环境的要求也越来越高。灰尘问题一直是家...
安卓系统怎么安装鸿蒙,安卓系统... 安卓系统如何安装鸿蒙系统——全面指南一、了解鸿蒙系统鸿蒙系统(HarmonyOS)是华为自主研发的操...
安装新风系统后不热,安装新风系... 安装新风系统,告别夏季炎热,享受清凉家居生活随着夏季的到来,高温和潮湿的天气让许多人感到不适。传统的...
清新区上门安装系统,清新区上门... 清新区上门安装系统服务,让电脑问题轻松解决上门安装系统服务,专业解决电脑难题清新区上门安装系统服务拥...
奥迪监控系统安装,提升行车安全... 奥迪监控系统安装:提升行车安全与便利性的智能升级一、奥迪监控系统安装概述奥迪监控系统主要包括车视野全...
安装正版系统详细教程图,安装正... 安装正版系统详细教程图解随着电脑使用时间的增长,系统可能会出现各种问题,重装系统成为了解决问题的有效...
安装新系统鼠标,安装新系统后鼠... 安装新系统后鼠标无法使用?快速解决指南在安装新操作系统后,遇到鼠标无法使用的情况并不罕见。本文将为您...
拜腾系统安装,拜腾系统安装指南 拜腾系统安装指南在开始安装拜腾系统之前,请确保您已经做好了以下准备工作: 一台电脑或平板电脑,用于...
安装新主板和cpu后怎么安装系... 安装新主板和CPU后如何安装系统准备工作在开始安装系统之前,我们需要做好以下准备工作: 备份重...
安装语音启动系统教程,榧庝俊补... 安装语音启动系统教程 一、前言 二、准备工作在开始安装之前,请确保您的设备满足以下条件: 操作系统...
安卓电视系统如何安装,安卓电视... 安卓电视系统安装指南随着智能电视的普及,越来越多的用户选择使用安卓电视系统。安卓电视系统不仅操作简便...
安装销售监控系统的好处,提升企... 安装销售监控系统:提升企业竞争力的五大优势随着市场竞争的日益激烈,企业对于销售环节的精细化管理需求越...
安装萤石系统多少钱,萤石云视频... 萤石系统安装费用一览:性价比与服务的完美结合随着社会安全意识的不断提高,家庭和企业对视频监控系统的需...
把win系统安装到u盘安装,如... 如何将Windows系统安装到U盘在开始安装之前,请确保您已经准备好了以下物品: 一个空的U盘,建...
安装小猫停车场系统,小猫停车场... 小猫停车场系统安装指南随着城市化进程的加快,停车难问题日益凸显。小猫停车场系统作为一种智能化的停车解...
安装系统重启只有光标,安装系统... 安装系统后重启只出现光标怎么办?全面解析及解决方案在电脑使用过程中,遇到安装系统后重启只出现光标的情...
请问个税系统怎么安装,轻松掌握... 个税系统安装指南:轻松掌握个税申报流程随着个人所得税改革的深入推进,个税系统的安装和使用变得越来越重...
安装隐藏子系统,安装隐藏子系统... 安装隐藏子系统的详细指南随着计算机技术的发展,隐藏子系统成为了许多高级用户和系统管理员追求的目标。隐...
安装造价给水系统,安装造价给水... 安装造价给水系统概述安装造价给水系统是建筑给水工程的重要组成部分,它负责将水从水源地输送到建筑物内部...