From 684618ff5553862099519fc7e54a78a9d8c80991 Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Thu, 12 Nov 2026 13:55:04 -0500 Subject: [PATCH 1/2] Convert to a TypeScript project, ship ESM only Source is now src/diff3.ts with its types inline, replacing the hand-written src/diff3.d.ts. Declarations are generated by tsc into dist/ts/ under isolatedDeclarations; Bun.build writes the ESM and IIFE bundles into dist/js/. - Replace `any` with generics; inputs accept readonly arrays - Add IStrippedPatchRes so stripPatch() output type-checks with patch() - Make MergeRegion a discriminated union of ok and conflict regions - Drop the CJS build; require() loads the ESM build (Node 20.19+/22.12+) - Add a `bun` export condition pointing at the TypeScript source - Convert tests to TypeScript with whole-result assertions - Add tsconfig.build.json and a check:types script BREAKING CHANGE: CommonJS build removed; the IIFE moves to dist/js/diff3.iife.js; MergeRegion is now a type alias union. Co-Authored-By: Claude Opus 5.5 --- .claude/skills/suggest/SKILL.md | 2 +- README.md | 34 +- package.json | 14 +- scripts/build_js.ts | 15 +- src/diff3.d.ts | 188 -------- src/{diff3.mjs => diff3.ts} | 430 ++++++++++++------ src/index.ts | 7 +- test/{LCS.test.js => LCS.test.ts} | 64 ++- ...{diff3Merge.test.js => diff3Merge.test.ts} | 112 ++--- test/diff3MergeRegions.test.js | 67 --- test/diff3MergeRegions.test.ts | 35 ++ test/diffComm.test.js | 87 ---- test/diffComm.test.ts | 43 ++ test/diffIndices.test.js | 29 -- test/diffIndices.test.ts | 20 + test/diffPatch.test.js | 139 ------ test/diffPatch.test.ts | 112 +++++ test/{merge.test.js => merge.test.ts} | 4 +- ...{mergeDiff3.test.js => mergeDiff3.test.ts} | 4 +- ...{mergeDigIn.test.js => mergeDigIn.test.ts} | 4 +- tsconfig.build.json | 12 + tsconfig.json | 29 +- 22 files changed, 640 insertions(+), 811 deletions(-) delete mode 100644 src/diff3.d.ts rename src/{diff3.mjs => diff3.ts} (57%) rename test/{LCS.test.js => LCS.test.ts} (62%) rename test/{diff3Merge.test.js => diff3Merge.test.ts} (54%) delete mode 100644 test/diff3MergeRegions.test.js create mode 100644 test/diff3MergeRegions.test.ts delete mode 100644 test/diffComm.test.js create mode 100644 test/diffComm.test.ts delete mode 100644 test/diffIndices.test.js create mode 100644 test/diffIndices.test.ts delete mode 100644 test/diffPatch.test.js create mode 100644 test/diffPatch.test.ts rename test/{merge.test.js => merge.test.ts} (96%) rename test/{mergeDiff3.test.js => mergeDiff3.test.ts} (95%) rename test/{mergeDigIn.test.js => mergeDigIn.test.ts} (91%) create mode 100644 tsconfig.build.json diff --git a/.claude/skills/suggest/SKILL.md b/.claude/skills/suggest/SKILL.md index f757f8a..be81651 100644 --- a/.claude/skills/suggest/SKILL.md +++ b/.claude/skills/suggest/SKILL.md @@ -36,7 +36,7 @@ Skip categories where things look fine. Don't invent problems. **Linting** — `.oxlintrc.json` rules that are stale, redundant, or missing for the code in `scripts/`, `src/`, and `test/` -**Packaging** — `package.json` `exports` conditions, `files`, and `types` that don't match what the build in `dist/` actually produces (ESM, CJS, IIFE, declarations) +**Packaging** — `package.json` `exports` conditions, `files`, and `types` that don't match what the build in `dist/` actually produces (ESM, IIFE, declarations) **Developer Experience** — missing or misleading `package.json` scripts, incomplete `.gitignore`/`.gitattributes`, missing docs or source comments diff --git a/README.md b/README.md index 3513857..d8b2552 100644 --- a/README.md +++ b/README.md @@ -22,17 +22,15 @@ To install node-diff3 as a dependency in your project: $ npm install --save node-diff3 ``` -**node-diff3** is distributed in CJS and ESM module formats for maxmimum compatibility. ([Read more about Javascript module formats](https://dev.to/iggredible/what-the-heck-are-cjs-amd-umd-and-esm-ikm)) - +**node-diff3** is written in TypeScript and distributed as an ESM module, with type declarations included. ([Read more about Javascript module formats](https://dev.to/iggredible/what-the-heck-are-cjs-amd-umd-and-esm-ikm)) ```js -const Diff3 = require('node-diff3'); // CommonJS import all -const diff3Merge = require('node-diff3').diff3Merge; // CommonJS import named -// or import * as Diff3 from 'node-diff3'; // ESM import all import { diff3Merge } from 'node-diff3'; // ESM import named ``` +CommonJS code can still `require('node-diff3')` on Node 20.19+ or 22.12+, which can load ESM modules with `require()`. + ### Use in Browsers You can also use **node-diff3** directly in a web browser. A good way to do this is to fetch the ["iife"](https://esbuild.github.io/api/#format-iife) bundle from the [jsDelivr CDN](https://www.jsdelivr.com/), which can even deliver minified versions. @@ -40,7 +38,7 @@ You can also use **node-diff3** directly in a web browser. A good way to do thi When you load this file in a ` + …