Synchronous version of Prettier
yarn add prettier @prettier/sync
import synchronizedPrettier from "@prettier/sync";
synchronizedPrettier.format("foo( )", { parser: "babel" });
// => 'foo();\n'
This package is a simple wrapper of make-synchronized
.
For more complex use cases, it's better to use make-synchronized
directly:
Example:
import * as fs from "node:fs/promises";
import * as prettier from "prettier";
import makeSynchronized from "make-synchronized";
export default makeSynchronized(import.meta, async function formatFile(file) {
const config = await prettier.resolveConfig(file);
const content = await fs.readFile(file, "utf8");
const formatted = await prettier.format(content, {
...config,
filepath: file,
});
await fs.writeFile(file, formatted);
});
Alternatively you can also use synckit
or make-synchronous
.
Type: object
Type: string | URL
Path or URL to prettier entry.
import { createSynchronizedPrettier } from "@prettier/sync";
const synchronizedPrettier = createSynchronizedPrettier({
prettierEntry: "/path/to/prettier/index.js",
});
synchronizedPrettier.format("foo( )", { parser: "babel" });
// => 'foo();\n'