prettier.format 报错ConfigError: C

2024-10-14  本文已影响0人  齐格Insight

背景

在我们的星舰项目里,我们需要使用prettier对代码进行格式化。

安装 prettier

pnpm install prettier

prettier 版本为:3.3.3

项目中使用

使用如下:

import * as prettier from "prettier";
prettier.format(codeContent, {
        parser: 'vue',
        semi: false,
        singleQuote: true
    })

注意 codeContent 为传入的 vue 文件内容。

但启动的时候报错:

prettier.js?v=3b3c63fc:1927 Uncaught (in promise) ConfigError: Couldn't resolve parser "vue". Plugins must be explicitly added to the standalone bundle.
image.png

问题解决

这个问题在官方的 issue 里已经有人提过
https://github.com/prettier/prettier/issues/11010

正确的用法如下:

import * as prettier from "prettier";
import parserHtml from "prettier/plugins/html";
import parserBabel from "prettier/plugins/babel";
import parserPostcss from "prettier/plugins/postcss";

prettier.format(codeContent, {
        parser: 'vue',
        semi: false,
        singleQuote: true,
        plugins: [parserHtml, parserBabel, parserPostcss] 
    })
上一篇 下一篇

猜你喜欢

热点阅读