From 3688cd67dbb75304016220ec4f882a56dcc8d38f Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sat, 25 Nov 2023 15:05:07 +0800 Subject: [PATCH] feat: use tshy to build and support ESM and CommonJS both (#22) --- .github/workflows/ci.yml | 4 +- .github/workflows/codeql.yml | 2 - .github/workflows/release.yml | 2 +- .gitignore | 1 + CHANGELOG.md | 77 +++++++++++++++++++++++ History.md | 74 ---------------------- benchmark/{is.date.js => is.date.cjs} | 7 +-- benchmark/{is.error.js => is.error.cjs} | 4 +- benchmark/{is.regexp.js => is.regexp.cjs} | 4 +- package.json | 48 +++++++++----- src/index.ts | 18 +++--- src/is.ts | 8 +-- src/types/external.ts | 6 +- src/types/node.ts | 2 +- src/types/std.ts | 4 +- test/index.test.ts | 16 ++--- test/is.test.ts | 12 ++-- test/types/external.test.ts | 5 +- test/types/node.test.ts | 2 +- test/types/primitive.test.ts | 5 +- test/types/std.test.ts | 33 +++++++--- tsconfig.json | 11 ++-- tsconfig.prod.json | 8 --- 23 files changed, 186 insertions(+), 167 deletions(-) delete mode 100644 History.md rename benchmark/{is.date.js => is.date.cjs} (95%) rename benchmark/{is.error.js => is.error.cjs} (99%) rename benchmark/{is.regexp.js => is.regexp.cjs} (99%) delete mode 100644 tsconfig.prod.json diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c4ab048..58f05ed 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,12 +7,10 @@ on: pull_request: branches: [ master ] - workflow_dispatch: {} - jobs: Job: name: Node.js - uses: artusjs/github-actions/.github/workflows/node-test.yml@v1 + uses: node-modules/github-actions/.github/workflows/node-test.yml@master with: os: 'ubuntu-latest' version: '16, 18, 20' diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 6266bcd..028b997 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -5,8 +5,6 @@ on: branches: [ "master" ] pull_request: branches: [ "master" ] - schedule: - - cron: "15 23 * * 3" jobs: analyze: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1612587..64a499b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -9,7 +9,7 @@ on: jobs: release: name: Node.js - uses: artusjs/github-actions/.github/workflows/node-release.yml@v1 + uses: node-modules/github-actions/.github/workflows/node-release.yml@master secrets: NPM_TOKEN: ${{ secrets.NPM_TOKEN }} GIT_TOKEN: ${{ secrets.GIT_TOKEN }} diff --git a/.gitignore b/.gitignore index 2f7ff39..7320369 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ npm-debug.log node_modules .nyc_output dist +.tshy diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b4eb94..d0a8332 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,3 +32,80 @@ ### Features * add d.ts support ([#17](https://github.com/node-modules/is-type-of/issues/17)) ([edcedcc](https://github.com/node-modules/is-type-of/commit/edcedcc70af64c054cc834e5dd2ed79edd4692ff)) + +--- + + +1.2.1 / 2018-09-22 +================== + +**fixes** + * [[`1468e4e`](http://github.com/node-modules/is-type-of/commit/1468e4e2d2a1221ff25187f430e5546db2fadacc)] - fix: hotfix is-class bug (#10) (fengmk2 <>) + +**others** + * [[`32aab2b`](http://github.com/node-modules/is-type-of/commit/32aab2b5431e76529f2cf9ddf1d112b99ed05394)] - chore: Improve some functions (#8) (Runrioter Wung <>) + +1.2.0 / 2017-08-10 +================== + + * feat: Add async function test function. (#6) + +1.1.0 / 2017-07-19 +================== + + * deps: upgrade is-class@0.0.4 + * feat: is.array() use Array.isArray() directly + * feat: improve is.error performance + * feat: improve is.regExp performance + * test: support coverage + * feat: imrpove is.date performance (#4) + +1.0.0 / 2015-11-09 +================== + + * deps: update all dependencies + * detect class + +0.3.1 / 2014-06-20 +================== + + * add is.finite() + +0.3.0 / 2014-05-10 +================== + + * split Long from long + * fix history + +0.2.1 / 2014-04-22 +================== + + * bump deps version + * Merge pull request #1 from node-modules/support-long-module + * is.long() support long module + * refactor + +0.2.0 / 2014-04-07 +================== + + * add readme for isstream + * add isStream + * add travis-yml + +0.1.0 / 2014-04-03 +================== + + * add isNaN + +0.0.2 / 2014-04-01 +================== + + * add is.int32 + +0.0.1 / 2014-03-12 +================== + + * add readme + * add test, add number type checker + * add core-util-is and generator promise + * Initial commit diff --git a/History.md b/History.md deleted file mode 100644 index 0832aae..0000000 --- a/History.md +++ /dev/null @@ -1,74 +0,0 @@ - -1.2.1 / 2018-09-22 -================== - -**fixes** - * [[`1468e4e`](http://github.com/node-modules/is-type-of/commit/1468e4e2d2a1221ff25187f430e5546db2fadacc)] - fix: hotfix is-class bug (#10) (fengmk2 <>) - -**others** - * [[`32aab2b`](http://github.com/node-modules/is-type-of/commit/32aab2b5431e76529f2cf9ddf1d112b99ed05394)] - chore: Improve some functions (#8) (Runrioter Wung <>) - -1.2.0 / 2017-08-10 -================== - - * feat: Add async function test function. (#6) - -1.1.0 / 2017-07-19 -================== - - * deps: upgrade is-class@0.0.4 - * feat: is.array() use Array.isArray() directly - * feat: improve is.error performance - * feat: improve is.regExp performance - * test: support coverage - * feat: imrpove is.date performance (#4) - -1.0.0 / 2015-11-09 -================== - - * deps: update all dependencies - * detect class - -0.3.1 / 2014-06-20 -================== - - * add is.finite() - -0.3.0 / 2014-05-10 -================== - - * split Long from long - * fix history - -0.2.1 / 2014-04-22 -================== - - * bump deps version - * Merge pull request #1 from node-modules/support-long-module - * is.long() support long module - * refactor - -0.2.0 / 2014-04-07 -================== - - * add readme for isstream - * add isStream - * add travis-yml - -0.1.0 / 2014-04-03 -================== - - * add isNaN - -0.0.2 / 2014-04-01 -================== - - * add is.int32 - -0.0.1 / 2014-03-12 -================== - - * add readme - * add test, add number type checker - * add core-util-is and generator promise - * Initial commit diff --git a/benchmark/is.date.js b/benchmark/is.date.cjs similarity index 95% rename from benchmark/is.date.js rename to benchmark/is.date.cjs index 5d6dd9a..18fd1b3 100644 --- a/benchmark/is.date.js +++ b/benchmark/is.date.cjs @@ -1,16 +1,15 @@ -'use strict'; - const path = require('path'); const fs = require('fs'); const Benchmark = require('benchmark'); const benchmarks = require('beautify-benchmark'); const t = require('core-util-is'); -const is = require('..'); +const is = require('is-type-of'); +// console.log(require.resolve('is-type-of')); const nowDate = new Date(); const nowNumber = Date.now(); const shortString = '一点点点中文abc'; -const longString = fs.readFileSync(path.join(__dirname, '../index.js'), 'utf8'); +const longString = fs.readFileSync(path.join(__dirname, '../package.json'), 'utf8'); const smallBuffer = Buffer.alloc(100); const bigBuffer = Buffer.alloc(1024 * 1024); diff --git a/benchmark/is.error.js b/benchmark/is.error.cjs similarity index 99% rename from benchmark/is.error.js rename to benchmark/is.error.cjs index 4977028..82778c3 100644 --- a/benchmark/is.error.js +++ b/benchmark/is.error.cjs @@ -1,11 +1,9 @@ -'use strict'; - const path = require('path'); const fs = require('fs'); const Benchmark = require('benchmark'); const benchmarks = require('beautify-benchmark'); const t = require('core-util-is'); -const is = require('..'); +const is = require('is-type-of'); const nowDate = new Date(); const nowNumber = Date.now(); diff --git a/benchmark/is.regexp.js b/benchmark/is.regexp.cjs similarity index 99% rename from benchmark/is.regexp.js rename to benchmark/is.regexp.cjs index 7c89b81..1f58f56 100644 --- a/benchmark/is.regexp.js +++ b/benchmark/is.regexp.cjs @@ -1,11 +1,9 @@ -'use strict'; - const path = require('path'); const fs = require('fs'); const Benchmark = require('benchmark'); const benchmarks = require('beautify-benchmark'); const t = require('core-util-is'); -const is = require('..'); +const is = require('is-type-of'); const nowDate = new Date(); const nowNumber = Date.now(); diff --git a/package.json b/package.json index b721013..5f6b76f 100644 --- a/package.json +++ b/package.json @@ -2,18 +2,34 @@ "name": "is-type-of", "version": "2.0.1", "description": "complete type checking for node", - "main": "dist/index.js", - "typings": "dist/index.d.ts", + "type": "module", + "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" + } + } + }, "files": [ - "dist" + "dist", + "src" ], "scripts": { - "prepublishOnly": "npm run tsc:clean && npm run tsc:prod", - "tsc": "tsc -b ./tsconfig.json", - "tsc:prod": "tsc -b ./tsconfig.prod.json", - "tsc:clean": "tsc -b --clean", + "prepublishOnly": "tshy && tshy-after", "test": "egg-bin test", - "ci": "npm run lint && egg-bin cov", + "ci": "npm run lint && egg-bin cov && npm run prepublishOnly", "lint": "eslint . --ext .ts" }, "repository": { @@ -38,15 +54,19 @@ "devDependencies": { "@eggjs/tsconfig": "^1.3.3", "@types/mocha": "^10.0.1", - "@types/node": "^18.16.3", + "@types/node": "^20.6.1", "beautify-benchmark": "^0.2.4", "benchmark": "^2.1.4", - "egg-bin": "^6.4.0", - "eslint": "^8.45.0", - "eslint-config-egg": "^12.2.1", + "core-util-is": "^1.0.3", + "egg-bin": "^6.5.2", + "eslint": "^8.49.0", + "eslint-config-egg": "^13.0.0", "long": "^3.2.0", "semver": "^5.4.1", "ts-expect": "^1.3.0", - "typescript": "^5.1.6" - } + "tshy": "^1.0.0", + "tshy-after": "^1.0.0", + "typescript": "^5.2.2" + }, + "types": "./dist/commonjs/index.d.ts" } diff --git a/src/index.ts b/src/index.ts index 5f031c7..486b2e6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1,12 @@ -import { is } from './is'; -import { isNull, isUndefined, isNaN } from './types/primitive'; -import { isClass, isFunction } from './types/std'; - -export * from './types/primitive'; -export * from './types/std'; -export * from './types/node'; -export * from './types/external'; -export { isInstanceOf } from './types/util'; +import { is } from './is.js'; +import { isNull, isUndefined, isNaN } from './types/primitive.js'; +import { isClass, isFunction } from './types/std.js'; + +export * from './types/primitive.js'; +export * from './types/std.js'; +export * from './types/node.js'; +export * from './types/external.js'; +export { isInstanceOf } from './types/util.js'; export default is; diff --git a/src/is.ts b/src/is.ts index 9a66c68..9e72811 100644 --- a/src/is.ts +++ b/src/is.ts @@ -15,7 +15,7 @@ import { isLong, isDouble, isSafeInteger, -} from './types/primitive'; +} from './types/primitive.js'; import { isArray, isFunction, @@ -30,15 +30,15 @@ import { isObject, isGenerator, isPromise, -} from './types/std'; +} from './types/std.js'; import { isBuffer, isStream, isReadable, isWritable, isDuplex, -} from './types/node'; -import { isLongObject } from './types/external'; +} from './types/node.js'; +import { isLongObject } from './types/external.js'; export const is = { // Primitive diff --git a/src/types/external.ts b/src/types/external.ts index a1acf65..2d8eb7b 100644 --- a/src/types/external.ts +++ b/src/types/external.ts @@ -1,6 +1,6 @@ -import { isNumber } from './primitive'; -import { isObject } from './std'; -import { hasOwnProperty } from './util'; +import { isNumber } from './primitive.js'; +import { isObject } from './std.js'; +import { hasOwnProperty } from './util.js'; interface LongObject { high: number; diff --git a/src/types/node.ts b/src/types/node.ts index 6bf5e5c..e0a7c76 100644 --- a/src/types/node.ts +++ b/src/types/node.ts @@ -1,6 +1,6 @@ import { Buffer } from 'node:buffer'; import { Duplex, Readable, Stream, Writable } from 'node:stream'; -import { hasOwnPropertyInChain, isInstanceOf } from './util'; +import { hasOwnPropertyInChain, isInstanceOf } from './util.js'; /** * returns true if val is buffer diff --git a/src/types/std.ts b/src/types/std.ts index bc2dbb7..1984d54 100644 --- a/src/types/std.ts +++ b/src/types/std.ts @@ -1,8 +1,8 @@ // Javascript Standard Object import { types, debuglog } from 'node:util'; -import { hasOwnProperty, isInstanceOf } from './util'; -import type { Class } from './util'; +import { hasOwnProperty, isInstanceOf } from './util.js'; +import type { Class } from './util.js'; const debug = debuglog('is-type-of:std'); diff --git a/test/index.test.ts b/test/index.test.ts index 42bd3e4..4d99243 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,5 +1,5 @@ import assert from 'node:assert'; -import is from '../src'; +import is from '../src/index.js'; import { isBoolean as isBooleanExpect, isNumber as isNumberExpect, @@ -36,7 +36,7 @@ import { isReadable as isReadableExpect, isStream as isStreamExpect, isWritable as isWritableExpect, -} from '../src'; +} from '../src/index.js'; import { boolean as isBooleanCompatibleExpect, number as isNumberCompatibleExpect, @@ -69,12 +69,12 @@ import { readable as isReadableCompatibleExpect, stream as isStreamCompatibleExpect, writable as isWritableCompatibleExpect, -} from '../src'; -import { isBoolean, isNumber, isString, isSymbol, isUndefined, isBigInt, isNull, isNullable, isInteger, isInteger32, isLong, isDouble, isPrimitive, isFinite, isNaN, isSafeInteger } from '../src/types/primitive'; -import { isArray, isFunction, isGeneratorFunction, isAsyncFunction, isAsyncGeneratorFunction, isObject, isClass, isRegExp, isDate, isPromiseLike, isGenerator, isError, isPromise } from '../src/types/std'; -import { isBuffer, isDuplex, isReadable, isStream, isWritable } from '../src/types/node'; -import { isLongObject } from '../src/types/external'; -import { is as realIs } from '../src/is'; +} from '../src/index.js'; +import { isBoolean, isNumber, isString, isSymbol, isUndefined, isBigInt, isNull, isNullable, isInteger, isInteger32, isLong, isDouble, isPrimitive, isFinite, isNaN, isSafeInteger } from '../src/types/primitive.js'; +import { isArray, isFunction, isGeneratorFunction, isAsyncFunction, isAsyncGeneratorFunction, isObject, isClass, isRegExp, isDate, isPromiseLike, isGenerator, isError, isPromise } from '../src/types/std.js'; +import { isBuffer, isDuplex, isReadable, isStream, isWritable } from '../src/types/node.js'; +import { isLongObject } from '../src/types/external.js'; +import { is as realIs } from '../src/is.js'; describe('test/index.test.ts', () => { diff --git a/test/is.test.ts b/test/is.test.ts index 95d3fc4..4a46b49 100644 --- a/test/is.test.ts +++ b/test/is.test.ts @@ -1,11 +1,9 @@ -'use strict'; - import assert from 'node:assert'; -import { is } from '../src/is'; -import { isBoolean, isNumber, isString, isSymbol, isUndefined, isBigInt, isNull, isNullable, isInteger, isInteger32, isLong, isDouble, isPrimitive, isFinite, isNaN } from '../src/types/primitive'; -import { isArray, isFunction, isGeneratorFunction, isAsyncFunction, isAsyncGeneratorFunction, isObject, isClass, isRegExp, isDate, isPromiseLike, isGenerator, isError, isPromise } from '../src/types/std'; -import { isBuffer, isDuplex, isReadable, isStream, isWritable } from '../src/types/node'; -import { isLongObject } from '../src/types/external'; +import { is } from '../src/is.js'; +import { isBoolean, isNumber, isString, isSymbol, isUndefined, isBigInt, isNull, isNullable, isInteger, isInteger32, isLong, isDouble, isPrimitive, isFinite, isNaN } from '../src/types/primitive.js'; +import { isArray, isFunction, isGeneratorFunction, isAsyncFunction, isAsyncGeneratorFunction, isObject, isClass, isRegExp, isDate, isPromiseLike, isGenerator, isError, isPromise } from '../src/types/std.js'; +import { isBuffer, isDuplex, isReadable, isStream, isWritable } from '../src/types/node.js'; +import { isLongObject } from '../src/types/external.js'; describe('is', () => { diff --git a/test/types/external.test.ts b/test/types/external.test.ts index 00621e3..f5a0672 100644 --- a/test/types/external.test.ts +++ b/test/types/external.test.ts @@ -1,7 +1,7 @@ -import * as Long from 'long'; import assert from 'node:assert'; +import Long from 'long'; import { expectType } from 'ts-expect'; -import { isLongObject } from '../../src/types/external'; +import { isLongObject } from '../../src/types/external.js'; describe('test/external.test.ts', () => { describe('isLongObject', () => { @@ -24,5 +24,4 @@ describe('test/external.test.ts', () => { } }); }); - }); diff --git a/test/types/node.test.ts b/test/types/node.test.ts index ab7693e..844dc71 100644 --- a/test/types/node.test.ts +++ b/test/types/node.test.ts @@ -3,7 +3,7 @@ import { EventEmitter } from 'node:events'; import { Duplex, PassThrough, Readable, Stream, Transform, Writable } from 'node:stream'; import util from 'node:util'; import { expectType } from 'ts-expect'; -import { isBuffer, isDuplex, isReadable, isStream, isWritable } from '../../src/types/node'; +import { isBuffer, isDuplex, isReadable, isStream, isWritable } from '../../src/types/node.js'; describe('test/node.test.ts', () => { describe('isBuffer', () => { diff --git a/test/types/primitive.test.ts b/test/types/primitive.test.ts index 07e95e4..945c914 100644 --- a/test/types/primitive.test.ts +++ b/test/types/primitive.test.ts @@ -1,6 +1,9 @@ import assert from 'node:assert'; import { expectNever, expectType } from 'ts-expect'; -import { isBigInt, isBoolean, isNull, isNullable, isNumber, isPrimitive, isString, isSymbol, isUndefined, isInteger, isInteger32, isNaN, isFinite, isLong, isDouble, isSafeInteger } from '../../src/types/primitive'; +import { + isBigInt, isBoolean, isNull, isNullable, isNumber, isPrimitive, isString, isSymbol, + isUndefined, isInteger, isInteger32, isNaN, isFinite, isLong, isDouble, isSafeInteger, +} from '../../src/types/primitive.js'; describe('test/primitive.test.ts', () => { describe('isString', () => { diff --git a/test/types/std.test.ts b/test/types/std.test.ts index 0bad962..f9d71f4 100644 --- a/test/types/std.test.ts +++ b/test/types/std.test.ts @@ -1,7 +1,10 @@ -import assert from 'node:assert'; +import { strict as assert } from 'node:assert'; import { expectType } from 'ts-expect'; -import { isArray, isFunction, isGeneratorFunction, isAsyncFunction, isAsyncGeneratorFunction, isObject, isClass, isRegExp, isDate, isPromiseLike, isGenerator, isError, isPromise } from '../../src/types/std'; -import { Class, isInstanceOf } from '../../src/types/util'; +import { + isArray, isFunction, isGeneratorFunction, isAsyncFunction, isAsyncGeneratorFunction, isObject, + isClass, isRegExp, isDate, isPromiseLike, isGenerator, isError, isPromise, +} from '../../src/types/std.js'; +import { Class, isInstanceOf } from '../../src/types/util.js'; describe('test/object.test.ts', () => { @@ -63,9 +66,15 @@ describe('test/object.test.ts', () => { it('should check value', () => { assert.strictEqual(isFunction(function() {}), true); assert.strictEqual(isFunction(() => {}), true); - assert.strictEqual(isFunction(async () => {}), true); - assert.strictEqual(isFunction(function* () {}), true); - assert.strictEqual(isFunction(async function* () {}), true); + assert.strictEqual(isFunction(async () => { + // ignore + }), true); + assert.strictEqual(isFunction(function* () { + // ignore + }), true); + assert.strictEqual(isFunction(async function* () { + // ignore + }), true); assert.strictEqual(isFunction(null), false); assert.strictEqual(isFunction({}), false); @@ -88,7 +97,9 @@ describe('test/object.test.ts', () => { }; assert.strictEqual(isGeneratorFunction(gen1), true); - const gen2 = function* () {}; + const gen2 = function* () { + // ignore + }; const fun = () => {}; const obj = {}; assert.strictEqual(isGeneratorFunction(gen2()), false); @@ -114,7 +125,9 @@ describe('test/object.test.ts', () => { }; assert.strictEqual(isAsyncFunction(asyncFun1), true); - const asyncFun2 = async function() {}; + const asyncFun2 = async function() { + // ignore + }; assert.strictEqual(isAsyncFunction(asyncFun2()), false); const fun = () => {}; assert.strictEqual(isAsyncFunction(fun), false); @@ -141,7 +154,9 @@ describe('test/object.test.ts', () => { }; assert.strictEqual(isAsyncGeneratorFunction(asyncGenFun1), true); - const asyncFun2 = async function() {}; + const asyncFun2 = async function() { + // ignore + }; const fun = () => {}; const obj = {}; assert.strictEqual(isAsyncGeneratorFunction(asyncFun2()), false); diff --git a/tsconfig.json b/tsconfig.json index 39bf8ca..298cdd2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,12 +3,9 @@ "compileOnSave": true, "compilerOptions": { "strict": true, - "outDir": "dist", "noImplicitAny": true, - "target": "es2022" - }, - "exclude": [ - "node_modules", - "dist" - ] + "target": "ES2022", + "module": "NodeNext", + "moduleResolution": "NodeNext", + } } diff --git a/tsconfig.prod.json b/tsconfig.prod.json deleted file mode 100644 index 15b6f49..0000000 --- a/tsconfig.prod.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "extends": "./tsconfig", - "exclude": [ - "node_modules", - "dist", - "test" - ] -}