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

相关推荐
data analyse 4564 分钟前
服务端访问日志聚合 PV 与 UV 怎么实现?日志采集与口径设计
大数据·前端·数据分析
JavaGuide9 分钟前
DeepSeek Harness 重磅更新,Codex、Claude Code 子智能体进官方插件列表了,还有 Git 工作树和思考翻译!
前端·后端
小羊没烦恼!25 分钟前
OS X 上使用.NET开发应用程序
开发语言·前端·数据库·人工智能·c#·策略模式
Eiceblue1 小时前
使用 Python 实现 Excel 行列转置(保留单元格样式)
前端·python·excel
西西学代码1 小时前
像素和颜色之间的转换(Flutter)
前端·javascript·flutter
茶底世界之下2 小时前
羽毛颜色已经很鲜艳,还要继续加色吗?用 Awaking 试一次减饱和
前端·ios·运营
风早爽太2 小时前
Mac 搭建 Vue 开发环境
前端·vue.js
Lank_M2 小时前
Word图片裁剪转HTML失效,转换器各漏读了哪一层
前端
青烟小生2 小时前
前端架构设计实战:从组件思维到系统思维
前端
Amos_Web2 小时前
Rspack 源码解析(二十):Typed Hook 系统如何靠宏生成
前端·rust·前端框架