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

Feat/support dual esm cjs #193

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
72 changes: 36 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@

> Tiny 200b functional event emitter / pubsub.

- **Microscopic:** weighs less than 200 bytes gzipped
- **Useful:** a wildcard `"*"` event type listens to all events
- **Familiar:** same names & ideas as [Node's EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter)
- **Functional:** methods don't rely on `this`
- **Great Name:** somehow [mitt](https://npm.im/mitt) wasn't taken
* **Microscopic:** weighs less than 200 bytes gzipped
* **Useful:** a wildcard `"*"` event type listens to all events
* **Familiar:** same names & ideas as [Node's EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter)
* **Functional:** methods don't rely on `this`
* **Great Name:** somehow [mitt](https://npm.im/mitt) wasn't taken

Mitt was made for the browser, but works in any JavaScript runtime. It has no dependencies and supports IE9+.

## Table of Contents

- [Install](#install)
- [Usage](#usage)
- [Examples & Demos](#examples--demos)
- [API](#api)
- [Contribute](#contribute)
- [License](#license)
* [Install](#install)
* [Usage](#usage)
* [Examples & Demos](#examples--demos)
* [API](#api)
* [Contribute](#contribute)
* [License](#license)

## Install

Expand Down Expand Up @@ -118,28 +118,28 @@ const emitter: Emitter<Events> = mitt<Events>();
<img src="https://i.imgur.com/CjBgOfJ.png" width="278" alt="preact + mitt preview">
</a>

* * *
***

## API

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

#### Table of Contents

- [mitt](#mitt)
- [all](#all)
- [on](#on)
- [Parameters](#parameters)
- [off](#off)
- [Parameters](#parameters-1)
- [emit](#emit)
- [Parameters](#parameters-2)
* [mitt](#mitt)
* [all](#all)
* [on](#on)
* [Parameters](#parameters)
* [off](#off)
* [Parameters](#parameters-1)
* [emit](#emit)
* [Parameters](#parameters-2)

### mitt

Mitt: Tiny (~200b) functional event emitter / pubsub.

Returns **Mitt**
Returns **Mitt**&#x20;

### all

Expand All @@ -151,8 +151,8 @@ Register an event handler for the given type.

#### Parameters

- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to listen for, or `'*'` for all events
- `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Function to call in response to given event
* `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to listen for, or `'*'` for all events
* `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Function to call in response to given event

### off

Expand All @@ -161,8 +161,8 @@ If `handler` is omitted, all handlers of the given type are removed.

#### Parameters

- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to unregister `handler` from, or `'*'`
- `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)?** Handler function to remove
* `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to unregister `handler` from (`'*'` to remove a wildcard handler)
* `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)?** Handler function to remove

### emit

Expand All @@ -173,8 +173,8 @@ Note: Manually firing '\*' handlers is not supported.

#### Parameters

- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** The event type to invoke
- `evt` **Any?** Any value (object is recommended and powerful), passed to each handler
* `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** The event type to invoke
* `evt` **Any?** Any value (object is recommended and powerful), passed to each handler

## Contribute

Expand All @@ -190,15 +190,15 @@ If don't, just open a [new clear and descriptive issue](../../issues/new).

Pull requests are the greatest contributions, so be sure they are focused in scope, and do avoid unrelated commits.

- Fork it!
- Clone your fork: `git clone https://github.com/<your-username>/mitt`
- Navigate to the newly cloned directory: `cd mitt`
- Create a new branch for the new feature: `git checkout -b my-new-feature`
- Install the tools necessary for development: `npm install`
- Make your changes.
- Commit your changes: `git commit -am 'Add some feature'`
- Push to the branch: `git push origin my-new-feature`
- Submit a pull request with full remarks documenting your changes.
* Fork it!
* Clone your fork: `git clone https://github.com/<your-username>/mitt`
* Navigate to the newly cloned directory: `cd mitt`
* Create a new branch for the new feature: `git checkout -b my-new-feature`
* Install the tools necessary for development: `npm install`
* Make your changes.
* Commit your changes: `git commit -am 'Add some feature'`
* Push to the branch: `git push origin my-new-feature`
* Submit a pull request with full remarks documenting your changes.

## License

Expand Down
37 changes: 22 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,33 @@
"name": "mitt",
"version": "3.0.1",
"description": "Tiny 200b functional Event Emitter / pubsub.",
"module": "dist/mitt.mjs",
"main": "dist/mitt.js",
"jsnext:main": "dist/mitt.mjs",
"umd:main": "dist/mitt.umd.js",
"source": "src/index.ts",
"typings": "index.d.ts",
"module": "dist/esm/mitt.mjs",
"main": "dist/cjs/mitt.js",
"umd:main": "dist/mitt.umd.js",
"types": "dist/esm/mitt.d.ts",
"exports": {
"types": "./index.d.ts",
"module": "./dist/mitt.mjs",
"import": "./dist/mitt.mjs",
"require": "./dist/mitt.js",
"default": "./dist/mitt.mjs"
".": {
"import": {
"types": "./dist/esm/mitt.d.ts",
"default": "./dist/esm/mitt.mjs"
},
"require": {
"types": "./dist/cjs/mitt.d.ts",
"default": "./dist/cjs/mitt.js"
}
}
},
"scripts": {
"test": "npm-run-all --silent typecheck lint mocha test-types",
"mocha": "mocha test",
"test-types": "tsc test/test-types-compilation.ts --noEmit --strict",
"lint": "eslint src test --ext ts --ext js",
"typecheck": "tsc --noEmit",
"bundle": "microbundle -f es,cjs,umd",
"build": "npm-run-all --silent clean -p bundle -s docs",
"bundle:cjs": "tsup --config ./tsup.config.cjs.ts",
"bundle:esm": "tsup --config ./tsup.config.esm.ts",
"bundle:umd": "tsup --config ./tsup.config.umd.ts",
"build": "npm-run-all --silent clean -p bundle:* -s docs && tsconfig-to-dual-package ./tsconfig.cjs.json ./tsconfig.esm.json",
"clean": "rimraf dist",
"docs": "documentation readme src/index.ts --section API -q --parse-extension ts",
"release": "npm run -s build -s && npm t && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish"
Expand All @@ -40,8 +46,7 @@
],
"license": "MIT",
"files": [
"dist",
"index.d.ts"
"dist"
],
"mocha": {
"extension": [
Expand Down Expand Up @@ -80,6 +85,8 @@
"sinon": "^9.0.2",
"sinon-chai": "^3.5.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
"tsconfig-to-dual-package": "^1.2.0",
"tsup": "^8.0.1",
"typescript": "^5.0.0"
}
}
8 changes: 8 additions & 0 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"moduleResolution": "Node10",
"outDir": "dist/cjs"
}
}
7 changes: 7 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "ESNext",
"outDir": "dist/esm"
}
}
10 changes: 4 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
"strict": true,
"noEmit": true,
"declaration": true,
"moduleResolution": "node",
"module": "ESNext",
"moduleResolution": "Bundler",
"skipLibCheck": true,
"esModuleInterop": true,
"esModuleInterop": true
},
"include": [
"src/*.ts",
"test/*.ts"
]
"include": ["src/*.ts", "test/*.ts"]
}
16 changes: 16 additions & 0 deletions tsup.config.cjs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineConfig } from 'tsup';

export default defineConfig({
entry: {
mitt: 'src/index.ts'
},
format: 'cjs',
outDir: 'dist/cjs',
dts: true,
tsconfig: './tsconfig.cjs.json',
target: 'safari11',
splitting: true,
sourcemap: true,
cjsInterop: true,
clean: true
});
15 changes: 15 additions & 0 deletions tsup.config.esm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineConfig } from 'tsup';

export default defineConfig({
entry: {
mitt: 'src/index.ts'
},
format: 'esm',
outDir: 'dist/esm',
dts: true,
tsconfig: './tsconfig.esm.json',
target: 'safari11',
splitting: false,
sourcemap: true,
clean: true
});
18 changes: 18 additions & 0 deletions tsup.config.umd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { defineConfig } from 'tsup';

export default defineConfig({
entry: {
mitt: 'src/index.ts'
},
format: 'iife',
outDir: 'dist',
globalName: 'mitt',
target: 'safari11',
splitting: false,
sourcemap: true,
clean: true,
outExtension: () => ({ js: '.umd.js' }),
footer: () => ({
js: 'Object.assign(window,{mitt:mitt.default})'
})
});