Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ A collection of ES7 async/await utilities. Install via NPM:
npm install asyncbox
```

Published as both ESM and CommonJS, so `import {sleep} from 'asyncbox'` and `const {sleep} = require('asyncbox')` both work.

Then, behold!

### Sleep
Expand Down
33 changes: 26 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,42 @@
"node": "^20.19.0 || ^22.12.0 || >=24.0.0",
"npm": ">=10"
},
"main": "./build/lib/asyncbox.js",
"type": "module",
"main": "./build/cjs/lib/asyncbox.js",
"module": "./build/esm/lib/asyncbox.js",
"types": "./build/cjs/lib/asyncbox.d.ts",
"exports": {
".": {
"import": {
"types": "./build/esm/lib/asyncbox.d.ts",
"default": "./build/esm/lib/asyncbox.js"
},
"require": {
"types": "./build/cjs/lib/asyncbox.d.ts",
"default": "./build/cjs/lib/asyncbox.js"
}
},
"./package.json": "./package.json"
},
"bin": {},
"directories": {
"lib": "./lib"
},
"files": [
"lib/**/*",
"build/lib/**/*"
"build/cjs/lib/**/*",
"build/cjs/package.json",
"build/esm/lib/**/*",
"build/esm/package.json"
],
"scripts": {
"build": "tsc -b",
"clean": "npm run build -- --clean",
"build": "tsc -b tsconfig.esm.json tsconfig.cjs.json",
"postbuild": "node scripts/postbuild.mjs",
"clean": "rm -rf build",
"rebuild": "npm run clean; npm run build",
Comment thread
mykola-mokhnach marked this conversation as resolved.
Outdated
"dev": "npm run build -- --watch",
"prepare": "npm run rebuild",
"test": "node --test --enable-source-maps --test-force-exit --test-timeout=60000 \"./build/test/**/*.spec.js\"",
"test": "node --test --enable-source-maps --test-force-exit --test-timeout=60000 \"./build/esm/test/**/*.spec.js\" \"./test/*.spec.cjs\"",
"lint": "eslint .",
"format": "prettier -w ./lib ./test",
"format:check": "prettier --check ./lib ./test",
Expand All @@ -59,6 +79,5 @@
"prettier": "^3.0.0",
"semantic-release": "^25.0.2",
"typescript": "^6.0.3"
},
"types": "./build/lib/asyncbox.d.ts"
}
}
9 changes: 9 additions & 0 deletions scripts/postbuild.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {mkdirSync, writeFileSync} from 'node:fs';

for (const [dir, type] of [
['build/esm', 'module'],
['build/cjs', 'commonjs'],
]) {
mkdirSync(dir, {recursive: true});
writeFileSync(`${dir}/package.json`, `${JSON.stringify({type}, null, 2)}\n`);
}
11 changes: 11 additions & 0 deletions test/cjs-interop.spec.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const assert = require('node:assert/strict');
const {test} = require('node:test');
// Requires the package by name (rather than a build path) so this exercises the same
// "exports"/"main" resolution real CommonJS consumers go through.
const asyncbox = require('asyncbox');

test('require() interop with the CJS build', async () => {
assert.equal(typeof asyncbox.sleep, 'function');
assert.equal(typeof asyncbox.retry, 'function');
await asyncbox.sleep(1);
});
9 changes: 9 additions & 0 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "build/cjs",
"module": "CommonJS",
"moduleResolution": "Bundler"
}
}
7 changes: 7 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "build/esm"
}
}
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"extends": "@appium/tsconfig/tsconfig.json",
"compilerOptions": {
"strict": true,
"outDir": "build",

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.

Should this be removed? The equivalent node-teen_process PR leaves this line untouched.

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.

Since outDir is set by the extended files, I guess it should be removed from the other PR

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.

asyncbox could drop it because it has no lint:types script — only tsc -b via the esm/cjs configs, which both set their own outDir. teen_process's lint:types needs the base config to still emit somewhere sane, so outDir: "build" here is intentional, not leftover cruft.

"types": ["node"],
"checkJs": true
},
"include": [
"lib",
"test"
]
],
"exclude": ["test/*.cjs"]
}