From 71d2591c3ab0352a296564cbb90c4527c3e200eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Huan=20=28=E6=9D=8E=E5=8D=93=E6=A1=93=29?= Date: Sun, 12 Sep 2021 19:58:32 +0800 Subject: [PATCH] ES Module support (#35) * ES Modules support * v0.5 * 0.5.2 --- .eslintrc.js => .eslintrc.cjs | 0 .github/workflows/npm.yml | 28 +++++++++++------ README.md | 3 +- examples/ducks-proposal.ts | 2 +- examples/vanilla-redux.ts | 2 +- package.json | 54 +++++++++++++++++++++----------- scripts/generate-package-json.sh | 17 ++++++++++ scripts/generate-version.sh | 18 ----------- scripts/npm-pack-testing.sh | 44 +++++++++++++++++++++++--- scripts/update-license.ts | 6 ++-- src/config.ts | 7 +++++ src/duck/actions.ts | 4 +-- src/duck/epics.ts | 8 ++--- src/duck/mod.ts | 14 ++++----- src/duck/operations.ts | 4 +-- src/duck/reducers.ts | 6 ++-- src/duck/rx-async.ts | 16 ++++++---- src/duck/selectors.ts | 2 +- src/duck/tests.spec.ts | 4 +-- src/duck/utils.ts | 4 +-- src/manager.ts | 2 +- src/mod.ts | 12 ++++--- src/package-json.spec.ts | 10 ++++++ src/package-json.ts | 11 +++++++ src/version.spec.ts | 28 ----------------- src/version.ts | 21 ------------- src/wechaty-redux.spec.ts | 16 +++++----- src/wechaty-redux.ts | 8 ++--- tests/fixtures/smoke-testing.ts | 10 +++--- tests/integration.spec.ts | 4 +-- tsconfig.cjs.json | 7 +++++ tsconfig.json | 2 +- 32 files changed, 214 insertions(+), 160 deletions(-) rename .eslintrc.js => .eslintrc.cjs (100%) create mode 100755 scripts/generate-package-json.sh delete mode 100755 scripts/generate-version.sh create mode 100644 src/config.ts create mode 100755 src/package-json.spec.ts create mode 100644 src/package-json.ts delete mode 100755 src/version.spec.ts delete mode 100644 src/version.ts create mode 100644 tsconfig.cjs.json diff --git a/.eslintrc.js b/.eslintrc.cjs similarity index 100% rename from .eslintrc.js rename to .eslintrc.cjs diff --git a/.github/workflows/npm.yml b/.github/workflows/npm.yml index 42c220e..4a8a349 100644 --- a/.github/workflows/npm.yml +++ b/.github/workflows/npm.yml @@ -7,8 +7,10 @@ jobs: name: Build strategy: matrix: - os: [ubuntu-latest] - node: [14] + os: + - ubuntu-latest + node: + - 16 runs-on: ${{ matrix.os }} steps: @@ -17,6 +19,8 @@ jobs: uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} + cache: npm + cache-dependency-path: package.json - name: Install Dependencies run: npm install @@ -32,19 +36,21 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: - node-version: 14 + node-version: 16 + cache: npm + cache-dependency-path: package.json - name: Install Dependencies run: npm install - - name: Generate Version - run: ./scripts/generate-version.sh + - name: Generate Package JSON + run: ./scripts/generate-package-json.sh - name: Pack Testing run: ./scripts/npm-pack-testing.sh publish: - if: github.event_name == 'push' && (github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/heads/v')) + if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/v')) name: Publish needs: [build, pack] runs-on: ubuntu-latest @@ -52,14 +58,16 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: - node-version: 14 + node-version: 16 registry-url: https://registry.npmjs.org/ + cache: npm + cache-dependency-path: package.json - name: Install Dependencies run: npm install - - name: Generate Version - run: ./scripts/generate-version.sh + - name: Generate Package JSON + run: ./scripts/generate-package-json.sh - name: Set Publish Config run: ./scripts/package-publish-config-tag.sh @@ -70,7 +78,7 @@ jobs: - name: Check Branch id: check-branch run: | - if [[ ${{ github.ref }} =~ ^refs/heads/(master|v[0-9]+\.[0-9]+.*)$ ]]; then + if [[ ${{ github.ref }} =~ ^refs/heads/(main|v[0-9]+\.[0-9]+.*)$ ]]; then echo ::set-output name=match::true fi # See: https://stackoverflow.com/a/58869470/1123955 - name: Is A Publish Branch diff --git a/README.md b/README.md index a65b234..e5392c1 100644 --- a/README.md +++ b/README.md @@ -167,9 +167,10 @@ See: [api/index.ts](src/api/index.ts) ## History -### Main +### Main v0.5 1. Upgrade RxJS version from 6 to 7.1 +1. ES Modules support ### v0.2 (Jun 2, 2020) diff --git a/examples/ducks-proposal.ts b/examples/ducks-proposal.ts index 8fdf7e0..45f097b 100644 --- a/examples/ducks-proposal.ts +++ b/examples/ducks-proposal.ts @@ -22,7 +22,7 @@ import { Ducks } from 'ducks' import { WechatyRedux, Duck, -} from '../src/mod' // 'wechaty-redux' +} from '../src/mod.js' // 'wechaty-redux' async function main () { /** diff --git a/examples/vanilla-redux.ts b/examples/vanilla-redux.ts index fe5efab..097419d 100644 --- a/examples/vanilla-redux.ts +++ b/examples/vanilla-redux.ts @@ -29,7 +29,7 @@ import { Wechaty } from 'wechaty' import { WechatyRedux, Duck, -} from '../src/mod' // 'wechaty-redux' +} from '../src/mod.js' // 'wechaty-redux' async function main () { /** diff --git a/package.json b/package.json index bfe7bfc..a9b52be 100644 --- a/package.json +++ b/package.json @@ -1,21 +1,32 @@ { "name": "wechaty-redux", - "version": "0.3.6", + "version": "0.5.2", "description": "Wechaty Redux Plugin Powered By Ducks", - "main": "dist/src/mod.js", - "typings": "dist/src/mod.d.ts", + "type": "module", + "exports": { + ".": { + "import": "./dist/esm/src/mod.js", + "require": "./dist/cjs/src/mod.js" + } + }, + "typings": "./dist/esm/src/mod.d.ts", + "engines": { + "wechaty": ">=0.69.41", + "node": ">=14" + }, "scripts": { + "build": "tsc && tsc -p tsconfig.cjs.json", "clean": "shx rm -fr dist/*", - "dist": "npm run clean && tsc", - "pack": "npm pack", + "dist": "npm-run-all clean build dist:commonjs", + "dist:commonjs": "jq -n \"{ type: \\\"commonjs\\\" }\" > dist/cjs/package.json", "lint": "npm run lint:es && npm run lint:ts && npm run lint:md", "lint:md": "markdownlint README.md", - "lint:ts": "tsc --noEmit", + "lint:ts": "tsc --isolatedModules --noEmit", "redux-devtools": "redux-devtools --hostname=localhost --port=8000", "start": "npm run quack", - "test": "npm run lint && npm run test:unit", + "test": "npm-run-all lint test:unit", "test:pack": "bash -x scripts/npm-pack-testing.sh", - "test:unit": "blue-tape -r ts-node/register \"src/**/*.spec.ts\" \"src/*.spec.ts\" \"examples/**/*.spec.ts\" \"tests/*.spec.ts\" \"tests/**/*.spec.ts\"", + "test:unit": "tap --node-arg=--loader=ts-node/esm --node-arg=--no-warnings \"src/**/*.spec.ts\" \"tests/**/*.spec.ts\"", "lint:es": "eslint --ignore-pattern tests/fixtures/ '{bin,examples,scripts,src,tests}/**/*.ts'" }, "repository": { @@ -38,32 +49,39 @@ "tag": "next" }, "dependencies": { - "redux": "^4.1.0", - "redux-observable": "^2.0.0-rc.2", - "rxjs": "^7.1", + "redux": "^4.1.1", + "redux-observable": "^2.0.0", + "rxjs": "^7.3", "typesafe-actions": "^5.1.0" }, - "peerDependencies": { - "wechaty": ">=0.61.2" - }, "devDependencies": { "@chatie/eslint-config": "^0.14.1", "@chatie/git-scripts": "^0.6.2", "@chatie/semver": "^0.4.7", "@chatie/tsconfig": "^0.20.2", - "@types/glob": "^7.1.3", - "@types/remote-redux-devtools": "^0.5.4", + "@types/glob": "^7.1.4", + "@types/remote-redux-devtools": "^0.5.5", + "cross-env": "^7.0.3", "ducks": "^0.10.2", "glob": "^7.1.7", + "npm-run-all": "^4.1.5", "pkg-jq": "^0.2.11", "remote-redux-devtools": "^0.5.16", "shx": "^0.3.3", - "tstest": "^0.4.10", + "tstest": "^0.5.16", "typescript": "4.4", "utility-types": "^3.10.0", - "wechaty": "^0.68.1", + "wechaty": "^0.69.41", "wechaty-puppet-mock": "^0.31.3" }, + "files": [ + "bin/", + "dist/", + "src/" + ], + "tap": { + "check-coverage": false + }, "git": { "scripts": { "pre-push": "npx git-scripts-pre-push" diff --git a/scripts/generate-package-json.sh b/scripts/generate-package-json.sh new file mode 100755 index 0000000..111e894 --- /dev/null +++ b/scripts/generate-package-json.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +set -e + +SRC_PACKAGE_JSON_TS_FILE='src/package-json.ts' + +[ -f ${SRC_PACKAGE_JSON_TS_FILE} ] || { + echo ${SRC_PACKAGE_JSON_TS_FILE}" not found" + exit 1 +} + +cat <<_SRC_ > ${SRC_PACKAGE_JSON_TS_FILE} +/** + * This file was auto generated from scripts/generate-version.sh + */ +import type { PackageJson } from 'type-fest' +export const packageJson: PackageJson = $(cat package.json) as any +_SRC_ diff --git a/scripts/generate-version.sh b/scripts/generate-version.sh deleted file mode 100755 index e8ef7f9..0000000 --- a/scripts/generate-version.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env bash -set -e - -SRC_VERSION_TS_FILE='src/version.ts' - -[ -f ${SRC_VERSION_TS_FILE} ] || { - echo ${SRC_VERSION_TS_FILE}" not found" - exit 1 -} - -VERSION=$(npx pkg-jq -r .version) - -cat <<_SRC_ > ${SRC_VERSION_TS_FILE} -/** - * This file was auto generated from scripts/generate-version.sh - */ -export const VERSION: string = '${VERSION}' -_SRC_ diff --git a/scripts/npm-pack-testing.sh b/scripts/npm-pack-testing.sh index 33621de..c37c59d 100755 --- a/scripts/npm-pack-testing.sh +++ b/scripts/npm-pack-testing.sh @@ -3,14 +3,14 @@ set -e VERSION=$(npx pkg-jq -r .version) -if npx --package @chatie/semver semver-is-prod $VERSION; then +if npx --package @chatie/semver semver-is-prod "$VERSION"; then NPM_TAG=latest else NPM_TAG=next fi npm run dist -npm run pack +npm pack TMPDIR="/tmp/npm-pack-testing.$$" mkdir "$TMPDIR" @@ -18,18 +18,52 @@ mv ./*-*.*.*.tgz "$TMPDIR" cp tests/fixtures/smoke-testing.ts "$TMPDIR" cd $TMPDIR + npm init -y -npm install *-*.*.*.tgz \ - @chatie/tsconfig@"$NPM_TAG" \ - "wechaty@$NPM_TAG" \ +npm install --production *-*.*.*.tgz \ + @types/node \ + @chatie/tsconfig@$NPM_TAG \ ducks \ + pkg-jq \ + "wechaty-puppet@$NPM_TAG" \ + "wechaty@$NPM_TAG" \ +# +# CommonJS +# ./node_modules/.bin/tsc \ + --target es6 \ + --module CommonJS \ + \ + --moduleResolution node \ --esModuleInterop \ --lib esnext \ + --noEmitOnError \ + --noImplicitAny \ --skipLibCheck \ + smoke-testing.ts + +echo +echo "CommonJS: pack testing..." +node smoke-testing.js + +# +# ES Modules +# +npx pkg-jq -i '.type="module"' + +./node_modules/.bin/tsc \ + --target es2020 \ + --module es2020 \ + \ + --moduleResolution node \ + --esModuleInterop \ + --lib esnext \ --noEmitOnError \ --noImplicitAny \ + --skipLibCheck \ smoke-testing.ts +echo +echo "ES Module: pack testing..." node smoke-testing.js diff --git a/scripts/update-license.ts b/scripts/update-license.ts index 00d7191..54cd416 100644 --- a/scripts/update-license.ts +++ b/scripts/update-license.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env ts-node +#!/usr/bin/env -S node --no-warnings --loader ts-node/esm /** * Wechaty Open Source Software - https://github.com/wechaty @@ -66,7 +66,7 @@ class LicenseTransformer extends Transform { // super(options) // } - public _transform (chunk: any, _: string /* encoding: string */, done: () => void) { + override _transform (chunk: any, _: string /* encoding: string */, done: () => void) { if (this.updated) { this.push(chunk) } else { @@ -127,7 +127,7 @@ class LicenseTransformer extends Transform { return updatedLineList.join('\n') } - public _flush (done: () => void) { + override _flush (done: () => void) { if (this.lineBuf) { this.push(this.lineBuf) this.lineBuf = '' diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 0000000..9fabd10 --- /dev/null +++ b/src/config.ts @@ -0,0 +1,7 @@ +import { packageJson } from './package-json.js' + +const VERSION = packageJson.version || '0.0.0' + +export { + VERSION, +} diff --git a/src/duck/actions.ts b/src/duck/actions.ts index 0d3d79f..c49ad69 100644 --- a/src/duck/actions.ts +++ b/src/duck/actions.ts @@ -23,7 +23,7 @@ import { createAsyncAction, } from 'typesafe-actions' -import { +import type { EventDongPayload, EventErrorPayload, EventScanPayload, @@ -44,7 +44,7 @@ import { import cuid from 'cuid' -import * as types from './types' +import * as types from './types.js' // interface ContactIdOptions { contactId: string } interface ErrorOptions { error: Error } diff --git a/src/duck/epics.ts b/src/duck/epics.ts index 44b3859..1a443c4 100644 --- a/src/duck/epics.ts +++ b/src/duck/epics.ts @@ -27,13 +27,13 @@ import { map, } from 'rxjs/operators' -import { +import type { Epic, } from 'redux-observable' -import * as actions from './actions' -import * as rxAsync from './rx-async' -import * as utils from './utils' +import * as actions from './actions.js' +import * as rxAsync from './rx-async.js' +import * as utils from './utils.js' const dingEpic: Epic = actions$ => actions$.pipe( filter(isActionOf(actions.ding)), diff --git a/src/duck/mod.ts b/src/duck/mod.ts index fa6ca50..1b9b5ee 100644 --- a/src/duck/mod.ts +++ b/src/duck/mod.ts @@ -17,14 +17,14 @@ * limitations under the License. * */ -import reducer from './reducers' +import reducer from './reducers.js' -import * as actions from './actions' -import * as epics from './epics' -import * as operations from './operations' -import * as selectors from './selectors' -import * as types from './types' -import * as utils from './utils' +import * as actions from './actions.js' +import * as epics from './epics.js' +import * as operations from './operations.js' +import * as selectors from './selectors.js' +import * as types from './types.js' +import * as utils from './utils.js' export { actions, diff --git a/src/duck/operations.ts b/src/duck/operations.ts index e66bd3c..46195d4 100644 --- a/src/duck/operations.ts +++ b/src/duck/operations.ts @@ -17,9 +17,9 @@ * limitations under the License. * */ -import { Dispatch } from 'redux' +import type { Dispatch } from 'redux' -import * as actions from './actions' +import * as actions from './actions.js' const ding = (dispatch: Dispatch) => (wechatyId: string, data: string) => dispatch(actions.ding(wechatyId, data)) const say = (dispatch: Dispatch) => (wechatyId: string, conversationId: string, text: string) => dispatch(actions.sayAsync.request({ conversationId, text, wechatyId })) diff --git a/src/duck/reducers.ts b/src/duck/reducers.ts index d534ec3..8308ca2 100644 --- a/src/duck/reducers.ts +++ b/src/duck/reducers.ts @@ -18,11 +18,11 @@ * */ import { createReducer, ActionType } from 'typesafe-actions' -import { DeepReadonly } from 'utility-types' +import type { DeepReadonly } from 'utility-types' -import { ContactPayload } from 'wechaty-puppet' +import type { ContactPayload } from 'wechaty-puppet' -import * as actions from './actions' +import * as actions from './actions.js' const initialState: DeepReadonly<{ [wechatyId: string]: undefined | { // wechaty id diff --git a/src/duck/rx-async.ts b/src/duck/rx-async.ts index 7dd9073..7aa2ab4 100644 --- a/src/duck/rx-async.ts +++ b/src/duck/rx-async.ts @@ -29,9 +29,9 @@ import { import { getWechaty, -} from '../manager' +} from '../manager.js' -import * as actions from './actions' +import * as actions from './actions.js' const ding$ = (action: ReturnType) => of( // void getWechaty(action.payload.wechatyId) @@ -41,20 +41,24 @@ const ding$ = (action: ReturnType) => of( // void catchError(e => of( actions.errorEvent( action.payload.wechatyId, - { data: String(e) }, + { ...e }, ) )), ) -const reset$ = (action: ReturnType) => from( // promise +/** + * Huan(202109): the return type of wechaty.reset() is void + * which means we will not get any response from wechaty.reset() + */ +const reset$ = (action: ReturnType) => of( getWechaty(action.payload.wechatyId) .reset(action.payload.data) ).pipe( ignoreElements(), - catchError(e => of( + catchError((e: Error) => of( actions.errorEvent( action.payload.wechatyId, - { data: String(e) }, + { ...e }, // { data: String(e) }, ) )), ) diff --git a/src/duck/selectors.ts b/src/duck/selectors.ts index 85dec42..ef55427 100644 --- a/src/duck/selectors.ts +++ b/src/duck/selectors.ts @@ -17,7 +17,7 @@ * limitations under the License. * */ -import { State } from './reducers' +import type { State } from './reducers.js' const getQrCode = (state: State) => (wechatyId: string) => state[wechatyId]?.qrcode const getUserPayload = (state: State) => (wechatyId: string) => state[wechatyId]?.user diff --git a/src/duck/tests.spec.ts b/src/duck/tests.spec.ts index d23a757..08607ae 100644 --- a/src/duck/tests.spec.ts +++ b/src/duck/tests.spec.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env ts-node +#!/usr/bin/env -S node --no-warnings --loader ts-node/esm /** * Wechaty Open Source Software - https://github.com/wechaty * @@ -20,6 +20,6 @@ */ import { validateDuck } from 'ducks' -import * as Duck from './mod' +import * as Duck from './mod.js' validateDuck(Duck) diff --git a/src/duck/utils.ts b/src/duck/utils.ts index 7e70dd7..c60c2ab 100644 --- a/src/duck/utils.ts +++ b/src/duck/utils.ts @@ -26,9 +26,9 @@ import { map, } from 'rxjs/operators' -import { getWechaty } from '../manager' +import { getWechaty } from '../manager.js' -import * as actions from './actions' +import type * as actions from './actions.js' import { Message } from 'wechaty' /** diff --git a/src/manager.ts b/src/manager.ts index 5b5f7bb..e9142f4 100644 --- a/src/manager.ts +++ b/src/manager.ts @@ -17,7 +17,7 @@ * limitations under the License. * */ -import { Wechaty } from 'wechaty' +import type { Wechaty } from 'wechaty' const instanceStore = new Map() diff --git a/src/mod.ts b/src/mod.ts index 49d15d4..4604fb1 100644 --- a/src/mod.ts +++ b/src/mod.ts @@ -17,20 +17,22 @@ * limitations under the License. * */ -import * as Duck from './duck/mod' +import * as Duck from './duck/mod.js' import { WechatyRedux, WechatyReduxOptions, -} from './wechaty-redux' +} from './wechaty-redux.js' -import { getWechaty } from './manager' -import { VERSION } from './version' +import { getWechaty } from './manager.js' +import { VERSION } from './config.js' +export type { + WechatyReduxOptions, +} export { Duck, getWechaty, VERSION, WechatyRedux, - WechatyReduxOptions, } diff --git a/src/package-json.spec.ts b/src/package-json.spec.ts new file mode 100755 index 0000000..5d9cac5 --- /dev/null +++ b/src/package-json.spec.ts @@ -0,0 +1,10 @@ +#!/usr/bin/env -S node --no-warnings --loader ts-node/esm + +import { test } from 'tstest' + +import { packageJson } from './package-json.js' + +test('Make sure the packageJson is fresh in source code', async t => { + const keyNum = Object.keys(packageJson).length + t.equal(keyNum, 0, 'packageJson should be empty in source code, only updated before publish to NPM') +}) diff --git a/src/package-json.ts b/src/package-json.ts new file mode 100644 index 0000000..0a9806f --- /dev/null +++ b/src/package-json.ts @@ -0,0 +1,11 @@ +/** + * This file will be overwrite when we publish NPM module + * by scripts/generate_version.ts + */ +import type { PackageJson } from 'type-fest' + +/** + * Huan(202108): + * The below default values is only for unit testing + */ +export const packageJson: PackageJson = {} diff --git a/src/version.spec.ts b/src/version.spec.ts deleted file mode 100755 index 36f47d0..0000000 --- a/src/version.spec.ts +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env ts-node - -/** - * Wechaty Open Source Software - https://github.com/wechaty - * - * @copyright 2016 Huan LI (李卓桓) , and - * Wechaty Contributors . - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -import test from 'tstest' - -import { VERSION } from './version' - -test('Make sure the VERSION is fresh in source code', async t => { - t.equal(VERSION, '0.0.0', 'version should be 0.0.0 in source code, only updated before publish to NPM') -}) diff --git a/src/version.ts b/src/version.ts deleted file mode 100644 index f2da2ca..0000000 --- a/src/version.ts +++ /dev/null @@ -1,21 +0,0 @@ -/** - * Wechaty Open Source Software - https://github.com/wechaty - * - * @copyright 2016 Huan LI (李卓桓) , and - * Wechaty Contributors . - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -export const VERSION = '0.0.0' diff --git a/src/wechaty-redux.spec.ts b/src/wechaty-redux.spec.ts index ce5e847..671e8e7 100755 --- a/src/wechaty-redux.spec.ts +++ b/src/wechaty-redux.spec.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env ts-node +#!/usr/bin/env -S node --no-warnings --loader ts-node/esm /** * Wechaty Open Source Software - https://github.com/wechaty @@ -19,8 +19,10 @@ * limitations under the License. * */ -import test from 'tstest' -import sinon from 'sinon' +import { + test, + sinon, +} from 'tstest' import { Wechaty, @@ -48,7 +50,7 @@ import { import { Duck, WechatyRedux, -} from './mod' +} from './mod.js' async function * wechatyFixtures () { const ducks = new Ducks({ @@ -57,7 +59,7 @@ async function * wechatyFixtures () { let devCompose = compose - if (process.env.REDUX_DEVTOOLS) { + if (process.env['REDUX_DEVTOOLS']) { devCompose = composeWithDevTools({ hostname : 'localhost', port : 8000, @@ -161,7 +163,7 @@ test('WechatyRedux: operations.say()', async t => { const TEXT = 'Hello, world.' - const [user, mary] = mocker.createContacts(2) + const [user, mary] = mocker.createContacts(2) as [mock.ContactMock, mock.ContactMock] mocker.login(user) const sandbox = sinon.createSandbox() @@ -189,7 +191,7 @@ test('WechatyRedux: Puppet `message` event', async t => { const TEXT = 'Hello, world.' - const [user, mary] = mocker.createContacts(2) + const [user, mary] = mocker.createContacts(2) as [mock.ContactMock, mock.ContactMock] mocker.login(user) const future = new Promise(resolve => bot.once('message', resolve)) diff --git a/src/wechaty-redux.ts b/src/wechaty-redux.ts index f2deece..e462347 100644 --- a/src/wechaty-redux.ts +++ b/src/wechaty-redux.ts @@ -30,15 +30,15 @@ import { map, } from 'rxjs/operators' -import { +import type { Store, } from 'redux' -import * as duck from './duck/mod' +import * as duck from './duck/mod.js' -import * as instances from './manager' +import * as instances from './manager.js' -import { +import type { EventDongPayload, EventErrorPayload, EventScanPayload, diff --git a/tests/fixtures/smoke-testing.ts b/tests/fixtures/smoke-testing.ts index 7acb8ef..f8a78fe 100644 --- a/tests/fixtures/smoke-testing.ts +++ b/tests/fixtures/smoke-testing.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env ts-node +#!/usr/bin/env -S node --no-warnings --loader ts-node/esm /** * Wechaty Open Source Software - https://github.com/wechaty * @@ -27,10 +27,6 @@ import { Ducks } from 'ducks' import { Wechaty } from 'wechaty' async function main () { - if (VERSION === '0.0.0') { - throw new Error('version should be set before publishing') - } - const ducks = new Ducks({ wechaty: Duck }) const store = ducks.configureStore() @@ -40,6 +36,10 @@ async function main () { WechatyRedux({ store }) ) + if (VERSION === '0.0.0') { + throw new Error('version should be set before publishing') + } + return 0 } diff --git a/tests/integration.spec.ts b/tests/integration.spec.ts index a8ecbe0..6cdf379 100644 --- a/tests/integration.spec.ts +++ b/tests/integration.spec.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env ts-node +#!/usr/bin/env -S node --no-warnings --loader ts-node/esm /** * Wechaty Open Source Software - https://github.com/wechaty @@ -25,7 +25,7 @@ import { import { WechatyRedux, -} from '../src/mod' +} from '../src/mod.js' test('integration testing', async (t) => { const name = WechatyRedux({ store: {} as any }).name diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json new file mode 100644 index 0000000..8693cd0 --- /dev/null +++ b/tsconfig.cjs.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "CommonJS", + "outDir": "dist/cjs", + }, +} diff --git a/tsconfig.json b/tsconfig.json index 728a055..bbf1a08 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "@chatie/tsconfig", "compilerOptions": { - "outDir": "dist", + "outDir": "dist/esm", }, "exclude": [ "node_modules/",