fix(otlp-exporter-base): use dynamic import instead of require in esm/esnext build #5220
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Which problem is this PR solving?
When bundling with
rollup
, the compiled files from the ESM build are used. When the bundle is run,require
is not defined because it's ESM - also the file it tries to require is not there since it's a bundle now.This is the case because we have a workaround in the
HttpExporterTransport
to lazy load a file that requireshttp
until we actually export. We need this workaround to ensure that the@opentelemetry/instrumentation-http
package can properly wrap everything - I'm not sure exactly how much of that is actually needed so I'll follow up with some more investigation after we get this fix in.This PR changes the
require()
to anawait import()
, which compiles torequire()
wrapped in aPromise.resolve()
for CJSimport()
for esm/esnextThese types of problems are actually prime candidates to test when we start working on
While this PR does not add any additional tests, it will unblock bundling with
rollup
for now. We should, however, get started on #4744 eventually to have this tested in an automated manner.Type of change
How Has This Been Tested?