Skip to content

Can't use Prettier within a browser #3296

Closed

Description

@bvaughn

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.

Activity

  1. added
    type:infra
    on Nov 20, 2017
  2. mitermayer commented on Nov 20, 2017

    @mitermayer
    Member

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

  3. bvaughn commented on Nov 20, 2017

    @bvaughn
    Author

    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)

  4. suchipi commented on Nov 20, 2017

    @suchipi
    Member

    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.

  5. suchipi commented on Nov 20, 2017

    @suchipi
    Member

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

  6. ikatyang commented on Nov 20, 2017

    @ikatyang
    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().

  7. bvaughn commented on Nov 20, 2017

    @bvaughn
    Author

    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. 😁

  8. duailibe commented on Nov 20, 2017

    @duailibe
    Collaborator

    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

  9. azz commented on Nov 21, 2017

    @azz
    Member

    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.

  10. duailibe commented on Dec 10, 2017

    @duailibe
    Collaborator

    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) ?

  11. suchipi commented on Dec 10, 2017

    @suchipi
    Member

    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.

  12. suchipi commented on Dec 10, 2017

    @suchipi
    Member

    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

  13. added
    help wanted
    on Dec 11, 2017
  14. siefkenj commented on Dec 21, 2017

    @siefkenj
    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.

  15. j-f1 commented on Dec 21, 2017

    @j-f1
    Member

    @siefkenj Check out #3511 and #3536.

  16. self-assigned this
    on Dec 26, 2017
  17. duailibe commented on Dec 26, 2017

    @duailibe
    Collaborator

    I'm gonna start working on this now :)

  18. azz commented on Dec 26, 2017

    @azz
    Member

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

  19. duailibe commented on Dec 27, 2017

    @duailibe
    Collaborator

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

  20. removed their assignment
    on Dec 27, 2017
  21. huv1k commented on Feb 24, 2018

    @huv1k

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

  22. nickserv commented on Apr 28, 2018

    @nickserv
    Member
  23. suchipi commented on May 2, 2018

    @suchipi
    Member

    @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.

  24. huv1k commented on May 27, 2018

    @huv1k

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

  25. azz commented on May 27, 2018

    @azz
    Member
  26. added this to the 1.13 milestone on May 27, 2018
  27. added
    locked-due-to-inactivity
    on Aug 25, 2018
  28. locked as resolved and limited conversation to collaborators on Aug 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedlocked-due-to-inactivitytype:infra

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions