Skip to content

build: Dual export ESM and CJS#16

Merged
sddonne merged 9 commits into
mainfrom
fix-distributable
Feb 20, 2026
Merged

build: Dual export ESM and CJS#16
sddonne merged 9 commits into
mainfrom
fix-distributable

Conversation

@sddonne
Copy link
Copy Markdown
Contributor

@sddonne sddonne commented Feb 20, 2026

Part of elastic/kibana#250514

Summary

After testing the installation of the package in Kibana here there are some changes in how the distributable is built so the package can be easily consumed.

Details

  • We are now distributing an ESM version and a CJS version. The ESM version will be used by bundlers (i.e. Webpack in Kibana) and will allow to do an efficient tree shaking for code running in the browser.

    The CJS version will be used by Node applications (i.e. Kibana web server) that cannot handle ESM directly.

    (Which version to use is decided automatically by the client, when using require or import, they check the export rules to know which one to use)

"exports": {
    ".": {
      "types": "./lib/esm/index.d.ts",
      "import": "./lib/esm/index.js",
      "require": "./lib/cjs/index.js",
      "default": "./lib/cjs/index.js"
    }
  },
  • @babel/runtime has been added as a dev dependency as it's needed to properly work with package using yarn link.

@sddonne sddonne changed the title build: fix distributable build: Dual export ESM and CJS Feb 20, 2026
@sddonne sddonne marked this pull request as ready for review February 20, 2026 10:09
@sddonne sddonne requested a review from a team as a code owner February 20, 2026 10:09
Comment thread src/ast/builder/builder.test.ts Outdated

import { Builder } from '.';
import { BasicPrettyPrinter } from '../../pretty_print';
import { Builder } from './index.ts';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this at EC, how are they doing this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They only export CJS, having the extensions is a requirement for exporting ESM, which is more modern and allows better tree shaking, I think having the extensions is a fare price to pay for having that.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like it at all tbh. I am pretty sure there are other ways to fix it. From a quick search I found some ideas. Let's do a research first to solve this because I personally find it very ugly and the differentiation from kibana is also bad

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was in doubt about adding an extra tool in the building phase, but I'm glad we do this, added tsup to transpile the code so we can keep using the way of importing Kibana has.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great honestly

Copy link
Copy Markdown
Contributor

@stratoula stratoula left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

@sddonne sddonne merged commit f60ba75 into main Feb 20, 2026
3 checks passed
@sddonne sddonne deleted the fix-distributable branch February 26, 2026 10:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants