Skip to content

feat!: replace the top-level browser field with #platform subpath imports - #7141

Merged
overbalance merged 19 commits into
open-telemetry:mainfrom
embrace-io:overbalance/remove-browser-field
Oct 8, 2026
Merged

overbalance merged 19 commits into
open-telemetry:mainfrom
embrace-io:overbalance/remove-browser-field

Conversation

@overbalance

@overbalance overbalance commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Stack: #7142 is stacked on #7141 and merges after it. #7140 is independent.

Summary

Fixes #7088

Twelve packages used a top-level browser field to swap dist/platform/node/* for dist/platform/browser/* at bundle time. They now import platform code through #platform, resolved by package.json#imports:

"imports": {
  "#platform": {
    "otel": {
      "node": "./src/platform/node/index.ts",
      "default": "./src/platform/browser/index.ts"
    },
    "node": {
      "require": "./dist/platform/node/index.cjs",
      "default": "./dist/platform/node/index.mjs"
    },
    "require": "./dist/platform/browser/index.cjs",
    "default": "./dist/platform/browser/index.mjs"
  }
}

Node.js is selected by the node condition and the browser build is the fallback, instead of the browser condition #7088 proposed, so every other environment needs no configuration. The src/platform/index.ts barrels, which hardcoded ./node, are deleted.

The otel condition points at src, so a package's own tests and type-checks need no build of that package. Only the package under test applies it: tsconfig.base.json sets customConditions, scripts/mocha-otel-condition.cjs adds it for mocha, and a karma webpack rule scoped to src replaces NormalModuleReplacementPlugin. Tarballs do not ship src, so consumers never match otel.

Build:

  • tsdown keeps #platform external in JS, so the consumer's resolver picks the platform. Declarations resolve it to the Node.js barrel, because node10 consumers cannot resolve #platform from a .d.ts; the tsc resolver follows otel there.
  • verify-pack loads every #platform target from the tarball, and checks that Node.js resolves the node branch while resolvers without node reach the browser build. It also fails on a map without an otel branch and on declarations that still reference #platform.

Breaking changes

  • Bundlers without imports support fail to resolve #platform. Jest needs 29.4 or later. React Native needs Metro 0.81.3 or later: React Native 0.79 and later, or 0.76 to 0.78 with an updated Metro.
  • webpack 5 needs enhanced-resolve 5.8.2 or later (webpack 5.54 or later).
  • Parcel needs package exports enabled, and cannot resolve #platform from CommonJS require(). ESM imports work.
  • The ./platform, ./platform/browser, ./detectors/platform and ./detectors/platform/browser subpath exports are removed. Import from the package root.

Known limitation

Jest 30 applies node to require() even under jsdom (jestjs/jest#16476), so it loads the Node.js implementations, as it did when it ignored the browser field. The migration guide gives workarounds.

Related PRs

Type of change

  • Breaking change
  • This change requires a documentation update

How Has This Been Tested?

  • npm run compile
  • npm test. Core's #platform test asserts by identity that src/platform/node loaded, and fails if the mocha hook is removed.
  • npm run test:browser. Core's browser identity test fails if the karma otel rule stops matching.
  • npm run test:webworker
  • npm run test:bundle
  • npm run verify-pack. A throwing browser barrel fails it.
  • npm run lint

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Merged · refreshed 2026-10-08 22:54 UTC

Status above doesn't look right?
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@codecov

codecov Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.05%. Comparing base (0f72eab) to head (f5d8ba0).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7141      +/-   ##
==========================================
- Coverage   95.05%   95.05%   -0.01%     
==========================================
  Files         383      382       -1     
  Lines       13522    13517       -5     
  Branches     3126     3126              
==========================================
- Hits        12853    12848       -5     
  Misses        669      669              
Files with missing lines Coverage Δ
...strumentation/src/instrumentationNodeModuleFile.ts 100.00% <100.00%> (ø)
...ckages/opentelemetry-exporter-zipkin/src/zipkin.ts 100.00% <100.00%> (ø)
...ges/opentelemetry-resources/src/detectors/index.ts 100.00% <ø> (ø)
packages/sdk-trace/src/TracerProvider.ts 97.22% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

overbalance added a commit to embrace-io/opentelemetry-js that referenced this pull request Oct 5, 2026
@overbalance
overbalance force-pushed the overbalance/remove-browser-field branch from 3c6b6a4 to 5f5d337 Compare October 5, 2026 20:38
@overbalance
overbalance marked this pull request as ready for review October 5, 2026 22:13
@overbalance
overbalance requested review from a team as code owners October 5, 2026 22:13
@pichlermarc pichlermarc self-assigned this Oct 7, 2026
@overbalance
overbalance force-pushed the overbalance/remove-browser-field branch from 6defa3d to 280074a Compare October 7, 2026 16:27
@overbalance overbalance added this to the SDK 3.0 milestone Oct 7, 2026
@david-luna

david-luna commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

I've used EDOT Browser as a Guinea pig:

  • checked out this PR and build the packages
  • replace core and sdk packages in EDOT with the built version
  • ran the example app

I'm getting a list of warnings though

> @elastic/opentelemetry-browser@0.3.0 build:min
> esbuild ./lib/bundle.js --bundle --conditions=esnext,module --minify --sourcemap --platform=browser --target=es2022 --outfile=build/elastic-otel-browser.min.js

▲ [WARNING] Ignoring this import because "../../../../../otel/ot-js/packages/sdk-metrics/dist/export/MetricData.mjs" was marked as having no side effects [ignored-bare-import]

    ../../../../../otel/ot-js/packages/sdk-metrics/dist/export/PeriodicExportingMetricReader.mjs:1:7:
      1 │ import "./MetricData.mjs";
        ╵        ~~~~~~~~~~~~~~~~~~

  "sideEffects" is false in the enclosing "package.json" file:

    ../../../../../otel/ot-js/packages/sdk-metrics/package.json:88:2:
      88 │   "sideEffects": false
         ╵   ~~~~~~~~~~~~~

▲ [WARNING] Ignoring this import because "../../../../../otel/ot-js/packages/sdk-metrics/dist/export/MetricData.mjs" was marked as having no side effects [ignored-bare-import]

    ../../../../../otel/ot-js/packages/sdk-metrics/dist/view/Aggregation.mjs:1:7:
      1 │ import "../export/MetricData.mjs";
        ╵        ~~~~~~~~~~~~~~~~~~~~~~~~~~

  "sideEffects" is false in the enclosing "package.json" file:

    ../../../../../otel/ot-js/packages/sdk-metrics/package.json:88:2:
      88 │   "sideEffects": false
         ╵   ~~~~~~~~~~~~~

▲ [WARNING] Ignoring this import because "../../../../../otel/ot-js/packages/sdk-metrics/dist/export/AggregationTemporality.mjs" was marked as having no side effects [ignored-bare-import]

    ../../../../../otel/ot-js/packages/sdk-metrics/dist/export/AggregationSelector.mjs:1:7:
      1 │ import "./AggregationTemporality.mjs";
        ╵        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  "sideEffects" is false in the enclosing "package.json" file:

    ../../../../../otel/ot-js/packages/sdk-metrics/package.json:88:2:
      88 │   "sideEffects": false
         ╵   ~~~~~~~~~~~~~

▲ [WARNING] Ignoring this import because "../../../../../otel/ot-js/packages/sdk-metrics/dist/view/AggregationOption.mjs" was marked as having no side effects [ignored-bare-import]

    ../../../../../otel/ot-js/packages/sdk-metrics/dist/export/AggregationSelector.mjs:2:7:
      2 │ import "../view/AggregationOption.mjs";
        ╵        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  "sideEffects" is false in the enclosing "package.json" file:

    ../../../../../otel/ot-js/packages/sdk-metrics/package.json:88:2:
      88 │   "sideEffects": false
         ╵   ~~~~~~~~~~~~~

▲ [WARNING] Ignoring this import because "../../../../../otel/ot-js/packages/sdk-trace/dist/Sampler.mjs" was marked as having no side effects [ignored-bare-import]

    ../../../../../otel/ot-js/packages/sdk-trace/dist/sampler/AlwaysOffSampler.mjs:1:7:
      1 │ import "../Sampler.mjs";
        ╵        ~~~~~~~~~~~~~~~~

  "sideEffects" is false in the enclosing "package.json" file:

    ../../../../../otel/ot-js/packages/sdk-trace/package.json:107:2:
      107 │   "sideEffects": false
          ╵   ~~~~~~~~~~~~~

▲ [WARNING] Ignoring this import because "../../../../../otel/ot-js/packages/sdk-trace/dist/Sampler.mjs" was marked as having no side effects [ignored-bare-import]

    ../../../../../otel/ot-js/packages/sdk-trace/dist/sampler/AlwaysOnSampler.mjs:1:7:
      1 │ import "../Sampler.mjs";
        ╵        ~~~~~~~~~~~~~~~~

  "sideEffects" is false in the enclosing "package.json" file:

    ../../../../../otel/ot-js/packages/sdk-trace/package.json:107:2:
      107 │   "sideEffects": false
          ╵   ~~~~~~~~~~~~~

6 of 21 warnings shown (disable the message limit with --log-limit=0)

  build/elastic-otel-browser.min.js      198.0kb
  build/elastic-otel-browser.min.js.map    1.3mb

Let's check the warning for AlwaysOffSampler. This is the original file

/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

import type { Sampler, SamplingResult } from '../Sampler';
import { SamplingDecision } from '../Sampler';

/** Sampler that samples no traces. */
export class AlwaysOffSampler implements Sampler {
  shouldSample(): SamplingResult {
    return {
      decision: SamplingDecision.NOT_RECORD,
    };
  }

