Skip to content

Commit

Permalink
Add example for complex use case
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 21, 2024
1 parent acd83b5 commit 1fe76d9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const {makeSynchronizedModule} = require('make-synchronized')
const { makeSynchronizedModule } = require("make-synchronized");

function createSynchronizedPrettier({ prettierEntry }) {
return makeSynchronizedModule(prettierEntry);
Expand Down
25 changes: 25 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@ synchronizedPrettier.format("foo( )", { parser: "babel" });
// => 'foo();\n'
```

This package is a simple wrapper of [`make-synchronized`](https://github.com/fisker/make-synchronized), currently only the functions and primitive values exported from `prettier` is functional, functions not exported directly (eg: `prettier.__debug.parse`) doesn't work, but it can be supported, if you want more functionality, please [open an issue](https://github.com/prettier/prettier-synchronized/issues/new).

For more complex use cases, it more reasonable to extract into a separate file, and run with [`make-synchronized`](https://github.com/fisker/make-synchronized), example

```js
// runs-in-worker.js
import * as fs from "node:fs/promises";
import * as prettier from "prettier";

export async function formatFile(file) {
const config = await prettier.resolveConfig(filepath);
const content = await fs.readFile(file, "utf8");
const formatted = await prettier.format(content, config);
await fs.writeFile(file, formatted);
}
```

```js
import makeSynchronized from "make-synchronized";

const {
formatFile, // Synchronize version of `formatFile` in `runs-in-worker.js`
} = makeSynchronized(new URL("./runs-in-worker.js", import.meta.url));
```
### `createSynchronizedPrettier(options)`
#### `options`
Expand Down

0 comments on commit 1fe76d9

Please sign in to comment.