Skip to content

Commit d51161a

Browse files
committed
📄 update js-core docs to reflect exports + physical sub-path dual strategy
- AGENTS.md: document that sub-paths are now exposed via both the `exports` field (for modern resolvers) and physical `<name>/package.json` fallbacks (for legacy resolvers); update "adding a new sub-path" steps - README.md: add a note about the dual resolution strategy and the TypeScript moduleResolution requirement for exports-aware sub-paths
1 parent f479363 commit d51161a

2 files changed

Lines changed: 20 additions & 13 deletions

File tree

‎packages/js-core/AGENTS.md‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,30 @@ Every export must work in both Node.js and browser environments. Do not add:
3838
- Node.js built-in modules (`fs`, `path`, `process`, etc.)
3939
- Any API that is not available in both runtimes
4040

41-
### Sub-paths via physical directories
41+
### Sub-path exports
4242

4343
All APIs live under a named sub-path (e.g. `@datadog/js-core/time`). There is no root entry
4444
point. Each sub-path corresponds to a single source file under `src/`.
4545

46-
Each sub-path is resolved via a **physical `<name>/package.json` directory** (relative
47-
`main`/`module`/`types`, see `time/package.json`) — the same maximally-compatible pattern the rest
48-
of the Browser SDK uses (e.g. `@datadog/browser-rum/internal`). This works across all resolvers,
49-
including legacy ones (webpack 4, old Node, older Jest/ts-node).
46+
Each sub-path is exposed **two ways** for maximum compatibility:
5047

51-
We deliberately do **not** use the `package.json` `"exports"` field yet. `exports` would give
52-
encapsulation and explicit `import`/`require`/`types` conditions, but it breaks resolvers that
53-
predate it, and getting native-Node ESM right under `exports` requires extra packaging (a
54-
`.mjs`/`type: module` ESM scope) the build doesn't currently emit. Since this package is published
55-
transitively to all Browser SDK consumers, switching to `exports` is a breaking change — defer it
56-
to a future **major** Browser SDK release.
48+
- **`exports` field** (root `package.json`) — used by modern resolvers (webpack 5, Vite, esbuild,
49+
Rollup, native Node ESM/CJS, TypeScript `node16`/`nodenext`/`bundler`). Maps the sub-path to the
50+
correct `import` (ESM), `require` (CJS), and `types` targets. The build emits `esm/package.json`
51+
with `{"type":"module"}` so Node.js correctly treats `esm/*.js` as ES modules (pass
52+
`--esm-type-module` to `build-package.ts`).
53+
- **Physical `<name>/package.json` fallback** — used by legacy resolvers that ignore `exports`
54+
(webpack 4, old Node, older Jest/ts-node). Relative `main`/`module`/`types` pointing at the
55+
same built files.
5756

5857
When adding a new sub-path:
5958

6059
1. Create `src/<name>.ts`
61-
2. Add a physical `<name>/package.json` with relative `main`/`module`/`types` (see
60+
2. Add `"./<name>"` to the `exports` field in `package.json` with `import`, `require`, and `types`
61+
conditions
62+
3. Add a physical `<name>/package.json` with relative `main`/`module`/`types` (see
6263
`time/package.json`), and add `"<name>"` to the `files` array so it ships in the package
63-
3. Add `"@datadog/js-core/<name>"` to the `paths` map in the root `tsconfig.base.json`
64+
4. Add `"@datadog/js-core/<name>"` to the `paths` map in the root `tsconfig.base.json`
6465

6566
## Current sub-paths
6667

‎packages/js-core/README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ yarn add @datadog/js-core
1515
The package exposes named sub-paths (e.g. `@datadog/js-core/time`). Import only what you need.
1616
There is no root entry point — always import from a sub-path.
1717

18+
Sub-paths are resolved via the `exports` field in `package.json` (for modern bundlers and native
19+
Node ESM/CJS) with a physical `<name>/package.json` fallback for legacy resolvers (webpack 4, etc.).
20+
21+
TypeScript users need `"moduleResolution": "bundler"`, `"node16"`, or `"nodenext"` in their
22+
`tsconfig.json` to resolve sub-path exports correctly.
23+
1824
### `@datadog/js-core/time`
1925

2026
Time utilities that work in any JavaScript runtime.

0 commit comments

Comments
 (0)