vue3+vite3项目实现保存自动化格式
- 安装eslint
npm i -D eslint eslint-plugin-vue @typescript-eslint/parser @typescript-eslint/eslint-plugin
- 安装prettier
npm i -D prettier eslint-config-prettier eslint-plugin-prettier
- 在根目录创建.eslintrc.js
官网链接:https://eslint.bootcss.com/docs/rules/
module.exports = {
root: true,
env: {
node: true
},
extends: [
'plugin:vue/vue3-essential',
'eslint:recommended',
'@vue/typescript/recommended',
'@vue/prettier',
'@vue/prettier/@typescript-eslint',
'plugin:prettier/recommended'
],
parserOptions: {
parsar: '@typescript-eslint/parsar',
ecmaVersion: 2020,
sourceType: 'module'
},
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off'
}
}
- 在根目录创建.prettierrc.cjs
module.exports = {
printWidth: 80,
tabWidth: 2,
useTabs: false,
semi: false,
vueIndentScriptAndStyle: true,
singleQuote: true,
trailingComma: 'none',
bracketSpacing: true,
arrowParens: 'always',
insertPragma: false,
endOfLine: 'auto'
}
- 在根目录创建.vscode文件夹,在文件夹中创建setting.json文件
{
"files.exclude": {
"**/.classpath": true,
"**/.project": true,
"**/.settings": true,
"**/.factorypath": true
},
"workbench.startupEditor": "none",
"vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
"redhat.telemetry.enabled": true,
"java.jdt.ls.vmargs": "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx1G -Xms100m",
"tabnine.experimentalAutoImports": true,
"kite.showWelcomeNotificationOnStartup": false,
"[vue]": {
"editor.defaultFormatter": "octref.vetur"
},
"window.zoomLevel": 1,
"workbench.iconTheme": "vscode-icons",
"workbench.colorTheme": "One Monokai",
"editor.cursorBlinking": "smooth",
"editor.cursorSmoothCaretAnimation": true,
"diffEditor.codeLens": true,
"editor.minimap.maxColumn": 120,
"editor.fontSize": 14,
"workbench.editor.autoLockGroups": {
"imagePreview.previewEditor": true,
"mainThreadWebview-markdown.preview": true
},
"eslint.alwaysShowStatus": true,
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"scssFormatter.printWidth": 200,
"scssFormatter.singleQuote": true,
"editor.tabSize": 2,
"files.autoSave": "afterDelay",
"diffEditor.wordWrap": "off",
"prettier.singleQuote": true,
"prettier.trailingComma": "all",
"prettier.eslintIntegration": true,
"eslint.validate": [
"html",
"vue",
"vue-html",
"javascript",
"javascriptreact"
],
"eslint.options": {
"extensions": [
".js",
".vue",
".ts",
".tsx"
]
},
"javascript.format.insertSpaceBeforeFunctionParenthesis": true,
"vetur.format.defaultFormatter.html": "js-beautify-html",
"vetur.format.defaultFormatter.js": "vscode-typescript",
"vetur.format.defaultFormatter": {
"html": "prettier",
"css": "prettier",
"postcss": "prettier",
"scss": "prettier",
"less": "prettier",
"js": "prettier",
"ts": "prettier",
"stylus": "stylus-supremacy"
},
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"[html]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"eslint.run": "onSave",
"javascript.format.enable": false,
"editor.formatOnPaste": true,
"editor.formatOnType": true
}