|
1 | 1 | import type { TraceContext } from './types' |
2 | 2 |
|
3 | | -// Cache the dynamic import across all calls. Resolved (or failed) once per |
4 | | -// process; subsequent calls hit the resolved promise as a synchronous microtask |
5 | | -// instead of re-entering the module resolver. |
6 | | -// |
7 | | -// `any` here intentionally avoids referencing @opentelemetry/api types at |
8 | | -// compile time, since it's an optional peer dep that may not be installed. |
9 | 3 | let otelModulePromise: Promise<any | null> | null = null |
10 | 4 |
|
11 | | -// Indirect the specifier through a variable so static bundlers |
12 | | -// (webpack, turbopack, rollup, vite, esbuild) do not attempt to resolve |
13 | | -// @opentelemetry/api at build time. It's an optional peer dep that may not |
14 | | -// be installed in the consumer app; we look it up purely at runtime. |
| 5 | +// Variable specifier + single magic-comment block: required to keep this |
| 6 | +// import optional through webpack / turbopack / vite / rolldown. See PR #2381. |
15 | 7 | const OTEL_PKG = '@opentelemetry/api' |
16 | 8 |
|
17 | 9 | function loadOtel(): Promise<any | null> { |
18 | 10 | if (otelModulePromise === null) { |
19 | | - // Magic comments tell each major SSR/Edge bundler to skip static |
20 | | - // resolution of this dynamic import — `@opentelemetry/api` is an |
21 | | - // optional peer dep, so it may not be installed in the consumer app. |
22 | | - // Without these, Turbopack (Next.js Edge), webpack, and Vite each |
23 | | - // error with `Module not found: Can't resolve '@opentelemetry/api'` |
24 | | - // when the peer dep is absent. The variable specifier (OTEL_PKG) |
25 | | - // also helps for bundlers that don't honor the comments. |
26 | | - // |
27 | | - // All three directives live in one block because rolldown's printer |
28 | | - // only preserves block comments containing `@__PURE__`, |
29 | | - // `@__NO_SIDE_EFFECTS__`, or `@vite-ignore`; standalone |
30 | | - // `/* webpackIgnore */` / `/* turbopackIgnore */` blocks get stripped |
31 | | - // from dist/index.mjs. Webpack and Turbopack both scan for their |
32 | | - // keyword anywhere in adjacent comments, so a shared block is honored. |
33 | 11 | otelModulePromise = ( |
34 | 12 | import(/* @vite-ignore webpackIgnore: true turbopackIgnore: true */ OTEL_PKG) as Promise<any> |
35 | 13 | ).catch(() => null) |
|
0 commit comments