vscode插件及用户配置

2,255 阅读7分钟

资料

Visual Studio Code

vscode配置 setting.json

{
    "workbench.iconTheme": "vscode-icons",
    "terminal.integrated.confirmOnExit": true,
    "terminal.integrated.copyOnSelection": true,
    "terminal.integrated.cursorBlinking": true,
    "terminal.integrated.cursorStyle": "underline",
    "explorer.confirmDelete": false,
    "explorer.confirmDragAndDrop": false,
    "window.zoomLevel": 0,
    "git.autofetch": true,
    "git.enableSmartCommit": true,
    "git.ignoreMissingGitWarning": true,
    "eslint.validate": [
        "javascript",
        "javascriptreact",
        "html",
        "vue",
        {
            "language": "html",
            "autoFix": true
        },
        {
            "language": "vue",
            "autoFix": true
        }
    ],
    "editor.quickSuggestions": {
        "other": true,
        "comments": true,
        "strings": true
    },
    "editor.codeActionsOnSave": {
        // For ESLint
        "source.fixAll.eslint": true,
        // For TSLint
        "source.fixAll.tslint": true,
        // For Stylelint
        "source.fixAll.stylelint": true
    },
    "editor.renderControlCharacters": true,
    "editor.maxTokenizationLineLength": 200000,
    "eslint.packageManager": "yarn",
    // vscode默认启用了根据文件类型自动设置tabsize的选项
    "editor.detectIndentation": false,
    // 重新设定tabsize
    "editor.tabSize": 4,
    "editor.tabCompletion": "onlySnippets",
    // #每次保存的时候自动格式化
    "editor.formatOnSave": true,
    // 使能每一种语言默认格式化规则
    "[html]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "[css]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "[less]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "[javascript]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "[vue]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "[json]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "prettier.printWidth": 200, // 换行字符串阈值
    "prettier.endOfLine": "auto", // 结尾是 \n \r \n\r auto
    "prettier.tabWidth": 4, // 缩进字节数
    "prettier.useTabs": false, // 缩进不使用tab,使用空格
    "prettier.semi": true, // 句末加分号
    "prettier.singleQuote": true, // 用单引号
    "prettier.proseWrap": "preserve", // 默认值。因为使用了一些折行敏感型的渲染器(如GitHub comment)而按照markdown文本样式进行折行
    "prettier.arrowParens": "avoid", //  (x) => {} 箭头函数参数只有一个时是否要有小括号。avoid:省略括号
    // "prettier.bracketSpacing": true, // 在对象,数组括号与文字之间加空格 "{ foo: bar }"
    // "prettier.disableLanguages": ["vue"], // 不格式化vue文件,vue文件的格式化单独设置
    // "prettier.eslintIntegration": false, //不让prettier使用eslint的代码格式进行校验
    // "prettier.htmlWhitespaceSensitivity": "ignore",
    // "prettier.ignorePath": ".prettierignore", // 不使用prettier格式化的文件填写在项目的.prettierignore文件中
    "prettier.jsxBracketSameLine": false, // 在jsx中把'>' 是否单独放一行
    "prettier.jsxSingleQuote": false, // 在jsx中使用单引号代替双引号
    // "prettier.requireConfig": false, // Require a 'prettierconfig' to format prettier
    "prettier.trailingComma": "none", // 最后一个对象元素加逗号
    // "prettier.trailingComma": "es5", // 在对象或数组最后一个元素后面是否加逗号(在ES5中加尾逗号)
    // "prettier.tslintIntegration": false // 不让prettier使用tslint的代码格式进行校验
    //  #让函数(名)和后面的括号之间加个空格
    "javascript.format.insertSpaceBeforeFunctionParenthesis": true,
    "javascript.updateImportsOnFileMove.enabled": "never",
    "typescript.updateImportsOnFileMove.enabled": "never",
    "javascript.implicitProjectConfig.experimentalDecorators": true,
    "sync.gist": "25f4df7a009f4a8f8108521717150a19",
    "sync.quietSync": false,
    "sync.removeExtensions": true,
    "sync.syncExtensions": true,
    "sync.autoDownload": false,
    "sync.autoUpload": false,
    "sync.forceDownload": false
}

