Support named exports with nodejs ESM#5162
Conversation
9828617 to
28d1934
Compare
| {file: 'relay-runtime/lib/index.js', expectedExports: 92}, | ||
| {file: 'react-relay/lib/index.js', expectedExports: 35}, | ||
| {file: 'react-relay/lib/hooks.js', expectedExports: 24}, | ||
| {file: 'react-relay/lib/legacy.js', expectedExports: 16}, |
There was a problem hiding this comment.
These counts feel brittle. Do we have to come update this magic number anytime we add/remove an export from one of these modules? If that's going to be the case, we likely need comments in each of the export files explaining this coupling.
Is there not some other way to enforce this? ESLint maybe?
There was a problem hiding this comment.
Or perhaps even better, some Babel transform or similar that can automatically produce the right output from our existing code.
There was a problem hiding this comment.
@captbaritone I removed this test entirely and added a an eslint rule to eslint-plugin-relay-internal. It's turned off by default and enabled only in the package entry files. Let me know what you think.
I went with a lint rule because I'm not sure I'd be able to cover every conceivable case in a babel transform.
104b754 to
a6a116d
Compare
a6a116d to
f72616e
Compare
|
@captbaritone has imported this pull request. If you are a Meta employee, you can view this in D99335902. |
|
@captbaritone merged this pull request in 9240507. |
|
@captbaritone instead of adding workarounds could we please move forward with #4548 instead? |
It is currently not possible to use named exports with Relay in a Node.js ESM file. For example, in a brand new project with the latest version of relay-runtime as a dependency, the following code will error:
index.mjs:
When an ESM file
imports a common js file (like relay-runtime) Node.js will use cjs-module-lexer to try to determine compatible named exports.From the node.js docs:
In the entry points of react-relay and relay-runtime, the pattern of exporting a property on another object causes
cjs-module-lexerto not detect any of named exports.Example:
In this PR, I updated the entrypoints of react-relay and relay-runtime to allow Node.js's cjs named export detection to work properly. I added a test case to the Gulp build process since to test this we need the final transpiled code, but I am open to other approaches.