Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use Prettier within a browser #3296

Closed
bvaughn opened this issue Nov 20, 2017 · 21 comments
Closed

Can't use Prettier within a browser #3296

bvaughn opened this issue Nov 20, 2017 · 21 comments
Labels
help wanted We're a small group who can't get to every issue promptly. We’d appreciate help fixing this issue! locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:infra Issues about CI, publishing to npm, or similar
Milestone

Comments

@bvaughn
Copy link

bvaughn commented Nov 20, 2017

Following up from this Twitter comment; relates to babel/website/issues/1388 and babel/website/pull/1466.

The Babel REPL has been using Prettier via bundle.run for some time. Version 1.7.0 broke our ability to do this with the error: "cosmiconfig requires at least version 4 of Node". Version 1.8.0 introduced a different error: "os$2.homedir is not a function".

It would be ideal if Prettier output a browser-friendly UMD bundle for use-cases like this, but in lieu of that maybe it could at least work via a solution like rundle.run? 😄

For the time being, we've pinned Babel to use Prettier version 1.6.1. You can try it out here if you'd like.

@ikatyang ikatyang added the type:infra Issues about CI, publishing to npm, or similar label Nov 20, 2017
@mitermayer
Copy link
Member

mitermayer commented Nov 20, 2017

This sounds reasonable to me, I can try looking into this during the weekend, unless @azz , @suchipi @ikatyang wants to look into this first

@bvaughn
Copy link
Author

bvaughn commented Nov 20, 2017

A UMD bundle would be so-so-so great ❤️ If there's support for that idea, I'd also be happy to help (although I am not familiar with Prettier's codebase)

@suchipi
Copy link
Member

suchipi commented Nov 20, 2017

I think we can fix "os$2.homedir is not a function" with the same approach as in this PR (adding to the rollup-plugin-replace config): #2930

"cosmiconfig requires at least version 4 of Node" might require a change to cosmiconfig.

@suchipi
Copy link
Member

suchipi commented Nov 20, 2017

Also, since we are already using rollup, we are probably not too far from generating a working UMD bundle.

@ikatyang
Copy link
Member

How about extracting prettier.format() to another file? The browser version seems no need to read config file (fs-related), it only cares about prettier.format().

@bvaughn
Copy link
Author

bvaughn commented Nov 20, 2017

A trimmed down version for the browser would be slick.

Would also be nice if there were multiple UMD targets - so we could avoid loading parsers for targets/languages we don't care about (eg CSS).

But I don't want to be greedy. 😁

@duailibe
Copy link
Member

The parsers are already bundled separately! Seems like we could do is have another bundle for the format function and we’ll have all the pieces in place, just needing some work so they can work together in the browser

@azz
Copy link
Member

azz commented Nov 21, 2017

It already kind of works - the files here are built with rollup and already run in the browser, you just need this gross patch:

self.global = self;
self.util = {};
self.path = {};
self.Buffer = {
isBuffer: function() {
return false;
}
};
// eslint-disable-next-line
fs = module$1 = module = path = os = crypto = {};
// eslint-disable-next-line no-undef
os.homedir = function() {
return "/home/prettier";
};
self.process = { argv: [], env: { PRETTIER_DEBUG: true }, version: "v8.5.0" };
self.assert = { ok: function() {}, strictEqual: function() {} };
self.require = function require(path) {
if (path === "stream") {
return { PassThrough() {} };
}
return self[path.replace(/.+-/, "")];
};

We can definitely improve this, splitting the cli into a separate bundle is a great idea. I like the idea of making a UMD bundle, too.

@duailibe
Copy link
Member

duailibe commented Dec 10, 2017

Now that I've worked a bit more on the playground there are a few problemas we need to sort to get this working:

Does it make sense to ship a version with those shims and a custom require (not the hackish version we use) ?

@suchipi
Copy link
Member

suchipi commented Dec 10, 2017

I think we should ship the shims, but instead of doing the lazy require stuff or using a custom require, we should ship a UMD bundle containing a factory function that users plug the parsers into, and we should ship the parser versions we vendor as UMD bundles.

@suchipi
Copy link
Member

suchipi commented Dec 10, 2017

So eventual CommonJS usage would be something like:

const makePrettier = require("prettier/bundle/makePrettier"); // or whatever
const flowParser = require("prettier/bundle/flow");
const prettier = makePrettier({ flowParser })

And you would only ship the parsers you need

@duailibe duailibe added the help wanted We're a small group who can't get to every issue promptly. We’d appreciate help fixing this issue! label Dec 11, 2017
@siefkenj
Copy link
Contributor

For my usage, it would be nice to be able to export prettier without a parser. I'm currently developing a parser out-of-tree, and I need to use the printDoc functions but not necessarily any parsing functions.

@j-f1
Copy link
Member

j-f1 commented Dec 21, 2017

@siefkenj Check out #3511 and #3536.

@duailibe duailibe self-assigned this Dec 26, 2017
@duailibe
Copy link
Member

I'm gonna start working on this now :)

@azz
Copy link
Member

azz commented Dec 26, 2017

@duailibe Wow, I was just typing up #3574.

@duailibe
Copy link
Member

Can't work on this right now, if anyone wants to take a shot at it (cc @azz)

@huv1k
Copy link

huv1k commented Feb 24, 2018

Any news on this? :) I think a lot of tools could benefit from it for example Graphql playground graphql/graphql-playground#327

@nickmccurdy
Copy link
Member

nickmccurdy commented Apr 28, 2018

This would also be great for using Prettier in browser extensions! See https://github.com/krawaller/prettier-js-extension.

@suchipi
Copy link
Member

suchipi commented May 2, 2018

@duailibe has been doing some refactoring work that is getting us closer to this but we aren't all the way there yet, if I understand correctly.

@huv1k
Copy link

huv1k commented May 27, 2018

Thx we are rocking it in playground now :) graphql/graphql-playground#702

@azz
Copy link
Member

azz commented May 27, 2018

Yep, shipped in 1.13.0:
https://prettier.io/docs/en/browser.html

@azz azz closed this as completed May 27, 2018
@azz azz added this to the 1.13 milestone May 27, 2018
@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Aug 25, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Aug 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
help wanted We're a small group who can't get to every issue promptly. We’d appreciate help fixing this issue! locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:infra Issues about CI, publishing to npm, or similar
Projects
None yet
Development

No branches or pull requests

10 participants