Skip to content

Revamp Build System #3574

Closed
Closed

Description

@azz

Prettier's build system is not pretty.

We support both install from both npm (npm i prettier) and GitHub (npm i prettier/prettier).

The structure of the npm production build is:

bin/
  prettier.js
index.js
third-party.js
parser-babylon.js
parser-flow.js
parser-....js

Parsers are packaged separately because they can be expensive to load into the browser or Node.js runtime.

#3296 also raises that it is non-trivial to load Prettier in the browser at all.

We use Rollup to create the bundles above. There is actually a copy of most of Prettier's source code in index.js and bin/prettier.js as they are separate entries without a common package.

We should look at a packages approach to creating bundles, and support UMD.

Activity

  1. added
    type:infra
    on Dec 26, 2017
  2. duailibe commented on Dec 26, 2017

    @duailibe
    Collaborator

    Nice writeup! There's some overlap with #3296 but I think that can be solved in a simple way - e.g. there's no CLI for the browser so there's no issue of code overlapping there - but could be optimized later with the findings of this issue :)

  3. azz commented on Dec 26, 2017

    @azz
    MemberAuthor

    Here's a sketch of what I'm thinking about:

    npm package.json "main": "./main"
    GItHub package.json "main": "./src/main"

    dist/
      cli.js
      doc.js
      common.js
      config.js
      main.js
    
      language-js/
        index.js
        parser-babylon.js
        parser-flow.js
        parser-typescript.js
      language-.../
        ...
    

    Replace all the eval("require") stuff with externals in Rollup.

  4. azz commented on Dec 26, 2017

    @azz
    MemberAuthor

    For the UMD build we don't want to externalize the parsers, instead we should create one UMD bundle for each plugin (language directory).

    So the build steps are:

    1. Rollup cli/, doc/, etc, format: UMD, externals: all other packages
    2. Rollup language-*/ -> language-*.js, format: CommonJS, externals: all packages + language-*/parser-*.js
    3. Rollup language-*.js -> language-*.umd.js, format: UMD.
  5. azz commented on Dec 26, 2017

    @azz
    MemberAuthor

    We can then support passing plugins as objects via options.plugins.

    AMD:

    define(
      [
        "prettier/main.umd",
        "prettier/language-js.umd",
        "prettier/language-graphql.umd"
      ],
      (prettier, js, graphql) => {
        prettier.format("", {
          plugins: [js, graphql]
        });
      }
    );

    Root:

    <script src="https://unpkg.com/prettier/main.umd.js"></script>
    <script src="https://unpkg.com/prettier/language-js.umd.js"></script>
    <script src="https://unpkg.com/prettier/language-graphql.umd.js"></script>
    
    <script>
    prettier.format("", {
      plugins: [prettierPlugin.js, prettierPlugin.graphql]
    });
    </script>
  6. vjeux commented on Dec 26, 2017

    @vjeux
    Contributor

    The tricky aspect of umd is that it is asynchronous but all the printing code is synchronous so we need for multiparser to be able to synchronously require code...

  7. azz commented on Dec 26, 2017

    @azz
    MemberAuthor

    Ah rats. That basically rules out supporting lazy-loading with AMD then 😢 . importScripts is synchronous but only in workers.

    I guess for Root/AMD you could just pre-load all the languages you want to use. For the playground we'd probably want to keep using importScripts though.

  8. azz commented on Jan 10, 2018

    @azz
    MemberAuthor

    Maybe we should just use webpack with CommonsChunkPlugin for the parsers?

  9. duailibe commented on Jan 10, 2018

    @duailibe
    Collaborator

    FYI rollup will add support for code splitting: rollup/rollup#1841

  10. azz commented on Jan 10, 2018

    @azz
    MemberAuthor

    Is that async code splitting?

  11. azz commented on Jan 10, 2018

    @azz
    MemberAuthor

    We need to just delete the build system and start again, it's grown too far out of hand. I had to do 832432d just to fix the build.

  12. duailibe commented on Jan 11, 2018

    @duailibe
    Collaborator
  13. xixixao commented on Mar 1, 2018

    @xixixao
    Contributor

    I might hack something together over the weekend to unblock browser compatible sync api.

  14. duailibe commented on Apr 27, 2018

    @duailibe
    Collaborator

    Okay I have done quite a bit of refactoring and we're in a better place now (see #4364 and #4386). A lot of stuff that was present in index.js is now in main/ and stuff there should not touch the filesystem. It's possible now to create a separate endpoint that doesn't load plugins and doesn't look for config files (those would have to be passed explicitly).. and I've created web.js as a POC to demonstrate this.

    What @azz designed here is technically possible now.. I'm not 100% sure what the next steps are now (I was deep in the refactoring), I'll come back to this in a couple of days with my head clearer.

  15. azz commented on May 27, 2018

    @azz
    MemberAuthor

    @duailibe think we can close this?

  16. duailibe commented on May 27, 2018

    @duailibe
    Collaborator

    I think so.. It’s better now, but it’s not perfect though

    I don’t know if there’s anything blocked by our current build now..

  17. azz commented on May 28, 2018

    @azz
    MemberAuthor

    Going to close it. Most of the issues raised (UMD especially) have been shipped. We can open more issues for specific follow-ups.

  18. vjeux commented on May 28, 2018

    @vjeux
    Contributor

    @duailibe btw I didn’t get the chance to say great job. Build systems are not super fun things to work on but they are so important :)

  19. duailibe commented on May 28, 2018

    @duailibe
    Collaborator

    @vjeux Thank you ❤️!

    It wasn't so bad either, I learned a lot in the process.. and hopefully all this will help us in the future 🙏 :)

  20. added
    locked-due-to-inactivity
    on Aug 26, 2018
  21. locked as resolved and limited conversation to collaborators on Aug 26, 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

    locked-due-to-inactivitytype:infra

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions