Skip to content

Commit e0c0f51

Browse files
authored
Merge pull request #40 from huan/esm
refactor to publslih as esm
2 parents 48ae1e0 + 26fc8a3 commit e0c0f51

13 files changed

+73
-48
lines changed

.eslintrc.js renamed to .eslintrc.cjs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
const rules = {
32
}
43

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ jspm_packages
4040
/bundles/
4141
/package-lock.json
4242
/state-switch-*.*.*.tgz
43-
43+
t.*
44+
t/

package.json

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
{
22
"name": "state-switch",
3-
"version": "0.14.1",
3+
"version": "0.15.4",
44
"description": "State Switch is a Change Monitor/Guarder for Async Actions.",
5-
"main": "bundles/state-switch.umd.js",
6-
"types": "dist/src/mod.d.ts",
5+
"main": "./dist/cjs/src/mod.js",
6+
"type": "module",
7+
"exports": {
8+
".": {
9+
"import": "./dist/esm/src/mod.js",
10+
"require": "./dist/cjs/src/mod.js"
11+
}
12+
},
13+
"typings": "./dist/esm/src/mod.d.ts",
714
"scripts": {
815
"clean": "rm -fr dist/* bundles/*",
9-
"dist": "npm run clean && npm run build && npm run rollup && npm run dist:es6to5",
10-
"dist:es6to5": "tsc --out ./bundles/state-switch.umd.js --target es5 --allowJs bundles/state-switch.es6.umd.js --lib es6,dom",
11-
"build": "tsc --module es6",
16+
"dist": "npm run clean && npm run build && (cd dist/cjs; npm init -y > /dev/null)",
17+
"build": "tsc && tsc -p tsconfig.cjs.json",
1218
"lint": "npm run lint:es && npm run lint:ts",
1319
"rollup": "rollup -c",
1420
"lint:ts": "tsc --noEmit",
1521
"demo": "ts-node example/demo",
1622
"test": "npm run lint && npm run test:ts",
1723
"test:pack": "bash -x scripts/npm-pack-testing.sh",
18-
"test:ts": "blue-tape -r ts-node/register src/**/*.spec.ts tests/**/*.spec.ts",
24+
"test:ts": "tap --node-arg=--loader=ts-node/esm src/**/*.spec.ts tests/**/*.spec.ts",
1925
"lint:es": "eslint --ignore-pattern fixtures/ 'src/**/*.ts' 'tests/**/*.ts'"
2026
},
2127
"repository": {
@@ -44,7 +50,7 @@
4450
"rollup-plugin-json": "^4.0.0",
4551
"rxjs": "^7.1.0",
4652
"tsd": "^0.16.0",
47-
"tstest": "^0.4.10"
53+
"tstest": "^0.5.16"
4854
},
4955
"files": [
5056
"LICENSE",

rollup.config.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ export default {
44
input: 'dist/src/mod.js',
55
output: {
66
banner: '/* state-switch version ' + require('./package.json').version + ' */',
7-
file: 'bundles/state-switch.es6.umd.js',
7+
file: 'bundles/state-switch.es6.js',
88
footer: '/* https://github.com/huan */',
9-
format: 'umd',
9+
format: 'es',
1010
name: 'window',
1111
sourcemap: true,
12+
exports: 'named',
1213
},
1314
plugins: [
1415
json({

scripts/npm-pack-testing.sh

+7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ mv *-*.*.*.tgz "$TMPDIR"
1010
cp tests/fixtures/smoke-testing.ts "$TMPDIR"
1111

1212
cd $TMPDIR
13+
1314
npm init -y
15+
# https://stackoverflow.com/a/59203952/1123955
16+
echo "`jq '.type="module"' package.json`" > package.json
17+
1418
npm install *-*.*.*.tgz \
1519
@types/node \
1620
typescript@latest
@@ -21,6 +25,9 @@ npm install *-*.*.*.tgz \
2125
--noEmitOnError \
2226
--noImplicitAny \
2327
--skipLibCheck \
28+
--target es2020 \
29+
--module es2020 \
30+
--moduleResolution node \
2431
smoke-testing.ts
2532

2633
node smoke-testing.js

src/mod.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
export {
1+
import {
22
StateSwitch,
3-
StateSwitch as default,
4-
} from './state-switch'
3+
} from './state-switch.js'
4+
import { VERSION } from './version.js'
55

6-
export { VERSION } from './version'
6+
export {
7+
StateSwitch,
8+
VERSION,
9+
}
10+
export default StateSwitch

src/state-switch.spec.ts

+19-18
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
#!/usr/bin/env ts-node
1+
#!/usr/bin/env node --loader ts-node/esm
22

3-
// tslint:disable:no-shadowed-variable
4-
import test from 'blue-tape'
5-
import sinon from 'sinon'
3+
import {
4+
test,
5+
sinon,
6+
} from 'tstest'
67

7-
import { StateSwitch } from './state-switch'
8+
import { StateSwitch } from './state-switch.js'
89

910
test('on()', async t => {
1011
const ss = new StateSwitch()
@@ -111,26 +112,26 @@ test('on/off events emitting', async t => {
111112
ss.addListener('on', spyOn)
112113
ss.addListener('off', spyOff)
113114

114-
t.true(spyOn.notCalled, 'spyOn is not called')
115-
t.true(spyOff.notCalled, 'spyOff is not called')
115+
t.ok(spyOn.notCalled, 'spyOn is not called')
116+
t.ok(spyOff.notCalled, 'spyOff is not called')
116117

117118
ss.on('pending')
118-
t.true(spyOn.calledOnce, 'spyOn is called once after on(pending)')
119-
t.deepEqual(spyOn.args[0], ['pending'], 'spyOn should be called with `pending` arg')
120-
t.true(spyOff.notCalled, 'spyOff is not called')
119+
t.ok(spyOn.calledOnce, 'spyOn is called once after on(pending)')
120+
t.same(spyOn.args[0], ['pending'], 'spyOn should be called with `pending` arg')
121+
t.ok(spyOff.notCalled, 'spyOff is not called')
121122

122123
ss.on(true)
123-
t.true(spyOn.calledTwice, 'spyOn is called once after on(pending)')
124-
t.deepEqual(spyOn.args[1], [true], 'spyOn should be called with `true` arg')
125-
t.true(spyOff.notCalled, 'spyOff is not called')
124+
t.ok(spyOn.calledTwice, 'spyOn is called once after on(pending)')
125+
t.same(spyOn.args[1], [true], 'spyOn should be called with `true` arg')
126+
t.ok(spyOff.notCalled, 'spyOff is not called')
126127

127128
ss.off('pending')
128-
t.true(spyOff.calledOnce, 'spyOff is called once after off(pending)')
129-
t.deepEqual(spyOff.args[0], ['pending'], 'spyOff should be called with `pending` arg')
129+
t.ok(spyOff.calledOnce, 'spyOff is called once after off(pending)')
130+
t.same(spyOff.args[0], ['pending'], 'spyOff should be called with `pending` arg')
130131

131132
ss.off(true)
132-
t.true(spyOff.calledTwice, 'spyOff is called twice after off(true)')
133-
t.deepEqual(spyOff.args[1], [true], 'spyOff should be called with `true` arg')
133+
t.ok(spyOff.calledTwice, 'spyOff is called twice after off(true)')
134+
t.same(spyOff.args[1], [true], 'spyOff should be called with `true` arg')
134135

135-
t.true(spyOn.calledTwice, 'spyOn called twice at last')
136+
t.ok(spyOn.calledTwice, 'spyOn called twice at last')
136137
})

src/state-switch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { EventEmitter } from 'events'
1212

1313
import NOP from 'nop'
1414

15-
import { VERSION } from './version'
15+
import { VERSION } from './version.js'
1616

1717
/**
1818
* Using Three Valued Logic for ON/OFF State

src/version.spec.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
#!/usr/bin/env ts-node
1+
#!/usr/bin/env node --loader ts-node/esm
22

3-
// tslint:disable:no-shadowed-variable
4-
import test from 'tstest'
3+
import { test } from 'tstest'
54

6-
import { VERSION } from './version'
5+
import { VERSION } from './version.js'
76

87
test('Make sure the VERSION is fresh in source code', async (t) => {
98
t.equal(VERSION, '0.0.0', 'version should be 0.0.0 in source code, only updated before publish to NPM')

tests/fixtures/smoke-testing.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ import Default, {
33
VERSION,
44
} from 'state-switch'
55

6-
if (VERSION === '0.0.0') {
7-
throw new Error('version should be set before publishing')
8-
}
9-
106
const ss = new StateSwitch()
117
console.info(`StateSwitch v${ss.version()}`)
128

@@ -19,4 +15,8 @@ if (Default !== StateSwitch) {
1915
throw new Error('default export does not match the exported module!')
2016
}
2117

18+
if (VERSION === '0.0.0') {
19+
throw new Error('version should be set before publishing')
20+
}
21+
2222
console.info('Smoke Testing PASSED!')

tests/from-event-type.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env ts-node
1+
#!/usr/bin/env node --loader ts-node/esm
22

33
import { test } from 'tstest'
44
import {
@@ -13,7 +13,7 @@ import {
1313
} from 'rxjs'
1414
import { HasEventTargetAddRemove } from 'rxjs/internal/observable/fromEvent'
1515

16-
import { StateSwitch } from '../src/state-switch'
16+
import { StateSwitch } from '../src/state-switch.js'
1717

1818
test('StateSwitch satisfy DOM EventTarget: HasEventTargetAddRemove', async t => {
1919
const state = new StateSwitch()

tsconfig.cjs.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"module": "CommonJS",
5+
"outDir": "dist/cjs",
6+
},
7+
}

tsconfig.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"compilerOptions": {
3-
"target": "es6",
4-
"module": "commonjs",
3+
"target": "es2020",
4+
"module": "es2020",
55
"moduleResolution": "node",
6-
"outDir": "dist",
6+
"outDir": "dist/esm",
77
"declaration": true,
88
"sourceMap": true,
99
"noLib": false,

0 commit comments

Comments
 (0)