Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor to publslih as esm #40

Merged
merged 9 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

const rules = {
}

Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ jspm_packages
/bundles/
/package-lock.json
/state-switch-*.*.*.tgz

t.*
t/
22 changes: 14 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
{
"name": "state-switch",
"version": "0.14.1",
"version": "0.15.4",
"description": "State Switch is a Change Monitor/Guarder for Async Actions.",
"main": "bundles/state-switch.umd.js",
"types": "dist/src/mod.d.ts",
"main": "./dist/cjs/src/mod.js",
"type": "module",
"exports": {
".": {
"import": "./dist/esm/src/mod.js",
"require": "./dist/cjs/src/mod.js"
}
},
"typings": "./dist/esm/src/mod.d.ts",
"scripts": {
"clean": "rm -fr dist/* bundles/*",
"dist": "npm run clean && npm run build && npm run rollup && npm run dist:es6to5",
"dist:es6to5": "tsc --out ./bundles/state-switch.umd.js --target es5 --allowJs bundles/state-switch.es6.umd.js --lib es6,dom",
"build": "tsc --module es6",
"dist": "npm run clean && npm run build && (cd dist/cjs; npm init -y > /dev/null)",
"build": "tsc && tsc -p tsconfig.cjs.json",
"lint": "npm run lint:es && npm run lint:ts",
"rollup": "rollup -c",
"lint:ts": "tsc --noEmit",
"demo": "ts-node example/demo",
"test": "npm run lint && npm run test:ts",
"test:pack": "bash -x scripts/npm-pack-testing.sh",
"test:ts": "blue-tape -r ts-node/register src/**/*.spec.ts tests/**/*.spec.ts",
"test:ts": "tap --node-arg=--loader=ts-node/esm src/**/*.spec.ts tests/**/*.spec.ts",
"lint:es": "eslint --ignore-pattern fixtures/ 'src/**/*.ts' 'tests/**/*.ts'"
},
"repository": {
Expand Down Expand Up @@ -44,7 +50,7 @@
"rollup-plugin-json": "^4.0.0",
"rxjs": "^7.1.0",
"tsd": "^0.16.0",
"tstest": "^0.4.10"
"tstest": "^0.5.16"
},
"files": [
"LICENSE",
Expand Down
5 changes: 3 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ export default {
input: 'dist/src/mod.js',
output: {
banner: '/* state-switch version ' + require('./package.json').version + ' */',
file: 'bundles/state-switch.es6.umd.js',
file: 'bundles/state-switch.es6.js',
footer: '/* https://github.com/huan */',
format: 'umd',
format: 'es',
name: 'window',
sourcemap: true,
exports: 'named',
},
plugins: [
json({
Expand Down
7 changes: 7 additions & 0 deletions scripts/npm-pack-testing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ mv *-*.*.*.tgz "$TMPDIR"
cp tests/fixtures/smoke-testing.ts "$TMPDIR"

cd $TMPDIR

npm init -y
# https://stackoverflow.com/a/59203952/1123955
echo "`jq '.type="module"' package.json`" > package.json

npm install *-*.*.*.tgz \
@types/node \
typescript@latest
Expand All @@ -21,6 +25,9 @@ npm install *-*.*.*.tgz \
--noEmitOnError \
--noImplicitAny \
--skipLibCheck \
--target es2020 \
--module es2020 \
--moduleResolution node \
smoke-testing.ts

node smoke-testing.js
12 changes: 8 additions & 4 deletions src/mod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export {
import {
StateSwitch,
StateSwitch as default,
} from './state-switch'
} from './state-switch.js'
import { VERSION } from './version.js'

export { VERSION } from './version'
export {
StateSwitch,
VERSION,
}
export default StateSwitch
37 changes: 19 additions & 18 deletions src/state-switch.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env ts-node
#!/usr/bin/env node --loader ts-node/esm

// tslint:disable:no-shadowed-variable
import test from 'blue-tape'
import sinon from 'sinon'
import {
test,
sinon,
} from 'tstest'

import { StateSwitch } from './state-switch'
import { StateSwitch } from './state-switch.js'

test('on()', async t => {
const ss = new StateSwitch()
Expand Down Expand Up @@ -111,26 +112,26 @@ test('on/off events emitting', async t => {
ss.addListener('on', spyOn)
ss.addListener('off', spyOff)

t.true(spyOn.notCalled, 'spyOn is not called')
t.true(spyOff.notCalled, 'spyOff is not called')
t.ok(spyOn.notCalled, 'spyOn is not called')
t.ok(spyOff.notCalled, 'spyOff is not called')

ss.on('pending')
t.true(spyOn.calledOnce, 'spyOn is called once after on(pending)')
t.deepEqual(spyOn.args[0], ['pending'], 'spyOn should be called with `pending` arg')
t.true(spyOff.notCalled, 'spyOff is not called')
t.ok(spyOn.calledOnce, 'spyOn is called once after on(pending)')
t.same(spyOn.args[0], ['pending'], 'spyOn should be called with `pending` arg')
t.ok(spyOff.notCalled, 'spyOff is not called')

ss.on(true)
t.true(spyOn.calledTwice, 'spyOn is called once after on(pending)')
t.deepEqual(spyOn.args[1], [true], 'spyOn should be called with `true` arg')
t.true(spyOff.notCalled, 'spyOff is not called')
t.ok(spyOn.calledTwice, 'spyOn is called once after on(pending)')
t.same(spyOn.args[1], [true], 'spyOn should be called with `true` arg')
t.ok(spyOff.notCalled, 'spyOff is not called')

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

ss.off(true)
t.true(spyOff.calledTwice, 'spyOff is called twice after off(true)')
t.deepEqual(spyOff.args[1], [true], 'spyOff should be called with `true` arg')
t.ok(spyOff.calledTwice, 'spyOff is called twice after off(true)')
t.same(spyOff.args[1], [true], 'spyOff should be called with `true` arg')

t.true(spyOn.calledTwice, 'spyOn called twice at last')
t.ok(spyOn.calledTwice, 'spyOn called twice at last')
})
2 changes: 1 addition & 1 deletion src/state-switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { EventEmitter } from 'events'

import NOP from 'nop'

import { VERSION } from './version'
import { VERSION } from './version.js'

/**
* Using Three Valued Logic for ON/OFF State
Expand Down
7 changes: 3 additions & 4 deletions src/version.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/usr/bin/env ts-node
#!/usr/bin/env node --loader ts-node/esm

// tslint:disable:no-shadowed-variable
import test from 'tstest'
import { test } from 'tstest'

import { VERSION } from './version'
import { VERSION } from './version.js'

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')
Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures/smoke-testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import Default, {
VERSION,
} from 'state-switch'

if (VERSION === '0.0.0') {
throw new Error('version should be set before publishing')
}

const ss = new StateSwitch()
console.info(`StateSwitch v${ss.version()}`)

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

if (VERSION === '0.0.0') {
throw new Error('version should be set before publishing')
}

console.info('Smoke Testing PASSED!')
4 changes: 2 additions & 2 deletions tests/from-event-type.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env ts-node
#!/usr/bin/env node --loader ts-node/esm

import { test } from 'tstest'
import {
Expand All @@ -13,7 +13,7 @@ import {
} from 'rxjs'
import { HasEventTargetAddRemove } from 'rxjs/internal/observable/fromEvent'

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

test('StateSwitch satisfy DOM EventTarget: HasEventTargetAddRemove', async t => {
const state = new StateSwitch()
Expand Down
7 changes: 7 additions & 0 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"outDir": "dist/cjs",
},
}
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"target": "es2020",
"module": "es2020",
"moduleResolution": "node",
"outDir": "dist",
"outDir": "dist/esm",
"declaration": true,
"sourceMap": true,
"noLib": false,
Expand Down