插件

网友版

{
    "workbench.settings.editor": "ui",

    // 文本编辑器
    "editor.suggestSelection": "first",
    "editor.renderControlCharacters": true,
    "editor.multiCursorModifier": "ctrlCmd",
    "editor.snippetSuggestions": "top",
    "editor.renderWhitespace": "all",
    "editor.formatOnPaste": true,
    "editor.wordWrap": "on",
    "editor.fontSize": 12,
    "editor.tabSize": 2,
    "editor.codeActionsOnSave": {
        // For ESLint
        "source.fixAll.eslint": true,
        // For TSLint
        "source.fixAll.tslint": true,
        // For Stylelint
        "source.fixAll.stylelint": true
    },
    "diffEditor.ignoreTrimWhitespace": true,

    // 窗口
    "window.zoomLevel": 0,

    // 文件资源管理
    "explorer.confirmDragAndDrop": false,
    "explorer.confirmDelete": false,

    // 终端
    "terminal.integrated.cursorBlinking": true,
    "terminal.integrated.rendererType": "dom",
    "terminal.integrated.cursorStyle": "line",

    // 文件
    "files.insertFinalNewline": true,
    "files.eol": "\n",
    "files.associations": {
        "*.vue": "vue",
        "*.wxss": "css",
        "*.cjson": "jsonc",
        "*.wxs": "javascript",
        "*.js": "javascript",
        "*.wxml": "html"
    },

    // 语言相关配置
    "html.format.extraLiners": "",
    "javascript.referencesCodeLens.enabled": true,

    // 针对某种语言,配置替代编辑器设置。
    // 以下配置大多是设置默认格式化程序
    "[html]": {
        "editor.defaultFormatter": "vscode.html-language-features"
    },
    "[vue]": {
        "editor.defaultFormatter": "octref.vetur"
    },
    "[css]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "[less]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "[scss]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "[javascript]": {
        "editor.defaultFormatter": "vscode.typescript-language-features"
    },
    "[javascriptreact]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "[typescript]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "[typescriptreact]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "[json]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "[markdown]": {
        "editor.defaultFormatter": "yzhang.markdown-all-in-one"
    },

    // ============= plugin config begin =============
    // sublime 按 tab 自动补全 tag
    "auto-close-tag.SublimeText3Mode": true,

    // emmet
    "emmet.triggerExpansionOnTab": true,
    "emmet.includeLanguages": {
        "vue-html": "html",
        "wxml": "html",
        "javascript": "javascriptreact"
    },
    "emmet.syntaxProfiles": {
        "vue-html": "html",
    },

    // 代码格式
    "prettier.singleQuote": true,
    "prettier.requireConfig": true,

    // sync githubg 同步模块
    "sync.gist": "Your GitHub Gist ID",
    "sync.autoDownload": false,
    "sync.autoUpload": true,
    "sync.forceDownload": false,
    "sync.removeExtensions": true,
    "sync.syncExtensions": true,
    "sync.forceUpload": true,

    // gitlens, git 可视化功能增强
    "gitlens.advanced.messages": {
        "suppressCommitHasNoPreviousCommitWarning": false,
        "suppressCommitNotFoundWarning": false,
        "suppressFileNotUnderSourceControlWarning": false,
        "suppressGitVersionWarning": false,
        "suppressLineUncommittedWarning": false,
        "suppressNoRepositoryWarning": false,
        "suppressResultsExplorerNotice": false,
        "suppressShowKeyBindingsNotice": true
    },
    "gitlens.views.fileHistory.enabled": true,
    "gitlens.views.lineHistory.enabled": true,

    // markdown
    "markdownlint.config": {
        "MD033": {
            "allowed_elements": [
                "iframe",
                "details",
                "summary",
                "div",
                "hr",
                "br",
                "a",
            ]
        }
    },
    "workbench.colorTheme": "One Monokai",
    "minapp-vscode.disableAutoConfig": true,
}

