Repository navigation
feat!: replace the top-level browser field with #platform subpath imports - #7141
overbalance merged 19 commits into
Conversation
Pull request dashboard statusMerged · refreshed 2026-10-08 22:54 UTC Status above doesn't look right?
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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
🚀 New features to boost your workflow:
|
3c6b6a4 to
5f5d337
Compare
6defa3d to
280074a
Compare
|
I've used EDOT Browser as a Guinea pig:
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.3mbLet's check the warning for /*
* 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 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.mapThe type import has been removed but the enum is not. The problem is that it becomes an bare import because the compiler removes 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
left a comment
There was a problem hiding this comment.
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)
|
@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. |
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
Summary
Fixes #7088
Twelve packages used a top-level
browserfield to swapdist/platform/node/*fordist/platform/browser/*at bundle time. They now import platform code through#platform, resolved bypackage.json#imports:Node.js is selected by the
nodecondition and the browser build is the fallback, instead of thebrowsercondition #7088 proposed, so every other environment needs no configuration. Thesrc/platform/index.tsbarrels, which hardcoded./node, are deleted.The
otelcondition points atsrc, so a package's own tests and type-checks need no build of that package. Only the package under test applies it:tsconfig.base.jsonsetscustomConditions,scripts/mocha-otel-condition.cjsadds it for mocha, and a karma webpack rule scoped tosrcreplacesNormalModuleReplacementPlugin. Tarballs do not shipsrc, so consumers never matchotel.Build:
#platformexternal in JS, so the consumer's resolver picks the platform. Declarations resolve it to the Node.js barrel, because node10 consumers cannot resolve#platformfrom a.d.ts; thetscresolver followsotelthere.verify-packloads every#platformtarget from the tarball, and checks that Node.js resolves thenodebranch while resolvers withoutnodereach the browser build. It also fails on a map without anotelbranch and on declarations that still reference#platform.Breaking changes
importssupport 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.enhanced-resolve5.8.2 or later (webpack 5.54 or later).#platformfrom CommonJSrequire(). ESM imports work../platform,./platform/browser,./detectors/platformand./detectors/platform/browsersubpath exports are removed. Import from the package root.Known limitation
Jest 30 applies
nodetorequire()even underjsdom(jestjs/jest#16476), so it loads the Node.js implementations, as it did when it ignored thebrowserfield. The migration guide gives workarounds.Related PRs
npm run typecheck:browser. Stacked on this PR.isEnabled()anddisableAutoFlushOnDocumentHide, which the published types already declare.Type of change
How Has This Been Tested?
npm run compilenpm test. Core's#platformtest asserts by identity thatsrc/platform/nodeloaded, and fails if the mocha hook is removed.npm run test:browser. Core's browser identity test fails if the karmaotelrule stops matching.npm run test:webworkernpm run test:bundlenpm run verify-pack. A throwing browser barrel fails it.npm run lint