Skip to content

Commit

Permalink
feat: support esm and cjs both
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Oct 10, 2023
1 parent 1a816ae commit c8c3b2c
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ results
node_modules
npm-debug.log
lib
bun.lockb
.tshy*
dist
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ one-time ready event object.
Create `ready` event object.

```ts
import Ready from 'get-ready';
import { Ready } from 'get-ready';

const obj = new Ready();

Expand Down Expand Up @@ -86,7 +86,6 @@ obj.ready(new Error('err'));
|[<img src="https://avatars.githubusercontent.com/u/221826?v=4" width="100px;"/><br/><sub><b>supershabam</b></sub>](https://github.com/supershabam)<br/>|[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/360661?v=4" width="100px;"/><br/><sub><b>popomore</b></sub>](https://github.com/popomore)<br/>|[<img src="https://avatars.githubusercontent.com/u/985607?v=4" width="100px;"/><br/><sub><b>dead-horse</b></sub>](https://github.com/dead-horse)<br/>|[<img src="https://avatars.githubusercontent.com/u/32174276?v=4" width="100px;"/><br/><sub><b>semantic-release-bot</b></sub>](https://github.com/semantic-release-bot)<br/>|
| :---: | :---: | :---: | :---: | :---: |


This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Mon Jun 05 2023 14:06:50 GMT+0800`.

<!-- GITCONTRIBUTOR_END -->
47 changes: 34 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@
"name": "get-ready",
"version": "3.0.0",
"description": "mixin to add one-time ready event callback handler",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib"
"src",
"dist"
],
"dependencies": {},
"devDependencies": {
"@eggjs/tsconfig": "^1.3.3",
"@types/mocha": "^10.0.1",
"@types/node": "^20.2.5",
"@types/mocha": "^10.0.2",
"@types/node": "^20.8.4",
"egg-bin": "^6.4.1",
"eslint": "^8.42.0",
"eslint-config-egg": "^12.2.1",
"eslint": "^8.51.0",
"eslint-config-egg": "^13.0.0",
"git-contributor": "^2.1.5",
"typescript": "^5.1.3"
"tshy": "^1.2.2",
"tshy-after": "^1.0.0",
"typescript": "^5.2.2"
},
"engines": {
"node": ">= 16.13.0"
},
"scripts": {
"contributor": "git-contributor",
"lint": "eslint .",
"lint": "eslint src test --ext ts",
"test": "npm run lint && egg-bin test",
"ci": "egg-bin cov && npm run tsc",
"ci": "egg-bin cov && npm run prepublishOnly && npm pack",
"clean": "tsc -b --clean",
"tsc": "tsc",
"prepublishOnly": "npm run tsc"
"prepublishOnly": "tshy && tshy-after"
},
"repository": {
"type": "git",
Expand All @@ -44,5 +44,26 @@
"license": "MIT",
"bugs": {
"url": "https://github.com/node-modules/get-ready/issues"
}
},
"tshy": {
"exports": {
"./package.json": "./package.json",
".": "./src/index.ts"
}
},
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
}
}
},
"type": "module",
"types": "./dist/commonjs/index.d.ts"
}
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type CallbackFunction = (err?: Error) => void;
export type ReadyFunctionArg = boolean | Error | CallbackFunction | undefined;

export default class Ready {
export class Ready {
#isReady: boolean;
#readyCallbacks: CallbackFunction[];
#readyArg?: Error = undefined;
Expand Down Expand Up @@ -79,3 +79,5 @@ export default class Ready {
obj.ready = (flagOrFunction: any) => ready.ready(flagOrFunction);
}
}

export default Ready;
38 changes: 37 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { strict as assert } from 'node:assert';
import Ready, { ReadyFunctionArg } from '../src/index';
import Ready, { ReadyFunctionArg, Ready as ReadyBase } from '../src/index.js';

class SomeClass {
property: string;
Expand All @@ -19,6 +19,19 @@ class SomeClass {
}
}

class ReadySubClass extends ReadyBase {
property: string;

constructor() {
super();
this.property = 'value';
}

method() {
return 'method';
}
}

describe('Ready.mixin', () => {
it('should exports mixin', () => {
assert(Ready.mixin);
Expand Down Expand Up @@ -160,6 +173,29 @@ describe('error', () => {
});
});

describe('work on Ready SubClass', () => {
it('should have Ready properties', () => {
const someClass = new ReadySubClass();
assert('ready' in someClass);
assert.equal(typeof someClass.ready, 'function');
});

it('should be separate from other instances', async () => {
const someClass = new ReadySubClass();
const anotherClass = new ReadySubClass();
let someCallCount = 0;
let anotherCallCount = 0;
anotherClass.ready(() => { anotherCallCount++; });
someClass.ready(() => { someCallCount++; });
someClass.ready(() => { someCallCount++; });
someClass.ready(true);
anotherClass.ready(true);
await nextTick();
assert(someCallCount === 2);
assert(anotherCallCount === 1);
});
});

function nextTick() {
return new Promise<void>(resolve => {
process.nextTick(resolve);
Expand Down
13 changes: 3 additions & 10 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@
"extends": "@eggjs/tsconfig",
"compilerOptions": {
"target": "ES2022",
"module": "Node16",
"outDir": "lib",
"module": "nodenext",
"moduleResolution": "nodenext",
"useUnknownInCatchVariables": false
},
"include": [
"src"
],
"exclude": [
"node_modules",
"test"
]
}
}

0 comments on commit c8c3b2c

Please sign in to comment.