open-telemetry / opentelemetry-js

OpenTelemetry JavaScript Client
https://opentelemetry.io
Apache License 2.0
2.77k stars 812 forks source link

Publish ESM packages following ESM spec #4898

Open JamieDanielson opened 4 months ago

JamieDanielson commented 4 months ago

Is your feature request related to a problem? Please describe.

Our packages currently publish directories of ESM builds, but they do not follow ESM spec and therefore cause problems for some end users trying to use them. For example, see issue #3989 .

Required (for each package) to fully support ESM

Maybe Required, otherwise Recommended

This (closed) PR can be used as a reference.

I would argue that we need a testing strategy in place to verify these changes, BEFORE we make any changes, as our current ESM tests cover a lot of ground but not the problems solved with this change. There are a few issues floating around I believe, but for now I will link #4008 to build out smoke / integration tests.

Related Issues that may be resolved with these changes:

david-luna commented 3 months ago

Hi @JamieDanielson

That's a pretty detailed list. Thanks! :) Is it a proposal or already has a consensus with other maintainers?

trentm commented 3 months ago
  • Change the package type to module in package.json

We don't need this step do we? If "exports" in package.json has the necessary "import" entries, then leaving empty or explicitly "type": "commonjs" should be fine.

JamieDanielson commented 2 months ago
  • Change the package type to module in package.json

We don't need this step do we? If "exports" in package.json has the necessary "import" entries, then leaving empty or explicitly "type": "commonjs" should be fine.

I have to research more to confirm. I'd read something about either how the code is transpiled or how the types are generated that require type: module but offhand I don't quite remember. All the examples I've seen of folks doing this approach, they updated the type. But as time goes on and more features come out to help with interop, I can see some of these requirements potentially going away (aside from minimum TypeScript and Node versions to take advantage of said features). When we're ready to move forward with this (will likely want more testing in place first) then we can revisit whether this change is needed.

ianwremmel commented 2 months ago

This is all anecdotal; I've been trying to figure out how to dual-publish for a while and I imagine there are multiple "correct" approaches. A big part of the struggle comes from TypeScripts's behaviors not being entirely documented (like, if we just wrote typesafe mjs and eliminated the typescript compile step, we pretty wouldn't every have folks complaining about cjs/esm issues). In any case, this is what I believe has to be done to dual publish (at least as far as package.json and build outputs):

So, this is a really long way to say, "yes, i think the type in package.json remains necessary".

If it's helpful, I've got this repo seemingly dual-publishing correctly (aside from the .d.ts vs .d.cts issue).