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,选择当前项目文件夹

相关推荐
lbh14 分钟前
当我开始像写代码一样和AI对话,一切都变了
前端·openai·ai编程
We་ct1 小时前
LeetCode 918. 环形子数组的最大和:两种解法详解
前端·数据结构·算法·leetcode·typescript·动态规划·取反
wefly20172 小时前
m3u8live.cn 在线M3U8播放器,免安装高效验流排错
前端·后端·python·音视频·前端开发工具
C澒2 小时前
微前端容器标准化 —— 公共能力篇:通用打印
前端·架构
德育处主任Pro2 小时前
前端元素转图片,dom-to-image-more入门教程
前端·javascript·vue.js
木斯佳2 小时前
前端八股文面经大全:小红书前端一二面OC(下)·(2026-03-17)·面经深度解析
前端·vue3·proxy·八股·响应式
陈天伟教授3 小时前
人工智能应用- 预测新冠病毒传染性:04. 中国:强力措施遏制疫情
前端·人工智能·安全·xss·csrf
zayzy3 小时前
前端八股总结
开发语言·前端·javascript
今天减肥吗3 小时前
前端面试题
开发语言·前端·javascript
Rabbit_QL3 小时前
【前端UI行话】前端 UI 术语速查表
前端·ui·状态模式