无法使用 Prettier 为我的摩纳哥代码编辑器进行格式化

问题描述 投票:0回答:1

我正在使用摩纳哥代码编辑器在 React 中制作一个代码编辑器 我正在尝试使用 prettier 在代码编辑器中格式化我的代码。 它给出错误' TypeError:无法读取未定义的属性(读取'语言') 在独立.mjs:22:694'

这是代码

import parser from "prettier/parser-babel";

try {
    const test = async () => {
      const code = `function add(a, b) {
              return a + b;
            }`;

      const prettier = await import("prettier/standalone");

      const formattedCode = await prettier.format(code, {
        semi: true,
        parser: "babel",
        plugins: [parser],
        useTabs: false,
        
      });
      console.log({ formattedCode });
    };
    test();
  } catch (e) {
    console.log({ e });
  }

我也尝试使用不同的解析器,但它不起作用。

reactjs prettier monaco-editor
1个回答
0
投票

尝试使用这些导入:

import babelPlugin from "prettier/plugins/babel";
import estreePlugin from "prettier/plugins/estree";

我还导入了

estree
插件,因为你需要它来解析 JavaScript :-)

© www.soinside.com 2019 - 2024. All rights reserved.