Support ESM plugins (#13201)

This commit is contained in:
fisker Cheung authored and GitHub committed 2022-08-06 10:42:27 +08:00
1 parent e096ea7552
commit 5aadab6f28
23 files changed
+151 -129

No files matched your search

+6 -4
View File
@@ -24,7 +24,7 @@ When plugins cannot be found automatically, you can load them with:
- The [API](api.md), via the `pluginSearchDirs` and `plugins` options:
```js
prettier.format("code", {
await prettier.format("code", {
parser: "foo",
pluginSearchDirs: ["./dir-with-plugins"],
plugins: ["prettier-plugin-foo"],
@@ -74,7 +74,7 @@ To turn off plugin autoloading, use `--no-plugin-search` when using Prettier CLI
## Developing Plugins
Prettier plugins are regular JavaScript modules with five exports:
Prettier plugins are regular JavaScript modules with the following five exports or default export with the following properties:
- `languages`
- `parsers`
@@ -178,7 +178,9 @@ export const printers = {
Prettier uses an intermediate representation, called a Doc, which Prettier then turns into a string (based on options like `printWidth`). A _printer_'s job is to take the AST generated by `parsers[<parser name>].parse` and return a Doc. A Doc is constructed using [builder commands](https://github.com/prettier/prettier/blob/main/commands.md):
```js
const { join, line, ifBreak, group } = require("prettier").doc.builders;
import { doc } from "prettier";
const { join, line, ifBreak, group } = doc.builders;
```
The printing process works as follows:
@@ -367,7 +369,7 @@ At the time of dispatching, Prettier will have annotated each AST comment node (
The `util.addTrailingComment`/`addLeadingComment`/`addDanglingComment` functions can be used to manually attach a comment to an AST node. An example `ownLine` function that ensures a comment does not follow a "punctuation" node (made up for demonstration purposes) might look like:
```js
const { util } = require("prettier");
import { util } from "prettier";
function ownLine(comment, text, options, ast, isLastComment) {
const { precedingNode } = comment;