From c8c3b2c0c257e5912066ea84f76d5487dcd4cb15 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Tue, 10 Oct 2023 23:42:56 +0800 Subject: [PATCH] feat: support esm and cjs both https://github.com/eggjs/egg/issues/5257 --- .gitignore | 3 +++ README.md | 3 +-- package.json | 47 +++++++++++++++++++++++++++++++++------------- src/index.ts | 4 +++- test/index.test.ts | 38 ++++++++++++++++++++++++++++++++++++- tsconfig.json | 13 +++---------- 6 files changed, 81 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 7bac85d..7333397 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ results node_modules npm-debug.log lib +bun.lockb +.tshy* +dist diff --git a/README.md b/README.md index ad0446b..7b47975 100644 --- a/README.md +++ b/README.md @@ -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(); @@ -86,7 +86,6 @@ obj.ready(new Error('err')); |[
supershabam](https://github.com/supershabam)
|[
fengmk2](https://github.com/fengmk2)
|[
popomore](https://github.com/popomore)
|[
dead-horse](https://github.com/dead-horse)
|[
semantic-release-bot](https://github.com/semantic-release-bot)
| | :---: | :---: | :---: | :---: | :---: | - 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`. diff --git a/package.json b/package.json index 72cd0e1..d812704 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } diff --git a/src/index.ts b/src/index.ts index b1c116a..7db2637 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; @@ -79,3 +79,5 @@ export default class Ready { obj.ready = (flagOrFunction: any) => ready.ready(flagOrFunction); } } + +export default Ready; diff --git a/test/index.test.ts b/test/index.test.ts index 723cca8..234793e 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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; @@ -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); @@ -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(resolve => { process.nextTick(resolve); diff --git a/tsconfig.json b/tsconfig.json index 1e5143c..294cc3b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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" - ] + } }