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

Build dual ESM/CJS packages; move some exports to deep imports #6580

Merged
merged 5 commits into from Jun 16, 2022

Commits on Jun 16, 2022

  1. Revert "Inline usage reporting protobuf into main package (#6515)"

    This reverts commit 0b58585.
    
    This leaves the fix of adding cors and body-parser to server's deps and
    leaves the removal of a TODO(AS4) about this.
    
    The build process for this package is completely different from a normal
    TS package, so keeping it together seems best. This seems like it'll
    make setting up ESM builds easier.
    glasser committed Jun 16, 2022
    Configuration menu
    Copy the full SHA
    dc43460 View commit details
    Browse the repository at this point in the history
  2. plugins: Use dynamic import instead of manual intermediate functions

    We want to avoid loading usage reporting protobuf until as late as
    possible. Previously we had a specific file src/plugins/index.ts full of
    little wrapper functions that call `require`. Now we use dynamic
    `import` (which is compiled to CJS as `require` by tsc) instead.
    
    We use "exports" in package.json to let you do deep imports from
    particular files (only). Note that this still only supports CJS!
    glasser committed Jun 16, 2022
    Configuration menu
    Copy the full SHA
    830be6c View commit details
    Browse the repository at this point in the history
  3. Generate ESM and CJS files; change to deep-import API

    The overall approach is to ask tsc to build two different projects for
    package/server, one with CJS output and one with ESM, outputting into
    two subdirectories of packages/server/dist. package.json teaches Node
    how to find various entry points with both ESM and CJS.
    
    To make the ESM version work, this equires making all "file imports"
    (not `import type` or imports of packages) end with `.js`. Instead of
    using `__dirname` we now update the version number in a normal TS file
    before building.
    
    We have to do some weird stuff with how we import the proto package
    since it is not ESM-y enough due to the weird index.js in it. (Maybe we
    should just fix our protobuf port to drop long support directly so we
    can have a nicer index.js which is analyzed properly?)
    
    We update the actual API so that `startStandaloneServer` must be
    imported from `@apollo/server/standalone` (and thus express can be
    tree-shaken if you don't use the standalone server) and so that the
    various plugins are imported from `@apollo/server/plugin/usageReporting`
    etc (and so you don't have to load protobuf stuff if you don't use it).
    glasser committed Jun 16, 2022
    Configuration menu
    Copy the full SHA
    71fa982 View commit details
    Browse the repository at this point in the history
  4. scripts to subdir

    glasser committed Jun 16, 2022
    Configuration menu
    Copy the full SHA
    1389f4f View commit details
    Browse the repository at this point in the history
  5. Make @apollo/usage-reporting-protobuf dual CJS/ESM too

    This mostly involved running pbjs a second time to generate ESM. A few
    tweaks were made to our fork `@apollo/protobufjs`:
    - Long support is just turned off by default, so we don't need the
      wrapper index.* files that just turn it off
    - The import line in generated code no longer includes `* as` since that
      didn't quite work in practice (it imported an object with a 'default'
      key on it)
    - The `@apollo/protobufjs` package.json uses `exports` to show where
      `/minimal` is
    glasser committed Jun 16, 2022
    Configuration menu
    Copy the full SHA
    64b2d63 View commit details
    Browse the repository at this point in the history