nodejs / import-in-the-middle

Like `require-in-the-middle`, but for ESM import
https://www.npmjs.com/package/import-in-the-middle
Apache License 2.0
55 stars 22 forks source link

Fix duplicate default exports #53

Closed jsumners-nr closed 6 months ago

jsumners-nr commented 6 months ago

Ref: https://github.com/newrelic/node-newrelic/issues/1920

👋 my last "fix" (#43) broke in a new way thanks to the helpful ESM allowance of exporting default multiple times. This PR resolves the issue. I suspect there could be further problems with the exports since I have no idea how exporting default multiple times is meant to be interpreted, but I suspect any such issues will be an extreme edge case.

The only other thing I can think of to do here is to somehow track that we already have a default export and munge the names of any subsequent ones.


I have since learned that ESM doesn't actually allow exporting default multiple times. Transitive default exports get mapped to some other name for the module that has imported them, and only the directly imported default gets exported. Still, we have to handle figuring out which default is the right one and construct our shim namespace accordingly.


2023-01-03: this can only be supported on Node versions where we parse the modules with an AST parser and interpret the code manually. So I have updated the test suite for this feature to only run on such versions.

jsumners-nr commented 6 months ago

Moving back to draft as the issue isn't quite solved. It is possible the wrong default gets exported. In the case of got, it is possible the "request" default gets exported instead of the main got API.