编辑器扩展

  • Open-In-Browser 由于VSCode没有提供直接在浏览器中打开文件的内置界面,所以此插件在快捷菜单中添加了在默认浏览器查看文件选项. 在 windows 下, 可以使用快捷键Alt + b快速的打开.
  • Minify 这是一款用于压缩合并JavaScript和CSS文件的应用程序。它提供了大量自定义的设置,以及自动压缩保存并导出为.min文件的选项。它能够分别通过uglify-js、clean-css和 html-minifier,与JavaScript、CSS和HTML协同工作。是一个可以不通过脚手架编译就进行压缩的办法,但平时工程化项目还是不能单单依赖这种方式。
  • TODO Highlight 开发中一些没有做完的事,一般会习惯性的打上TODO备注,但再要找的时候就很难显目的找到了。TODO Highlight顾名思义就是让TODO高亮的插件。

前端类增强

  • Auto Rename Tag 自动重命名配对的HTML / XML标记, 这在修改标签名上一点挺便捷的.
  • Auto Close Tag 这个扩展是为 XML,PHP,Vue,JavaScript,TypeScript,JSX,TSX 等其他语言启用的。同时它是可配置的。输入标签的右括号后,结束标签将被自动插入。
  • CSS Peek 使用此插件,你可以追踪至样式表中CSS类和ids定义的地方。当你在 HTML 文件中右键单击选择器时,选择“ Go to Definition 和 Peek definition ”选项,它便会给你发送样式设置的 CSS 代码。
  • JavaScript (ES6) code snippets es6已经是经常运用在我们的项目中了, 该扩展包含用于 Vscode 编辑器的 ES6 语法的 JavaScript 代码片段.(同时支持 JavaScript 和 TypeScript)。
  • Vetur vue开发必备, 可以格式化.vue文件, 由官方维护.
  • Vue 2 Snippets vue开发必备, 这个插件基于最新的Vue官方语法高亮文件添加了 Syntax Highlight, 并且依据Vue 2的 API 添加了Code Snippets`.
  • WakaTime 统计开发时间和效率,只需注册一个账号即可~

代码规范化

  • ESLint 要养成一个良好的编程习惯, ESLint就是一个不错的选择. 它能适应项目中的规则来纠正你的不好的习惯。
  • Prettier Prettier 是目前 Web 开发中最受欢迎的代码格式化程序。安装了这个插件,它就能够自动应用 Prettier,并将整个 JS 和 CSS 文档快速格式化为统一的代码样式。
  • EditorConfig Editor Config可以让团队开发时统一约定好一种规范. 这个主流的编辑器都支持.
  • [markdown] 如果经常使用 Markdown 的话,还可以使用安装markdownlint和Markdown All in One这两个插件,前者可以约束编写的格式,后者提供方便的键盘快捷方式,目录,自动预览等。

网友版2

代码质量控制实现方式

  • eslint 检查代码质量
  • prer html 检查代码风格
  • git hook 强制执行编码风格检测和修正

工具检查、自动校正与优化

"scripts": {
"format": "prettier --write \"src/业务模块/**/*.js\" \"src/业务模块/**/*.vue\"",
"precommit": "lint-staged",
"lint": "eslint --ext .js,.vue src/业务模块/xxx目录/",
"lint-fix": "eslint --ext .js,.vue  src/业务模块/ --fix",
}
// 强制校验规范
lint-stage:{
    "src/业务模块/**/*.{js,json,css,vue}":{
        "prettier --write",
        "eslint --fix",
        "git add"
    }
}

编辑器配置

idea|webstorm 配置eslint

  • 全局安装eslint插件
npm install eslint -g
  • 查看npm 全局安装路径
npm config ls

  • eslint插件支持设置 备注:eslint 建议放开,如果禁止的话,代码提交时会对提交的代码重新进行eslint规范检查

  • 使用自动修复功能

  • 配置别名跳转

    webstrom:设置—Languages & Farmeworks–webpack 配置webpack configuration file 到 项目目录build下webpack.base.config.js 就可以了

  • vscode 配置jsconfig.json文件:
{
    "compilerOptions": {
        "baseUrl": ".",
        "paths": {
            "@/*": ["src/*"]
        }
    },
    "exclude": ["node_modules", "dist"]
}

vscode 配置eslint

  • 安装插件

    • eslint
    • prettier
    • vetur
  • 用户设置

{
  // vscode默认启用了根据文件类型自动设置tabsize的选项
  "editor.detectIndentation": false,
  // 重新设定tabsize
  "editor.tabSize": 2,
  // #每次保存的时候自动格式化
  "editor.formatOnSave": true,
  // #每次保存的时候将代码按eslint格式进行修复
  "eslint.autoFixOnSave": true,
  // 添加 vue 支持
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    {
      "language": "vue",
      "autoFix": true
    }
  ],
  // #让prettier使用eslint的代码格式进行校验
  "prettier.eslintIntegration": true,
  // #去掉代码结尾的分号
  "prettier.semi": false,
  // #使用带引号替代双引号
  "prettier.singleQuote": true,
  // #让函数(名)和后面的括号之间加个空格
  "javascript.format.insertSpaceBeforeFunctionParenthesis": true,
  // #这个按用户自身习惯选择
  "vetur.format.defaultFormatter.html": "js-beautify-html",
  // #让vue中的js按编辑器自带的ts格式进行格式化
  "vetur.format.defaultFormatter.js": "vscode-typescript",
  "vetur.format.defaultFormatterOptions": {
    "js-beautify-html": {
      "wrap_attributes": "force-aligned"
      // #vue组件中html代码格式化样式
    }
  },
  // 格式化stylus, 需安装Manta's Stylus Supremacy插件
  "stylusSupremacy.insertColons": false, // 是否插入冒号
  "stylusSupremacy.insertSemicolons": false, // 是否插入分好
  "stylusSupremacy.insertBraces": false, // 是否插入大括号
  "stylusSupremacy.insertNewLineAroundImports": false, // import之后是否换行
  "stylusSupremacy.insertNewLineAroundBlocks": false // 两个选择器中是否换行
}

eslint-loader 配置

webpack 主要配置点:

{
  enforce: "pre",
  test: /\.(vue|js|jsx)/,
  exclude: /node_modules/,
  loader: "eslint-loader",
  include: [ path.resolve(__dirname, '../src/业务模块/')],
  options: {
    formatter: require("eslint-friendly-formatter"),
  }
},

eslint 常见问题

  1. Mixed spaces and tabs
// 禁止使用 空格 和 tab 混合缩进;
// 大多数代码约定要求使用空格或 tab 进行缩进。因此,一行代码同时混有 tab 缩进和空格缩进
  1. Expected error to be handled.
// try catch 没有对error 处理
console.log(error)
  1. This 'v-if' should be moved to the wrapper element.(todo)
// 当 v-if 与 v-for 一起使用时,v-for 具有比 v-if 更高的优先级,这意味着 v-if 将分别重复运行于每个 v-for 循环中 不推荐v-if和v-for同时使用
console.log(error)
  1. Type of the default value for 'btns' prop must be a function.
// vue 组件语法,用function包裹  
btns: {
  type: Array,
  default: () => []
}
  1. Return statement should not contain assignment.
// 使用括号括起来
return style.display = newVal ? 'block' : 'none'
  1. Elements in iteration expect to have 'v-bind:key' directives.
// v-for 增加key
<div class="rule-item" v-for="(item,index) of handleData" :key="index">

error Parsing error: Unexpected token import

Do not access Object.prototype method 'hasOwnProperty' from target object.

github.com/vuejs/eslin…