Skip to content

Add ESM loader support to @kbn/babel-register with regex-based allowlist and fix package.json exports resolution#71

Draft
Copilot wants to merge 7 commits into
streams-workflowsfrom
copilot/sub-pr-69-again
Draft

Add ESM loader support to @kbn/babel-register with regex-based allowlist and fix package.json exports resolution#71
Copilot wants to merge 7 commits into
streams-workflowsfrom
copilot/sub-pr-69-again

Conversation

Copy link
Copy Markdown

Copilot AI commented Nov 22, 2025

Summary

Adds ESM module support to @kbn/babel-register using Node.js 22's module.register() API, creates a shared regex-based ESM allowlist for configuring which packages use ESM vs CommonJS using Babel's overrides feature, and fixes @kbn/import-resolver to handle { default: string } in package.json exports field.

Changes

@kbn/babel-register

  • Refactored into separate install_cjs.js and install_esm.js modules for better separation of concerns
  • Main install() function now calls both installCJS() and installESM() automatically
  • ESM loader resolves imports by appending .ts|.tsx|.js|.jsx extensions and checking for index files
  • Transforms loaded files with Babel, respecting existing ignore patterns (node_modules except @kbn)
  • Created esm_allowlist.js containing regex patterns for packages that should use ESM (e.g., /kbn-streams-ai-cli/)
// Enable both CJS and ESM transformation
require('@kbn/babel-register').install();

@kbn/babel-preset (node_preset)

  • Uses Babel's overrides feature to apply ESM configuration based on regex pattern matching
  • Shares the ESM allowlist from @kbn/babel-register/esm_allowlist to ensure consistency
  • Automatically configures packages matching allowlist patterns (like @kbn/streams-ai-cli) to output ESM (module: false) instead of CJS (module: 'cjs')
  • Leverages Babel's built-in file matching instead of custom logic for cleaner, more maintainable code

@kbn/import-resolver

  • Fixed tryExportsResolve() to handle package root exports with { ".": { "default": "./dist/index.js" } } pattern
  • When subPathParts is empty, entry is now "." instead of early exit null
  • Enables resolution of packages using modern conditional exports syntax

Checklist

  • Any text added follows EUI's writing guidelines, uses sentence case text and includes i18n support
  • Documentation was added for features that require explanation or tutorials
  • Unit or functional tests were updated or added to match the most common scenarios
  • If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the docker list
  • This was checked for breaking HTTP API changes, and any breaking changes have been approved by the breaking-change committee. The release_note:breaking label should be applied in these situations.
  • Flaky Test Runner was used on any tests changed
  • The PR description includes the appropriate Release Notes section, and the correct release_note:* label is applied per the guidelines
  • Review the backport guidelines and apply applicable backport:* labels.

Identify risks

  • ESM loader requires Node.js 22+ - throws clear error on older versions
  • Import resolver change only affects packages using conditional exports with default field - existing behavior preserved for all other cases
  • ESM allowlist architecture allows gradual migration - packages are explicitly opted in via regex patterns, default behavior unchanged
  • Babel preset changes use standard overrides feature, scoped to allowlisted packages only via regex matching, no impact on existing packages

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

dgieselaar and others added 2 commits November 22, 2025 16:48
* Initial plan

* Implement ink-based CLI with React components

- Created new cli_ink directory with React/ink implementation
- Implemented core components: Header, Menu, Input, LogsDisplay
- Implemented screens: MainMenu, SelectStream, SelectConnector, SetTimeRange, StreamActions, OnboardingMenu, DescribeDataset, ChatWithData, WorkflowResult
- Created utilities for log buffering and time range management
- Integrated all workflows (partition, onboarding, etc.)
- Added global key handlers: 'l' for logs, 'q' for back, CMD+C for clean exit
- Updated main index.ts to use new ink implementation

Co-authored-by: dgieselaar <352732+dgieselaar@users.noreply.github.com>

* Add AnalyzeStream screen and update documentation

- Created AnalyzeStream screen (placeholder for future implementation)
- Wire partition-stream action to workflow execution
- Updated README with comprehensive documentation
- Added navigation flow diagram and usage instructions

Co-authored-by: dgieselaar <352732+dgieselaar@users.noreply.github.com>

* Fix code review issues

- Add missing workflows: description and field-definitions
- Add error handling for undefined workflows
- Fix custom time range handling in computeTimeRangeBounds
- Add ESC key support for Input component
- Update help text to clarify 'q' behavior in input fields
- Properly propagate custom time ranges through state

Co-authored-by: dgieselaar <352732+dgieselaar@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: dgieselaar <352732+dgieselaar@users.noreply.github.com>
Copilot AI and others added 2 commits November 22, 2025 23:14
…xports handling

Co-authored-by: dgieselaar <352732+dgieselaar@users.noreply.github.com>
Co-authored-by: dgieselaar <352732+dgieselaar@users.noreply.github.com>
Copy link
Copy Markdown
Author

