diff --git a/README.md b/README.md index 0219f3a..e49e485 100644 --- a/README.md +++ b/README.md @@ -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 @@ -118,7 +118,7 @@ const emitter: Emitter = mitt(); preact + mitt preview -* * * +*** ## API @@ -126,20 +126,20 @@ const emitter: Emitter = mitt(); #### 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** ### 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 @@ -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 @@ -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 @@ -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//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//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 diff --git a/package.json b/package.json index e51397c..0744a42 100644 --- a/package.json +++ b/package.json @@ -2,18 +2,22 @@ "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", @@ -21,8 +25,10 @@ "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" @@ -40,8 +46,7 @@ ], "license": "MIT", "files": [ - "dist", - "index.d.ts" + "dist" ], "mocha": { "extension": [ @@ -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" } } diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json new file mode 100644 index 0000000..ba04861 --- /dev/null +++ b/tsconfig.cjs.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "CommonJS", + "moduleResolution": "Node10", + "outDir": "dist/cjs" + } +} diff --git a/tsconfig.esm.json b/tsconfig.esm.json new file mode 100644 index 0000000..0d924d0 --- /dev/null +++ b/tsconfig.esm.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "ESNext", + "outDir": "dist/esm" + } +} diff --git a/tsconfig.json b/tsconfig.json index 6d1b988..9686a23 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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"] } diff --git a/tsup.config.cjs.ts b/tsup.config.cjs.ts new file mode 100644 index 0000000..f57eb40 --- /dev/null +++ b/tsup.config.cjs.ts @@ -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 +}); diff --git a/tsup.config.esm.ts b/tsup.config.esm.ts new file mode 100644 index 0000000..084dc6b --- /dev/null +++ b/tsup.config.esm.ts @@ -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 +}); diff --git a/tsup.config.umd.ts b/tsup.config.umd.ts new file mode 100644 index 0000000..6f127df --- /dev/null +++ b/tsup.config.umd.ts @@ -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})' + }) +});