Skip to content

fix: add ESM exports to support consistent CJS interop in Vite/Rolldown#95

Open
neisukee wants to merge 1 commit intocodler:masterfrom
neisukee:fix/esm-exports
Open

fix: add ESM exports to support consistent CJS interop in Vite/Rolldown#95
neisukee wants to merge 1 commit intocodler:masterfrom
neisukee:fix/esm-exports

Conversation

@neisukee
Copy link

Problem

With the introduction of consistent CJS interop in Vite (powered by Rolldown),
projects using "type": "module" receive module.exports as the default import
instead of module.exports.default.

This causes react-ga4's default import to return the entire exports object
rather than the GA4 instance, breaking ga4.initialize() and other methods.

See: https://rolldown.rs/in-depth/bundling-cjs#ambiguous-default-import-from-cjs-modules

Solution

Add an ESM entry point via the exports field in package.json,
so bundlers can resolve the proper ESM version directly.

Changes

  • Modified babel.config.js to support ESM build (modules: false when BABEL_ENV=esm)
  • Added .js extensions to relative imports in src/ for Node.js ESM compatibility
  • CJS build output moved to dist/cjs/
  • ESM build output added at dist/esm/
  • Added exports, module fields to package.json

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