eslint9.0之后如何安装新版本eslint+prettier规范

eslint9.0之后如何安装新版本eslint+prettier规范

之前写过一篇eslint9.0之后如何安装旧版本eslint+prettier规范,中间一直想更新关于新版本的eslint+prettier配置方法。但是eslint9.0之后,官方工具@eslint/config@latest很长一段时间都没有稳定下来,没有特别简单和稳定的配置方法,另外也在等oxlint,所以就拖到了现在。最近准备开一个新坑,所以就记录一下。

在此之前,特别说明目前使用的eslint版本是9.30.1,使用的是官方提供的配置工具@eslint/create-config@1.9.0。后续如果有更新,配置方法有不同可以评论区留言。

配置eslint

还是一样的步骤,执行命令:

bash 复制代码
npx eslint --init

按照步骤选择需要的配置项,如下:

bash 复制代码
D:\code\lint-demo>npx eslint --init
You can also run this command directly using 'npm init @eslint/config@latest'.

> front-end@0.0.0 npx
> create-config

@eslint/create-config: v1.9.0

√ What do you want to lint? · javascript
√ How would you like to use ESLint? · problems    
√ What type of modules does your project use? · esm
√ Which framework does your project use? · vue
√ Does your project use TypeScript? · no
√ Where does your code run? · browser
The config that you've selected requires the following dependencies:

eslint, @eslint/js, globals, eslint-plugin-vue
√ Would you like to install them now? · Yes
√ Which package manager do you want to use? · npm
☕️Installing...

这时根目录会生成eslint.config.js文件,为什么一直拖着没更新新版本的配置方法也是这个文件,总在变。

js 复制代码
// eslint.config.js
import js from "@eslint/js";
import globals from "globals";
import pluginVue from "eslint-plugin-vue";
import { defineConfig } from "eslint/config";


export default defineConfig([
  { files: ["**/*.{js,mjs,cjs,vue}"], plugins: { js }, extends: ["js/recommended"] },
  { files: ["**/*.{js,mjs,cjs,vue}"], languageOptions: { globals: globals.browser } },
  pluginVue.configs["flat/essential"],
]);

配置prettier

我一直希望是开箱即用或者是简单配置就能使用eslint+prettier,但是还是需要手动去安装其他插件: prettier(插件本体)、eslint-plugin-prettier(prettier集成到eslint)、eslint-config-prettier(冲突时,prettier覆盖eslint)。

bash 复制代码
npm i prettier eslint-plugin-prettier eslint-config-prettier -D

然后手动写入prettier配置文件

json 复制代码
// .prettierrc
// 这是我平时习惯的一些配置项
// printWidth: 每行最大宽度为 160
// semi: 不使用分号
// trailingComma: 不添加末尾逗号
// bracketSpacing: 对象括号之间留有空格
// singleQuote: 使用单引号进行字符串包裹
{
  "printWidth": 160,
  "semi": false,
  "trailingComma": "none",
  "bracketSpacing": true,
  "singleQuote": true
}

配置集成

最后一步,让eslint+prettier集成并生效。目前的步骤是非常简单,只需要两行代码:

js 复制代码
import js from '@eslint/js'
import globals from 'globals'
import pluginVue from 'eslint-plugin-vue'
import { defineConfig } from 'eslint/config'

import eslintPluginPrettier from 'eslint-plugin-prettier/recommended' // 导入 Prettier 和 ESLint 集成插件

export default defineConfig([
  { files: ['**/*.{js,mjs,cjs,vue}'], plugins: { js }, extends: ['js/recommended'] },
  { files: ['**/*.{js,mjs,cjs,vue}'], languageOptions: { globals: globals.browser } },
  pluginVue.configs['flat/essential'],
  eslintPluginPrettier // 启用 Prettier 作为 ESLint 的规则执行代码格式化
])

总结,全过程步骤总共三个:

  1. 命令形式配置基础eslint
  2. 执行命令,手动安装 prettiereslint-plugin-prettiereslint-config-prettier,并创建.prettierrc规则文件。
  3. eslint.config.js新增两行代码,引入和启用prettier集成config插件

如果过程中有某些地方不一样,欢迎评论区留言。如果不生效,有以下处理方法:

  1. 参考eslint9.0之后如何安装旧版本eslint+prettier规范
  2. 使用脚手架自带的eslint+prettier配置。
  3. 等待oxlint
相关推荐
源图客3 分钟前
Claude Code基础使用
服务器·前端·数据库
asdzx6719 分钟前
在 React 中轻松实现 PDF 转 Word:纯前端 WASM 方案实战
前端·react.js·pdf
daols8826 分钟前
vue 甘特图 vxe-gantt 实现双进度条:计划 vs 实际,支持多级子任务拆分
vue.js·甘特图·vxe-table·vxe-gantt
爱勇宝9 小时前
第 1 章:别把“需求文档”当成真正的需求
前端·后端·程序员
梨子同志9 小时前
常用命令
前端
梨子同志10 小时前
React 状态管理
前端
Dxy123931021610 小时前
MySQL索引完整教程:创建、查看、修改、删除与日常管理
前端·javascript·mysql
剪刀石头布啊11 小时前
js能被遍历的集合有哪些特征,为何能被遍历
前端
剪刀石头布啊11 小时前
js解构
前端
剪刀石头布啊11 小时前
防抖功能的逐步递进
前端