@@ -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
4343All APIs live under a named sub-path (e.g. ` @datadog/js-core/time ` ). There is no root entry
4444point. 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
5857When adding a new sub-path:
5958
60591 . 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
0 commit comments