Copilot AI commented Nov 22, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • ci-stats.kibana.dev
    • Triggering command: /home/REDACTED/.nvm/versions/node/v22.17.1/bin/node /home/REDACTED/.nvm/versions/node/v22.17.1/bin/node --no-experimental-require-module scripts/kbn bootstrap --quiet (dns block)
    • Triggering command: /home/REDACTED/.nvm/versions/node/v22.17.1/bin/node /home/REDACTED/.nvm/versions/node/v22.17.1/bin/node --no-experimental-require-module scripts/kbn bootstrap (dns block)
    • Triggering command: /home/REDACTED/.nvm/versions/node/v22.17.1/bin/node node scripts/eslint --fix src/platform/packages/private/kbn-import-resolver/src/import_resolver.ts src/platform/packages/shared/kbn-babel-register/index.js src/platform/packages/shared/kbn-babel-register/esm_loader.js (dns block)
  • download.cypress.io
    • Triggering command: /home/REDACTED/.nvm/versions/node/v22.17.1/bin/node /home/REDACTED/.nvm/versions/node/v22.17.1/bin/node index.js --exec install (dns block)
  • googlechromelabs.github.io
    • Triggering command: /home/REDACTED/.nvm/versions/node/v22.17.1/bin/node /home/REDACTED/.nvm/versions/node/v22.17.1/bin/node install.js (dns block)
  • iojs.org
    • Triggering command: /usr/bin/curl curl -q --fail --compressed -L -s REDACTED -o - (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title [WIP] Rewrite streams-ai-cli using React ink Add ESM loader support to @kbn/babel-register and fix package.json exports resolution Nov 22, 2025
Copilot AI requested a review from dgieselaar November 22, 2025 23:18
Copy link
Copy Markdown
Owner

@dgieselaar dgieselaar left a comment

Choose a reason for hiding this comment

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

@copilot:

  • create two separate files, install_esm.js and install_cjs.js
  • make sure kbn-babel-register exports only install(), which calls both installCJS and installESM
  • do not add READMEs or tests for existing functionality. Only add tests for new changes. Undo any changes in this PR that do not meet these requirements
  • in node_preset, which contains the Babel config, add an override that sets module: false instead of module: cjs, based on an includelist. Add @kbn/streams-ai-cli to this list, this package should use ESM and not CJS.
  • consider whether this allowlist should be shared with kbn-babel-register. If so, figure out the right way to do this, without copying files.

…allowlist

- Split install logic into install_cjs.js and install_esm.js
- Main index.js now exports only install() which calls both installers
- Created esm_allowlist.js with @kbn/streams-ai-cli
- Updated node_preset.js to use module: false for allowlisted packages
- Removed unnecessary README updates and tests for existing functionality
- Kept only tests for new ESM functionality (esm_loader.test.js)

Co-authored-by: dgieselaar <352732+dgieselaar@users.noreply.github.com>
Copy link
Copy Markdown
Author

Copilot AI commented Nov 23, 2025

@copilot:

  • create two separate files, install_esm.js and install_cjs.js
  • make sure kbn-babel-register exports only install(), which calls both installCJS and installESM
    ...

Completed all requested changes in commit 5319e20:

Structural changes:

  • Created install_cjs.js and install_esm.js with separate installation logic
  • Updated index.js to export only install() which calls both installCJS() and installESM()

ESM allowlist:

  • Created esm_allowlist.js with @kbn/streams-ai-cli in the list
  • Updated node_preset.js to check the allowlist and set module: false (instead of module: 'cjs') for packages in the list
  • The allowlist is shared between kbn-babel-register and kbn-babel-preset via require

Cleanup:

  • Removed USAGE_EXAMPLES.md and index.test.js (tests for existing functionality)
  • Removed import_resolver.exports.test.ts (test for existing functionality)
  • Reverted README changes to original state
  • Kept only esm_loader.test.js (tests for new ESM loader functionality)

Copilot AI changed the title Add ESM loader support to @kbn/babel-register and fix package.json exports resolution Add ESM loader support to @kbn/babel-register with allowlist and fix package.json exports resolution Nov 23, 2025
Copilot AI requested a review from dgieselaar November 23, 2025 03:55
Copy link
Copy Markdown
Owner

@dgieselaar dgieselaar left a comment

Choose a reason for hiding this comment

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

@copilot Use regexes (e.g. /kbn-streams-ai-cli/) and overrides for the ESM allowlist.

@dgieselaar
Copy link
Copy Markdown
Owner

@copilot Use regexes (e.g. /kbn-streams-ai-cli/) and overrides for the ESM allowlist.

- Changed ESM_ALLOWLIST to use regex patterns (e.g., /kbn-streams-ai-cli/)
- Updated node_preset.js to use Babel's overrides feature instead of manual filename checking
- Simplified logic by leveraging Babel's built-in test matching with regex patterns
- Removed custom isInEsmAllowlist function and Path dependency

Co-authored-by: dgieselaar <352732+dgieselaar@users.noreply.github.com>
Copy link
Copy Markdown
Author

Copilot AI commented Nov 23, 2025

@copilot Use regexes (e.g. /kbn-streams-ai-cli/) and overrides for the ESM allowlist.

Implemented in commit c55b165:

  • Changed ESM_ALLOWLIST to use regex patterns: /kbn-streams-ai-cli/
  • Updated node_preset.js to use Babel's overrides feature with the regex patterns as the test property
  • Removed custom filename checking logic - now leveraging Babel's built-in pattern matching

@copilot Use regexes (e.g. /kbn-streams-ai-cli/) and overrides for the ESM allowlist.

Implemented in commit c55b165:

  • Changed ESM_ALLOWLIST to use regex patterns: /kbn-streams-ai-cli/
  • Updated node_preset.js to use Babel's overrides feature with the regex patterns as the test property
  • Removed custom filename checking logic - now leveraging Babel's built-in pattern matching

Copilot AI changed the title Add ESM loader support to @kbn/babel-register with allowlist and fix package.json exports resolution Add ESM loader support to @kbn/babel-register with regex-based allowlist and fix package.json exports resolution Nov 23, 2025
Copilot AI requested a review from dgieselaar November 23, 2025 09:18
@dgieselaar dgieselaar force-pushed the streams-workflows branch 2 times, most recently from 328b855 to da68802 Compare December 3, 2025 09:55
@dgieselaar dgieselaar force-pushed the streams-workflows branch 2 times, most recently from 6d69934 to 663895e Compare December 7, 2025 09:29
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.

2 participants