Skip to content

Build RexReplace as an ES module and as CommonJS to close #862 - #863

Merged
mathiasrw merged 19 commits into
mainfrom
fix/862
Oct 27, 2026
Merged

mathiasrw merged 19 commits into
mainfrom
fix/862

Conversation

@mathiasrw

@mathiasrw mathiasrw commented Oct 27, 2026 •

Copy link
Copy Markdown
Owner

Closes #862

Stacked on #861.

Change

  • The package is an ES module ("type": "module"), and the CLI is built as one. So regex, regex/internals and regex-utilities are imported when RexReplace runs, as normal dependencies, and are no longer bundled in. Only the subroutines of Regex+ stay copied in src/libs, as they are not exported.
  • The engine is built as a library in both forms, dist/engine.js and dist/engine.cjs, with an exports map. So import {engine} from 'rexreplace' and require('rexreplace') both work. Before, main pointed to src/engine.js, which does not exist.
  • require and r() in --js code come from createRequire() in the ES module builds, as an ES module has no require, and from require in the CommonJS build, as Bun fixes import.meta.url there to the path of the build machine. They find modules as before.
  • yargs is loaded with createRequire(), as CommonJS, as its ES module breaks the lines of the help in the middle of words, which would also reach the option table of the README.
  • npm/sync-version.js is npm/sync-version.cjs, as it is CommonJS.
  • exports also has ./bin/*, so the rexreplace-bun package can still load the CLI of Bun.
Bundle of the CLI
main 52 KB
#855 to #861 110 KB
This pull request 69 KB

Tests

yarn test-ci passes, with the CLI tests on Node and on Bun, which install the package. By hand: an atomic group, require("path") and r("os") in --js code on Node and on Bun, -v, -h and an unknown flag, and the engine from an installed package with require('rexreplace') and with import.

After the review of 28 October

  • The help breaks lines at words again, as yargs is loaded as CommonJS.
  • dist/engine.cjs has no path of the build machine, so require() in --js code finds packages through require('rexreplace') on any machine. Checked from an installed package with require("regex"), through require and import.

From the code review of #855

At the end of the train, as #860 and #861 change the same lines:

  • A group named like a cousin, like a_, wins over the space cousin of a in any order: the cousins are set first, the groups after.
  • The help says that under the flag n only the --js code has no $1, while a replacement text still reads $1 as the first named group. The same line of the README is in Add the rr object, pad() and print() to --js to close #856 #861, which changes that row too.

@mathiasrw
mathiasrw added this pull request to stack #864 October 27, 2026 13:33
Base automatically changed from fix/856-rr to main October 27, 2026 15:56
@mathiasrw
mathiasrw merged commit e25ee56 into main Oct 27, 2026
6 checks passed
@mathiasrw
mathiasrw deleted the fix/862 branch October 28, 2026 16:50
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.

Build RexReplace as an ES module and as CommonJS

1 participant