Skip to content

Commit 8aa1ba2

Browse files
committed
fix(misc): otel import issue
1 parent 3944b82 commit 8aa1ba2

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

packages/shared/tracing/src/extract.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@ let otelModulePromise: Promise<any | null> | null = null
1414
// be installed in the consumer app; we look it up purely at runtime.
1515
const OTEL_PKG = '@opentelemetry/api'
1616

17+
// Wrap import() in a Function constructor so the import() expression is
18+
// never present as syntax in the bundled output. hermesc (the Hermes bytecode
19+
// compiler used by React Native release builds) rejects import() at parse
20+
// time — before any dead-code elimination — so the syntax must be physically
21+
// absent from the bundle. new Function() bodies are opaque strings to all
22+
// static parsers, including hermesc.
23+
// eslint-disable-next-line no-new-func
24+
const _dynamicImport = new Function('p', 'return import(p)') as (
25+
pkg: string
26+
) => Promise<any>
27+
1728
function loadOtel(): Promise<any | null> {
1829
if (otelModulePromise === null) {
19-
// The variable specifier + magic comments together cover every common
20-
// bundler. Webpack/Turbopack honor the ignore comments; bundlers that
21-
// ignore the comments still bail out on the non-literal specifier.
22-
otelModulePromise = import(
23-
/* webpackIgnore: true */ /* @vite-ignore */ /* turbopackIgnore: true */ OTEL_PKG
24-
).catch(() => null)
30+
otelModulePromise = _dynamicImport(OTEL_PKG).catch(() => null)
2531
}
2632
return otelModulePromise
2733
}

0 commit comments

Comments
 (0)