Skip to content

W2: package entry points work in CommonJS, ESM and bundlers - #29

Merged
czypnt merged 4 commits into
mainfrom
w2-exports
Nov 22, 2026
Merged

czypnt merged 4 commits into
mainfrom
w2-exports

Conversation

@czypnt

@czypnt czypnt commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

W2: package entry points

What. package.json main pointed at the browser script build dist/datalyr.js (an IIFE that assigns a local var Datalyr), so require('@datalyr/web') returned {}. The CommonJS build dist/datalyr.cjs.js was built but nothing pointed at it, and there was no exports map.

Change (package.json only, no build or source change):

  • main → dist/datalyr.cjs.js; module and types unchanged.
  • unpkg and jsdelivr → dist/datalyr.js, so a bare CDN URL for the npm package (cdn.jsdelivr.net/npm/@datalyr/web, unpkg.com/@datalyr/web) still serves the browser script it served before (both CDNs fell back to main).
  • exports: . → types dist/index.d.ts, import dist/datalyr.esm.js, require dist/datalyr.cjs.js, default dist/datalyr.esm.js; plus ./package.json and ./dist/*, so deep imports such as @datalyr/web/dist/datalyr.js, datalyr.replay.min.js and datalyr.pixels.min.js still resolve (checked with require.resolve and import.meta.resolve).
  • CHANGELOG entry under Unreleased.

The main repo's infra/tracking build reads dist/datalyr.js and package.json by file path, not through package resolution, so it is unaffected.

Test. Proof projects below (built tarball installed into real projects). No unit test: nothing in src/ changed.

Proof: built tarball vs published 1.9.14

npm ci && npm run clean && npm run build && npm pack on this branch, then npm install <tarball> into throwaway projects (typescript 5.9.3, vite 6.3.5, Node 26.5.0). "Before" is npm install @datalyr/web@1.9.14.

Project Check Before (1.9.14) After (this PR)
CommonJS require('@datalyr/web') .default.init and .datalyr.init are functions error: module is {} (no keys) works
Node ESM import datalyr from / import { datalyr } from, typeof init === 'function' error: init undefined on both (IIFE loaded as CJS) works
TS moduleResolution: node16, ESM project ("type": "module") tsc --noEmit on import datalyr, { datalyr as named } + .init error TS2339 Property 'init' does not exist on the default import same error TS2339 on the default import; named import types fine (see Not done)
TS moduleResolution: node16, CommonJS project same works works
TS moduleResolution: bundler same works works
Vite build import datalyr from '@datalyr/web' bundles works (via module) works (via exports import)

Tarball contents

npm pack --dry-run file list before (origin/main package.json) and after: 157 files each, identical paths and sizes; the only difference is package.json (1798 → 2130 bytes).

File list (after)
LICENSE 1073
README.md 3285
dist/applovin-consent-sdk.test.d.ts 66
dist/applovin-consent-sdk.test.d.ts.map 144
dist/attribution-ad-platforms.test.d.ts 726
dist/attribution-ad-platforms.test.d.ts.map 170
dist/attribution-lyr.test.d.ts 61
dist/attribution-lyr.test.d.ts.map 154
dist/attribution-oppref.test.d.ts 64
dist/attribution-oppref.test.d.ts.map 159
dist/attribution.d.ts 6843
dist/attribution.d.ts.map 2197
dist/attribution.test.d.ts 339
dist/attribution.test.d.ts.map 139
dist/auto-identify.d.ts 2797
dist/auto-identify.d.ts.map 1439
dist/config.d.ts 2106
dist/config.d.ts.map 858
dist/config.test.d.ts 52
dist/config.test.d.ts.map 116
dist/container-applovin.test.d.ts 64
dist/container-applovin.test.d.ts.map 140
dist/container-linkedin.test.d.ts 64
dist/container-linkedin.test.d.ts.map 140
dist/container-policy.test.d.ts 62
dist/container-policy.test.d.ts.map 136
dist/container-shopify-companion.test.d.ts 73
dist/container-shopify-companion.test.d.ts.map 158
dist/container-x.test.d.ts 57
dist/container-x.test.d.ts.map 126
dist/container.d.ts 17830
dist/container.d.ts.map 6077
dist/container.test.d.ts 55
dist/container.test.d.ts.map 122
dist/datalyr.cjs.js 488251
dist/datalyr.cjs.js.map 281342
dist/datalyr.esm.js 488126
dist/datalyr.esm.js.map 281336
dist/datalyr.esm.min.js 145853
dist/datalyr.esm.min.js.map 185715
dist/datalyr.js 529289
dist/datalyr.js.map 282162
dist/datalyr.min.js 145939
dist/datalyr.min.js.map 185712
dist/datalyr.pixels.min.js 22413
dist/datalyr.pixels.min.js.map 49031
dist/datalyr.replay.min.js 125219
dist/datalyr.replay.min.js.map 640303
dist/destination-pixels.d.ts 4378
dist/destination-pixels.d.ts.map 2090
dist/encryption.d.ts 2046
dist/encryption.d.ts.map 614
dist/fingerprint.d.ts 1325
dist/fingerprint.d.ts.map 584
dist/identify-dedupe.test.d.ts 61
dist/identify-dedupe.test.d.ts.map 153
dist/identity-hydration-race.test.d.ts 69
dist/identity-hydration-race.test.d.ts.map 169
dist/identity-pii.test.d.ts 58
dist/identity-pii.test.d.ts.map 147
dist/identity-provisional.test.d.ts 66
dist/identity-provisional.test.d.ts.map 163
dist/identity.d.ts 9125
dist/identity.d.ts.map 2088
dist/identity.test.d.ts 240
dist/identity.test.d.ts.map 131
dist/in-app-handoff-emit.test.d.ts 65
dist/in-app-handoff-emit.test.d.ts.map 142
dist/in-app-handoff.d.ts 2636
dist/in-app-handoff.d.ts.map 617
dist/in-app-handoff.test.d.ts 60
dist/in-app-handoff.test.d.ts.map 132
dist/index.d.ts 31016
dist/index.d.ts.map 6194
dist/index.test.d.ts 122
dist/index.test.d.ts.map 186
dist/late-config.test.d.ts 454
dist/late-config.test.d.ts.map 161
dist/pixels-id-contract.test.d.ts 64
dist/pixels-id-contract.test.d.ts.map 140
dist/pixels/applovin.d.ts 12095
dist/pixels/applovin.d.ts.map 3520
dist/pixels/index.d.ts 508
dist/pixels/index.d.ts.map 537
dist/pixels/linkedin.d.ts 4958
dist/pixels/linkedin.d.ts.map 1438
dist/pixels/x.d.ts 6049
dist/pixels/x.d.ts.map 1938
dist/queue-403.test.d.ts 55
dist/queue-403.test.d.ts.map 141
dist/queue-identity-order.test.d.ts 536
dist/queue-identity-order.test.d.ts.map 160
dist/queue-offline-backlog.test.d.ts 67
dist/queue-offline-backlog.test.d.ts.map 165
dist/queue.d.ts 10112
dist/queue.d.ts.map 2467
dist/queue.test.d.ts 355
dist/queue.test.d.ts.map 127
dist/replay-heat.test.d.ts 57
dist/replay-heat.test.d.ts.map 126
dist/replay-loader.d.ts 7291
dist/replay-loader.d.ts.map 3520
dist/replay-loader.test.d.ts 59
dist/replay-loader.test.d.ts.map 130
dist/replay-masking-real.test.d.ts 65
dist/replay-masking-real.test.d.ts.map 142
dist/replay-privacy.test.d.ts 60
dist/replay-privacy.test.d.ts.map 132
dist/replay-recorder.test.d.ts 61
dist/replay-recorder.test.d.ts.map 134
dist/replay-sdk.test.d.ts 309
dist/replay-sdk.test.d.ts.map 156
dist/replay-selector.test.d.ts 61
dist/replay-selector.test.d.ts.map 134
dist/replay/heat.d.ts 4002
dist/replay/heat.d.ts.map 3039
dist/replay/privacy.d.ts 3439
dist/replay/privacy.d.ts.map 1095
dist/replay/recorder.d.ts 4191
dist/replay/recorder.d.ts.map 2220
dist/replay/selector.d.ts 1781
dist/replay/selector.d.ts.map 1063
dist/session.d.ts 3401
dist/session.d.ts.map 1463
dist/shopify-cart.d.ts 483
dist/shopify-cart.d.ts.map 195
dist/shopify-consent-container.test.d.ts 382
dist/shopify-consent-container.test.d.ts.map 187
dist/shopify-consent-policy.test.d.ts 619
dist/shopify-consent-policy.test.d.ts.map 186
dist/shopify-product-view.d.ts 1624
dist/shopify-product-view.d.ts.map 461
dist/shopify-product-view.test.d.ts 356
dist/shopify-product-view.test.d.ts.map 178
dist/storage-migration.test.d.ts 63
dist/storage-migration.test.d.ts.map 157
dist/storage.d.ts 3122
dist/storage.d.ts.map 1923
dist/storage.test.d.ts 204
dist/storage.test.d.ts.map 129
dist/stripe-links.test.d.ts 58
dist/stripe-links.test.d.ts.map 128
dist/stripe-session.d.ts 3538
dist/stripe-session.d.ts.map 981
dist/stripe-session.test.d.ts 60
dist/stripe-session.test.d.ts.map 132
dist/tiktok-dedupe.test.d.ts 862
dist/tiktok-dedupe.test.d.ts.map 172
dist/types.d.ts 7592
dist/types.d.ts.map 7259
dist/utils.d.ts 3554
dist/utils.d.ts.map 1452
dist/utils.test.d.ts 165
dist/utils.test.d.ts.map 125
dist/visitor-session-recovery.test.d.ts 503
dist/visitor-session-recovery.test.d.ts.map 168
package.json 2130

Changes to what the SDK sends

None. Entry points only; every build is from the same source and no built file changed.

Not done

  • TypeScript node16/nodenext in an ESM project, default import. dist/index.d.ts sits in a package with no "type", so TypeScript reads it as CommonJS types and the default import is typed as the whole module (datalyr.default.init). Named import import { datalyr } from '@datalyr/web' is typed correctly. Fixing the default import needs an ESM declaration file (index.d.mts) or a .mjs ESM build, which is a build change and outside this item (owner rule: keep the built files exactly as they are). Bundler resolution (Vite, Next.js, webpack) is unaffected.
  • Node ESM on old Node. dist/datalyr.esm.js is a .js file in a package without "type": "module"; Node loads it as ESM through module syntax detection (Node 20.19+ / 22.12+). On older Node, a native import of the package fails with a syntax error. Before this PR it never worked on any Node version (gave {}). A .mjs build would remove this limit; same build-change reason as above.

🤖 Generated with Claude Code


Show it working
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

czypnt and others added 4 commits November 22, 2026 15:38
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@czypnt
czypnt merged commit 7e70181 into main Nov 22, 2026
1 check passed
@czypnt
czypnt deleted the w2-exports branch November 22, 2026 23:14
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.

1 participant