使用prettier插件时,报错:Error formatting code: ConfigError: Couldn‘t find plugin for AST format “estree“.

有任何问题,欢迎站内私信博主交流。


正文开始

  • 一、问题描述
  • 二、原因
  • 三、解决方案
  • 总结


一、问题描述

纯前端使用prettier插件时,报错:Error formatting code: ConfigError: Couldn’t find plugin for AST format “estree”. Plugins must be explicitly added to the standalone bundle.

意思是找不到estree插件。

二、原因

注意是纯前端,nodejs、electron还没有尝试,不作为参考。

很有可能是你在纯前端代码中,使用import prettier from "prettier"引入插件,这样插件会去找默认解析器,博主介绍prettier和monaco使用的文章中提到过,前端使用插件,没有默认解析器。

如果直接引入prettier,不管如何设置parser属性,都无效,都会去找estree插件,所以会报找不到对应语言插件的错误。

三、解决方案

引入插件使用standalone或者prettier都可以,即:

import prettier from 'prettier/standalone'
或者
import prettier from 'prettier'

然后引入需要的插件:

import prettierPluginBabel from 'prettier/plugins/babel'
import prettierPluginVue from 'prettier/plugins/html'

在prettier中定义插件:

 const formatted = await prettier.format(jsonSource.value, {
      parser: 'json',
      plugins: [prettierPluginBabel, prettierPluginVue],// 根据需要添加
      semi: true,
      trailingComma: 'all',
      singleQuote: true,
      printWidth: 80,
      tabWidth: 2,
    })

总结

获取资源,可以通过私信博主来获取。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

中二少年学编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值