From 06affb3c5bf81d80691de7471dce8139f8d6a7ad Mon Sep 17 00:00:00 2001 From: Ankur Dubey Date: Sat, 24 Oct 2026 17:01:56 +0530 Subject: [PATCH] feat: ship a CommonJS build alongside ESM --- CHANGELOG.md | 2 +- README.md | 2 +- package.json | 19 +++++++++++++++---- scripts/mark-cjs.ts | 5 +++++ tsconfig.cjs.json | 9 +++++++++ 5 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 scripts/mark-cjs.ts create mode 100644 tsconfig.cjs.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 8467efa..3dc1f06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `createTimeSlicer` for loops that don't fit a map, such as `while` loops, tree walks and nested loops. - `yieldToEventLoop`, which yields with the best method the runtime has: `scheduler.yield()`, then `setImmediate`, then `MessageChannel`, then `setTimeout`. - `AbortSignal` support to stop a loop early. -- TypeScript types. Runs in Node.js 22+, Bun, Deno, browsers and Electron. +- ESM and CommonJS builds, each with TypeScript types, so both `import` and `require` work. Runs in Node.js 22+, Bun, Deno, browsers and Electron. [unreleased]: https://github.com/RagedPanda/time-sliced/commits/main diff --git a/README.md b/README.md index 4f14527..e7836d2 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ await timeSlicedForEach(rows, processRow); npm install time-sliced ``` -No dependencies, with TypeScript types. Runs in Node.js 22+, Bun, Deno, browsers and Electron (main and renderer). +No dependencies. Ships ESM and CommonJS with types for both, so `import` and `require` both work. Runs in Node.js 22+, Bun, Deno, browsers and Electron (main and renderer). ## Benchmark diff --git a/package.json b/package.json index 7aab15a..3a9f261 100644 --- a/package.json +++ b/package.json @@ -4,12 +4,23 @@ "description": "Run long loops without freezing the event loop: yield on a time budget, not every N items", "license": "MIT", "author": "Ankur Dubey (https://github.com/RagedPanda)", - "repository": "github:RagedPanda/time-sliced", + "repository": { + "type": "git", + "url": "git+https://github.com/RagedPanda/time-sliced.git" + }, "type": "module", + "main": "./dist/cjs/index.js", + "types": "./dist/cjs/index.d.ts", "exports": { ".": { - "types": "./dist/esm/index.d.ts", - "default": "./dist/esm/index.js" + "import": { + "types": "./dist/esm/index.d.ts", + "default": "./dist/esm/index.js" + }, + "require": { + "types": "./dist/cjs/index.d.ts", + "default": "./dist/cjs/index.js" + } } }, "files": [ @@ -20,7 +31,7 @@ "node": ">=22" }, "scripts": { - "build": "node -e \"fs.rmSync('dist', {recursive: true, force: true})\" && tsc -p tsconfig.esm.json", + "build": "node -e \"fs.rmSync('dist', {recursive: true, force: true})\" && tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json && node scripts/mark-cjs.ts", "typecheck": "tsc --noEmit", "test": "node --test \"test/**/*.test.ts\"", "bench": "node bench/event-loop-lag.ts", diff --git a/scripts/mark-cjs.ts b/scripts/mark-cjs.ts new file mode 100644 index 0000000..e35bad5 --- /dev/null +++ b/scripts/mark-cjs.ts @@ -0,0 +1,5 @@ +// The package is "type": "module", so Node.js would read dist/cjs/*.js as ES modules. +// This nested package.json makes it read them, and their .d.ts files, as CommonJS. +import {writeFileSync} from 'node:fs'; + +writeFileSync(new URL('../dist/cjs/package.json', import.meta.url), `${JSON.stringify({type: 'commonjs'}, undefined, '\t')}\n`); diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json new file mode 100644 index 0000000..d9b8fc1 --- /dev/null +++ b/tsconfig.cjs.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.esm.json", + "compilerOptions": { + "module": "commonjs", + "moduleResolution": "bundler", + "verbatimModuleSyntax": false, + "outDir": "dist/cjs" + } +}