返回介绍

Browser

发布于 2019-12-07 19:27:48 字数 2358 浏览 1119 评论 0 收藏 0

Run Prettier in the browser with the standalone.js UMD bundle shipped in the NPM package (starting in version 1.13). The UMD bundle only formats the code and has no support for config files, ignore files, CLI usage, or automatic loading of plugins.

prettier.format(code, options)

Unlike the format function from the main API, this function does not load plugins automatically, so a plugins property is required if you want to load plugins. Additionally, the parsers included in the Prettier package won't be loaded automatically, so you need to load them before using them.

See Usage below for examples.

Usage

Global

<script src="https://unpkg.com/prettier@1.19.1/standalone.js"></script>
<script src="https://unpkg.com/prettier@1.19.1/parser-graphql.js"></script>
<script>
  prettier.format("query { }", {
    parser: "graphql",
    plugins: prettierPlugins
  });
</script>

ES Modules

import prettier from "prettier/standalone";
import parserGraphql from "prettier/parser-graphql";

prettier.format("query { }", {
  parser: "graphql",
  plugins: [parserGraphql]
});

AMD

define([
  "https://unpkg.com/prettier@1.19.1/standalone.js",
  "https://unpkg.com/prettier@1.19.1/parser-graphql.js"
], (prettier, ...plugins) => {
  prettier.format("query { }", { parser: "graphql", plugins });
});

CommonJS

const prettier = require("prettier/standalone");
const plugins = [require("prettier/parser-graphql")];
prettier.format("query { }", { parser: "graphql", plugins });

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.

Worker

importScripts("https://unpkg.com/prettier@1.19.1/standalone.js");
importScripts("https://unpkg.com/prettier@1.19.1/parser-graphql.js");
prettier.format("query { }", { parser: "graphql", plugins: prettierPlugins });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文