Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build dual ESM/CJS packages; move some exports to deep imports (#6580)
* 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. * 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! * 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). * scripts to subdir * 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
- Loading branch information