  toString(): string {
    return 'AlwaysOffSampler';
  }
}

and this is the compiled result in .mjs format

import "../Sampler.mjs"; // <- I guess this is the enum import
//#region src/sampler/AlwaysOffSampler.ts
/** Sampler that samples no traces. */
var AlwaysOffSampler = class {
	shouldSample() {
		return { decision: 0 };  // <- enum value inlined
	}
	toString() {
		return "AlwaysOffSampler";
	}
};
//#endregion
export { AlwaysOffSampler };
//# sourceMappingURL=AlwaysOffSampler.mjs.map

The type import has been removed but the enum is not. The problem is that it becomes an bare import because the compiler removes SamplingDecision since the values are inlined.

EDIT: this compilation behaviour also happens in main but for some reason I'm not getting the warnings there. I'll check it.

The warnings are not a problem per see it makes sense the bundler ignores the imports since the enum values are inlined and the import is not really necessary.

@pichlermarc pichlermarc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thank you for working on this! 🙂

(IIUC we could get rid of the mocha condition (and karma) if we were to switch to vitest as proposed in the original draft for #6293 using resolve.conditions. We definitely should not do it in this PR here as it would blow up the diff, but it might be worth re-visiting the idea once we've finished up the SDK 3.0 work, even if it's only to kick out karma from devDependencies)

@overbalance

overbalance commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

@david-luna if you set conditions' "module" you will be bypassing "exports" and pulling the top-level "module" key value. That probably won't work any longer because you need subpath exports to use certain parts of OTel.

@overbalance
overbalance added this pull request to the merge queue Oct 8, 2026
Merged via the queue into open-telemetry:main with commit 47f3858 Oct 8, 2026
26 checks passed
@overbalance
overbalance deleted the overbalance/remove-browser-field branch October 8, 2026 22:46
pacocartones added a commit to pacocartones/opentelemetry-js that referenced this pull request Oct 13, 2026
Port the factories to the #platform layout from open-telemetry#7141, open-telemetry#7170 and open-telemetry#7173: the
package entry points export createOtlp*Exporter from '#platform', the removed
src/platform/index.ts files stay removed, convertLegacyHttpOptionsWithoutEnv
moves with its module to platform/node/configuration, and the metrics proto
browser factory takes the browser configuration type.

Signed-off-by: pacocartones <pacocartones@users.noreply.github.com>
Assisted-by: Claude Opus 5.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace the legacy top-level "browser" field with #platform subpath imports

4 participants