Files
test-repo/docs/browser.md
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

121 lines
4.0 KiB
Markdown
Raw Normal View History

2018-05-27 15:17:38 -03:00
---
id: browser
title: Browser
---
2020-11-05 06:07:29 +02:00
Run Prettier in the browser using its **standalone** version. This version only formats the code and has no support for config files, ignore files, CLI usage, or automatic loading of plugins.
The standalone version comes as:
- ES modules: `esm/standalone.mjs`, starting in version 2.2
- UMD: `standalone.js`, starting in version 1.13
2018-05-27 15:17:38 -03:00
The [`browser` field](https://github.com/defunctzombie/package-browser-field-spec) in Prettier’s `package.json` points to `standalone.js`. That’s why you can just `import` or `require` the `prettier` module to access Prettier’s API, and your code can stay compatible with both Node and the browser as long as webpack or another bundler that supports the `browser` field is used. This is especially convenient for [plugins](plugins.md).
2018-05-27 15:17:38 -03:00
### `prettier.format(code, options)`
2020-12-13 05:10:59 +08:00
Unlike the `format` function from the [main API](api.md#prettierformatsource--options), this function does not load plugins automatically, so a `plugins` property is required if you want to load [plugins](#plugins). Additionally, the parsers included in the Prettier package won’t be loaded automatically, so you need to load them as plugins before using them.
2018-05-27 15:17:38 -03:00
See [Usage](#usage) below for examples.
2020-12-13 05:10:59 +08:00
## Plugins
All available plugins are files named `parser-*.js` in <https://unpkg.com/browse/prettier@2.2.1/> and `parser-*.mjs` in <https://unpkg.com/browse/prettier@2.2.1/esm/>.
If you want format embed code, you need load related plugins too, for example
```html
<script type="module">
import prettier from "https://unpkg.com/prettier@2.2.1/esm/standalone.mjs";
import parserBabel from "https://unpkg.com/prettier@2.2.1/esm/parser-babel.mjs";
prettier.format("const html = /* HTML */ `<DIV> </DIV>`", {
parser: "babel",
plugins: [parserBabel],
});
</script>
```
the HTML code inside JavaScript code won't get formatted, because it requires `html` parser too, correct usage
```html
<script type="module">
import prettier from "https://unpkg.com/prettier@2.2.1/esm/standalone.mjs";
import parserBabel from "https://unpkg.com/prettier@2.2.1/esm/parser-babel.mjs";
import parserHtml from "https://unpkg.com/prettier@2.2.1/esm/parser-html.mjs";
prettier.format("const html = /* HTML */ `<DIV> </DIV>`", {
parser: "babel",
plugins: [parserBabel, parserHtml],
});
</script>
```
2018-05-27 15:17:38 -03:00
## Usage
### Global
```html
2020-11-28 09:21:04 +09:00
<script src="https://unpkg.com/prettier@2.2.1/standalone.js"></script>
<script src="https://unpkg.com/prettier@2.2.1/parser-graphql.js"></script>
2019-11-09 00:33:28 +08:00
<script>
2020-12-13 05:10:59 +08:00
prettier.format("type Query { hello: String }", {
2019-11-09 00:33:28 +08:00
parser: "graphql",
plugins: prettierPlugins,
});
2018-05-27 15:17:38 -03:00
</script>
```
Note that the [`unpkg` field](https://unpkg.com/#examples) in Prettier’s `package.json` points to `standalone.js`, that’s why `https://unpkg.com/prettier` can also be used instead of `https://unpkg.com/prettier/standalone.js`.
### ES Modules
2020-12-13 05:10:59 +08:00
```html
<script type="module">
import prettier from "https://unpkg.com/prettier@2.2.1/esm/standalone.mjs";
import parserGraphql from "https://unpkg.com/prettier@2.2.1/esm/parser-graphql.mjs";
2020-12-13 05:10:59 +08:00
prettier.format("type Query { hello: String }", {
parser: "graphql",
plugins: [parserGraphql],
});
</script>
```
2018-05-27 15:17:38 -03:00
### AMD
```js
define([
2020-11-28 09:21:04 +09:00
"https://unpkg.com/prettier@2.2.1/standalone.js",
"https://unpkg.com/prettier@2.2.1/parser-graphql.js",
2018-05-27 15:17:38 -03:00
], (prettier, ...plugins) => {
2020-12-13 05:10:59 +08:00
prettier.format("type Query { hello: String }", {
parser: "graphql",
plugins,
});
2018-05-27 15:17:38 -03:00
});
```
### CommonJS
```js
const prettier = require("prettier/standalone");
const plugins = [require("prettier/parser-graphql")];
2020-12-13 05:10:59 +08:00
prettier.format("type Query { hello: String }", {
parser: "graphql",
plugins,
});
2018-05-27 15:17:38 -03:00
```
This syntax doesn’t necessarily work in the browser, but it can be used when bundling the code with browserify, Rollup, webpack, or another bundler.
2018-05-27 15:17:38 -03:00
### Worker
```js
2020-11-28 09:21:04 +09:00
importScripts("https://unpkg.com/prettier@2.2.1/standalone.js");
importScripts("https://unpkg.com/prettier@2.2.1/parser-graphql.js");
2020-12-13 05:10:59 +08:00
prettier.format("type Query { hello: String }", {
parser: "graphql",
plugins: prettierPlugins,
});
2018-05-27 15:17:38 -03:00
```