VSCODE前端代码风格格式化

前端代码风格格式化设置

npm安装见Node.js安装及环境配置超详细教程【Windows系统】_windows 安装nodejs-CSDN博客


总共三部分

  1. npm/pnpm安装prettier @spotify/prettier-config
  2. prettier(格式规范导入,前端代码格式化,其余代码忽略prettier格式化)
  3. vscode插件Prettier安装,并把Prettier配置导入vscode项目

具体如下7步------其中1-3对每个项目来说可能都要重新配置


  1. 查找三方规范:npm search prettier-config 或者pnpm

    可以采用@spotify/prettier-config,spotify------大厂维护、实战验证、更新活跃

    复制代码
    @spotify/prettier-config
    Spotify's base Prettier config
    Version 15.0.0 published 2023-05-18 by spotify-eng
    Maintainers: spotify-eng
    https://npm.im/@spotify/prettier-config
  2. 安装: pnpm install --save-dev prettier @spotify/prettier-config

    注意每个项目都需要安装

  3. 创建 .prettierrc.js

    javascript 复制代码
    module.exports = require('@spotify/prettier-config');
  4. VSCODE工作区.vscode/settings.json设置,按照设置保存即可格式化:

    json 复制代码
    {
      "[html]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode",
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
          "source.fixAll": "explicit"
        }
      },
      "[css]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode",
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
          "source.fixAll": "explicit"
        }
      },
      "[javascript]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode",
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
          "source.fixAll": "explicit"
        }
      },
      "prettier.requireConfig": true,
      "prettier.prettierPath": "./node_modules/prettier"
    }
  5. 创建或更新 .prettierignore:

    复制代码
    # 构建输出
    node_modules/
    dist/
    build/
    
    # 配置文件  
    package-lock.json
    yarn.lock
    pnpm-lock.yaml
    
    # 文档文件
    *.md
    *.txt
    
    # 其他语言文件
    *.py
    *.java
    *.cpp
    *.c
    *.go
    *.rs
    *.xml
    *.yaml
    *.yml
    Dockerfile
    Makefile
    
    # 特定目录
    backend/
    scripts/
  6. VSCODE安装Prettier - Code formatter插件

  7. VSCODEPrettier配置选择:Ctrl + Shift + P 输入Prettier: Change Configuration,选择当前项目文件夹

相关推荐
PedroQue992 小时前
0.8.2 新增 backFallback 配置,无 tabbar 页返回无忧
前端·uni-app
淡漠的蓝精灵2 小时前
web.py:一个简单到极致的 Python Web 框架
开发语言·前端·python·其他
开开心心就好2 小时前
竖式除法出题工具,整除余数混合模式
运维·服务器·前端·javascript·ocr·perl·1024程序员节
起个名字费劲死了3 小时前
Halcon学习一:骰子识别
前端·python·学习
大江东去浪淘尽千古风流人物3 小时前
【HexVIO】DSP卸载双目惯性VIO:Hexagon向量化前端与单帧融合调度深度解析
前端·状态模式·slam·dsp·vio·hexagon·移动端加速
其实防守也摸鱼3 小时前
Chrome 扩展安全攻防实录:从攻击面到防御实践
开发语言·前端·网络·chrome·安全·web安全·php
web打印社区3 小时前
Lodop排障总纲-常见报错一张表
开发语言·前端·javascript·chrome·websocket
李剑一3 小时前
为什么几乎所有的知名开源项目都是外国人写的?
前端·ai编程·deepseek
小郎君6583 小时前
(一)为什么表单需要第三种状态:disabled、readonly 与纯文本展示
前端·vue.js
计算机魔术师3 小时前
1.4万亿没人接,OpenAI自己砍了1500亿
前端