From 47ed9a77e3f21b42206209b4f63bf04ff5cd4b2a Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Mon, 25 May 2020 09:51:54 +0300 Subject: [PATCH] Add the browser and unpkg field to package.json (#8387) --- docs/browser.md | 4 ++++ package.json | 2 ++ 2 files changed, 6 insertions(+) diff --git a/docs/browser.md b/docs/browser.md index e1d89e685..4a8d9657c 100644 --- a/docs/browser.md +++ b/docs/browser.md @@ -5,6 +5,8 @@ title: Browser 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. +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). + ### `prettier.format(code, options)` 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. Additionally, the parsers included in the Prettier package won't be loaded automatically, so you need to load them before using them. @@ -26,6 +28,8 @@ See [Usage](#usage) below for examples. ``` +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 ```js diff --git a/package.json b/package.json index 7a550b60f..11558ae83 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,8 @@ "author": "James Long", "license": "MIT", "main": "./index.js", + "browser": "./standalone.js", + "unpkg": "./standalone.js", "engines": { "node": ">=10.13.0" },