【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",
};

相关内容

热门资讯

安卓系统能转什么系统好,探索最... 你有没有想过,你的安卓手机是不是也能换换口味,体验一下其他系统的魅力呢?没错,今天就来聊聊这个话题:...
龙之狂热安卓系统,释放龙族狂热 亲爱的手机控们,你是否曾为拥有一款独特的安卓系统而疯狂?今天,就让我带你走进一个充满奇幻色彩的龙之狂...
vivo手机安卓系统怎么升级系... 亲爱的手机控们,你是不是也和我一样,对手机的新功能充满期待呢?尤其是vivo手机的用户,是不是也在想...
鸿蒙2.0退回安卓系统,一场系... 你知道吗?最近科技圈里可是炸开了锅,因为华为的鸿蒙2.0操作系统竟然要退回安卓系统了!这可不是一个简...
安卓系统怎么复制卡,安卓系统卡... 你有没有遇到过这种情况:手机里的照片、视频或者重要文件,突然想备份到电脑上,却发现安卓系统的卡复制功...
app兼容低安卓系统,打造全民... 你有没有发现,现在手机APP更新换代的速度简直就像坐上了火箭!不过,你知道吗?有些APP可是特别贴心...
中间安卓系统叫什么,中间安卓系... 你有没有想过,安卓系统里竟然还有一个中间的版本?没错,就是那个让很多手机用户既熟悉又陌生的版本。今天...
安卓怎么用os系统,利用And... 你有没有想过,你的安卓手机其实可以变身成一个功能强大的操作系统呢?没错,就是那个我们平时在电脑上使用...
pe系统安卓能做么,探索安卓平... 亲爱的读者,你是否曾好奇过,那款在安卓设备上大受欢迎的PE系统,它究竟能做什么呢?今天,就让我带你一...
安卓 打印机系统,安卓打印机系... 你有没有想过,家里的安卓手机和打印机之间竟然能建立起如此紧密的联系?没错,就是那个安卓打印机系统!今...
安卓系统视频做铃声,轻松将视频... 你有没有想过,手机里那些动人心弦的视频,竟然可以变成手机铃声?没错,就是那种一响起就能瞬间抓住你耳朵...
海信电视安卓系统更新,畅享智能... 亲爱的电视迷们,你是否也像我一样,对家里的那台海信电视充满了期待?最近,海信电视安卓系统迎来了一次大...
安卓系统网页不能载入,排查与解... 最近是不是你也遇到了安卓系统网页不能载入的烦恼?别急,让我来帮你一探究竟,找出解决之道!一、问题现象...
赛欧3安卓系统,智能出行新体验 你有没有发现,现在的汽车越来越智能了?这不,我最近就发现了一款特别有意思的车型——赛欧3,它竟然搭载...
能装安卓系统吗,哪些设备能轻松... 你有没有想过,那些看起来普普通通的平板电脑,其实里面藏着大大的秘密呢?没错,就是能装安卓系统!今天,...
安卓能变苹果系统吗,技术揭秘与... 你有没有想过,安卓手机能不能变成苹果系统呢?这听起来就像是科幻电影里的情节,但今天,我们就来揭开这个...
车载安卓系统好卡,车载安卓系统... 你有没有遇到过这样的情况?你的车载安卓系统突然变得超级卡,就像蜗牛一样慢吞吞的,让人抓狂!没错,我就...
安卓系统怎样删除固件,轻松优化... 你有没有遇到过这种情况:手机里的安卓系统突然变得卡顿,或者某个固件版本让你觉得不爽,想要重新来过?别...
安卓鸿蒙系统视频对比,性能与体... 亲爱的读者们,你是否也像我一样,对安卓和鸿蒙系统之间的较量充满了好奇?今天,就让我们一起揭开这场科技...
电脑安卓系统横评,横跨性能与体... 你有没有想过,你的手机和电脑,其实就像两个超级英雄,各有各的本领和特点?今天,就让我带你来一场电脑安...