首页 > 其他分享 >前端工程化工具系列(六)—— VS Code(v1.89.1):强大的代码编辑器

前端工程化工具系列(六)—— VS Code(v1.89.1):强大的代码编辑器

时间:2024-06-03 19:30:24浏览次数:13  
标签:插件 Code 格式化 v1.89 VS editor 工程化 css

VS Code(Visual Studio Code)是一款由微软开发的强大且轻量级的代码编辑器,支持多种编程语言,并提供了丰富的扩展插件生态系统。

这里主要介绍如何使用配置 ESLint、Stylelint 等插件来提升开发效率。

1 自动格式化代码

最终要达到的效果是:在对文件保存时自动格式化 Vue、JS/TS、CSS/SCSS 等文件中的代码。
很多文章都提出使用 prettier 来做代码格式化,但在使用过程中发现它有一些局限性。比如:从一个模块中导入多个接口时,用 prettier 格式化时后会一个接口放一行,相当难看(如下图),而且还没法被 ESLint 修复。
在这里插入图片描述
这个问题于 2019 年 3 月就在 github 的 issues 中被提出,5 年过去了仍没有解决,遂放弃 prettier 插件,寻找别的解决方案。后来发现 VS Code 自带的格式化工具就挺好,再与 EditorConfig 及其他插件一结合就挺完美的。

在 VS Code 中安装 ESLint、Stylelint、 Vue - Official 插件。
在项目根目录下创建 .editorconfig 文件,填入以下内容:

# top-most EditorConfig file
root = true

# All Files
[*]
charset = utf-8                  # Set default charset
indent_style = space             # 2 space indentation
indent_size = 2
end_of_line = lf                 # Unix-style
insert_final_newline = true      # newlines with a newline ending every file
trim_trailing_whitespace = true  # remove any whitespace characters preceding newline characters
max_line_length = 200            # Forces hard line wrapping after the amount of characters specified

# Markdown Files
[*.md]
insert_final_newline = false
trim_trailing_whitespace = false

在 VS Code 中打开通过快捷键 Crtl + Shift + P ,输入settings.json 打开 settings.json文件。
在这里插入图片描述
在 settings.json 中加入以下内容:

{
  // 保存时自动格式化
  "editor.formatOnSave": true,
  // 保存时自动修复错误
  "editor.codeActionsOnSave": {
    "source.fixAll": "explicit"
  },
  // 开启对 .vue 等文件的检查修复
  "eslint.validate": [
    "javascript",
    "typescript",
    "javascriptreact",
    "typescriptreact",
    "html",
    "vue",
    "markdown"
  ],
  // 开启对样式文件的检查修复
  "stylelint.validate": [
    "css",
    "less",
    "scss",
    "postcss",
    "vue",
    "html"
  ],
  // 默认使用vscode的css格式化
  "[css]": {
    "editor.defaultFormatter": "vscode.css-language-features"
  },
  "[scss]": {
    "editor.defaultFormatter": "vscode.css-language-features"
  },
  "[typescript]": {
    "editor.defaultFormatter": "vscode.typescript-language-features"
  },
  "[vue]": {
    "editor.defaultFormatter": "Vue.volar"
  }
}

标签:插件,Code,格式化,v1.89,VS,editor,工程化,css
From: https://blog.csdn.net/zapzqc/article/details/139398039

相关文章

  • Code First 入门
    CodeFirst是EntityFramework(EF)中一种流行的开发模式,它允许开发者专注于应用程序的域模型(即类),而由EF根据这些类自动生成数据库结构。以下是CodeFirst的详细入门指南:1.安装EntityFramework首先,你需要在你的项目中安装EntityFramework。这通常可以通过NuGet包管理器来完成。......
  • pytest运行报错:UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xaf in posit
    pytest运行报错:UnicodeDecodeError:‘gbk’codeccan’tdecodebyte0xafinposition82:illegalmultibytesequence  D:\Python\environment\python3.9.7\python3.exeD:/Python/nas_autotest/run.pyTraceback(mostrecentcalllast):File"D:\Python\......
  • BK7258--wifi音视频soc芯片,1080P H264 wifi低功耗保活,内置BLE,音频code,psram,flash,USB2.
    BK7258是上海博通推出的高度集成的Wi-Fi+BLE combo音视频芯片,支持UVC和DVP摄像头,该芯片集成音视频外设及接口,1080P,H.264,低功耗,内置flash,dsp,psram,驱屏,回声消除及降噪等,广泛适用于可视猫眼,门锁,门铃,ipc,内窥,儿童相机等应用市场。可视门铃应用:DVP接口支持720p25fps图像采集;MJPE......
  • 记 Codes 开源免费研发管理平台 —— 日报与工时融合集中式填报的创新实现
    继上一回合生成式全局看板的创新实现后,本篇我们来讲一讲日报与工时融合集中式填报的创新实现。  市面上所有的研发管理软件,大多都有工时相关功能,但是却没有日报功能,好像也没什么问题,但是在使用过程中体验非常不好,为什么呢?项目管理对于基层工作人员来说,主要解决这三个问题:开展......
  • LeetCode 1168. Optimize Water Distribution in a Village
    原题链接在这里:https://leetcode.com/problems/optimize-water-distribution-in-a-village/description/题目:Thereare n housesinavillage.Wewanttosupplywaterforallthehousesbybuildingwellsandlayingpipes.Foreachhouse i,wecaneitherbuildaw......
  • LeetCode 720. Longest Word in Dictionary
    原题链接在这里:https://leetcode.com/problems/longest-word-in-dictionary/description/题目:Givenanarrayofstrings words representinganEnglishDictionary,return thelongestwordin words thatcanbebuiltonecharacteratatimebyotherwordsin wor......
  • leetcode第263题:丑数
    丑数的因子只能是2,3,5。但是可能有多个2,多个3,多个5.因此需要循环地除以2、3、5.publicclassSolution{publicboolIsUgly(intn){if(n<=0){returnfalse;}int[]factors={2,3,5};for(inti=0;i......
  • leetcode第1281题: 整数的各位积和之差
    publicclassSolution{publicintSubtractProductAndSum(intn){intsum=0;intji=1;while(n>0){intnum=n%10;sum+=num;ji*=num;n/=10;}returnji-sum;......
  • vscode 报错:应输入表达式
    如图,报错:应输入表达式,但是我的==看起来是正确的。后面发现原来是我的定义有问题,这里定义处多了分号“;”,然后vscode检测不到该定义,故会报错。......
  • LeetCode 1151. 最少交换次数来组合所有的 1
    1151.最少交换次数来组合所有的1给出一个二进制数组 data,你需要通过交换位置,将数组中 任何位置 上的1组合到一起,并返回所有可能中所需 最少的交换次数。示例1:输入:data=[1,0,1,0,1]输出:1解释:有三种可能的方法可以把所有的1组合在一起:[1,1,1,0,0],交换......