From 57e433381733c17aae66c378105eecd118998e6a Mon Sep 17 00:00:00 2001 From: kleinfreund Date: Mon, 16 Aug 2021 19:10:16 +0200 Subject: [PATCH] feat: provide package as plain ES module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BREAKING CHANGE: Changes the package’s distribution format from UMD to ES module. BREAKING CHANGE: Changes the exported `poll` function from a default export to a named export. Update your code by replacing `import poll from 'poll'` with `import { poll } from 'poll'`. --- CHANGELOG.md | 53 +++++++++++++++++++++ README.md | 118 +++++++++++++++++++++++++++++------------------ dist/poll.js | 2 +- package.json | 1 + rollup.config.js | 23 ++++----- src/poll.test.ts | 2 +- src/poll.ts | 2 +- tsconfig.json | 6 +-- 8 files changed, 141 insertions(+), 66 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..82a8330 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,53 @@ +# Changelog + +## [3.0.0](https://github.com/kleinfreund/poll/compare/v2.0.1...v3.0.0) (2021-08-16) + +### Breaking changes + +- Changes the package’s distribution format from UMD to ES module. +- Changes the exported `poll` function from a default export to a named export. Update your code by replacing `import poll from 'poll'` with `import { poll } from 'poll'`. + +## [2.0.1](https://github.com/kleinfreund/poll/compare/v2.0.0...v2.0.1) (2021-03-27) + +### Bug fixes + +- Fixes incorrect usage instructions in README.md file. + +## [2.0.0](https://github.com/kleinfreund/poll/compare/v1.0.1...v2.0.0) (2021-03-21) + +### Breaking changes + +- Removes the separate ESM and CJS bundles in favour of one UMD bundle that can be used more easily in most scenarios while keeping the bundle size down. The bundle in the dist directory is now also minified. + +## [1.0.1](https://github.com/kleinfreund/poll/compare/v1.0.0...v1.0.1) (2019-08-23) + +### Bug fixes + +- Removes safe guard preventing use of `poll` function with negative `delay` values. + +## [1.0.0](https://github.com/kleinfreund/poll/compare/v0.1.0...v1.0.0) (2019-08-04) + +### Breaking changes + +- Importing the CommonJS module with `require('poll')` no longer resolves to the `poll` function. Use `require('poll').default` instead. + + ```node + const poll = require('poll').default; + ``` + +**Non-breaking changes**: + +- The package is now available as an ES module. If you use `poll` as a dependency, import it like this: + + ```js + import poll from 'poll/dist/esm/poll.mjs'; + ``` + +- Tests now use Jest instead of Ava. +- Tests are now based on fake timers instead of calling `setTimeout` in the tests directly. Unfortunately, tests still require a lot of trickery to manually clear out the promise queue. If you know how to test this without sprinkling `await Promise.resolve()` all over my tests, I’m all ears! + +## 0.1.0 (2019-08-03) + +### Features + +- Releases initial version of the poll package. diff --git a/README.md b/README.md index f45d87b..72f63ee 100644 --- a/README.md +++ b/README.md @@ -8,72 +8,58 @@ Links: - [on BundlePhobia](https://bundlephobia.com/result?p=poll) - [**github.com**/kleinfreund/poll](https://github.com/kleinfreund/poll) - - ## Contents -- [Installation](#installation) +- [Installation & usage](#installation--usage) - [Documentation](#documentation) - [Examples](#examples) - [Versioning](#versioning) +- [Update package version](#update-package-version) +## Installation & usage +### npm package -## Installation - -### Browser - -Download the UMD bundle file … +1. Install the `poll` package. -```sh -curl -O https://raw.githubusercontent.com/kleinfreund/poll/main/dist/poll.js -``` + ```sh + npm install poll + ``` -… and use it like this: +2. Import the `poll` function and use it. -```js -const poll = window.poll.default + ```js + // “poll” is mapped to “poll/dist/poll.js” by Node.js via the package’s “exports” field. + import { poll } from 'poll' -function fn() { - console.log('Hello, beautiful!') -} - -poll(fn, 1000) -``` - -### Node - -Install the npm package as a dependency … - -```sh -npm install poll -``` + function fn() { + console.log('Hello, beautiful!') + } -… and import it like this: + poll(fn, 1000) + ``` -- CommonJS module +### Plain file - ```js - const poll = require('poll').default +1. Download the `poll` module. - function fn() { - console.log('Hello, beautiful!') - } + ```sh + curl -O https://raw.githubusercontent.com/kleinfreund/poll/main/dist/poll.js + ``` - poll(fn, 1000) - ``` +2. Import the `poll` function and use it. -- ES module + ```html + + ``` ## Documentation @@ -140,3 +126,43 @@ poll(fn, 50, shouldStopPolling) ## Versioning This package uses [semantic versioning](https://semver.org). + +## Update package version + +1. Make some changes and run the tests and the build script. + + ```sh + npm test + npm run build + ``` + +2. Commit the changes. +3. Verify that you’re authenticated with npm. + + ```sh + npm whomai + ``` + + If you’re not authenticated, do so using `npm login`. + +4. Change the package’s version locally. + + ```sh + # See `npm version --help` for more options + npm version minor + ``` + + This changes the version number in the package.json file and adds a new git tag matching the new version. + +5. Push your changes and the updated git tags separately. + + ```sh + git push + git push --tags + ``` + +6. Publish the package. + + ```sh + npm publish + ``` diff --git a/dist/poll.js b/dist/poll.js index 29f4a74..9f5a129 100644 --- a/dist/poll.js +++ b/dist/poll.js @@ -1 +1 @@ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).poll={})}(this,(function(e){"use strict";e.default=async function(e,t,o=(()=>!1)){t=Math.max(0,t);do{if(await e(),o())break;await new Promise((e=>setTimeout(e,t)))}while(!o())},Object.defineProperty(e,"__esModule",{value:!0})})); +async function a(a,e,i=(()=>!1)){e=Math.max(0,e);do{if(await a(),i())break;await new Promise((a=>setTimeout(a,e)))}while(!i())}export{a as poll}; diff --git a/package.json b/package.json index 0625393..afd25b6 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "async", "await" ], + "type": "module", "exports": "./dist/poll.js", "main": "./dist/poll.js", "module": "./dist/poll.js", diff --git a/rollup.config.js b/rollup.config.js index 53d7a83..69b7dff 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,18 +1,13 @@ import typescript from '@rollup/plugin-typescript' import { terser } from 'rollup-plugin-terser' -export default [ - { - input: 'src/poll.ts', - output: { - format: 'umd', - name: 'poll', - exports: 'named', - file: 'dist/poll.js', - }, - plugins: [ - typescript(), - terser(), - ], +export default { + input: 'src/poll.ts', + output: { + dir: 'dist', }, -] + plugins: [ + typescript(), + terser(), + ], +} diff --git a/src/poll.test.ts b/src/poll.test.ts index f60bc39..8160fcb 100644 --- a/src/poll.test.ts +++ b/src/poll.test.ts @@ -1,4 +1,4 @@ -import poll from './poll' +import { poll } from './poll' describe('poll', () => { beforeEach(() => { diff --git a/src/poll.ts b/src/poll.ts index e2d9159..cecf029 100644 --- a/src/poll.ts +++ b/src/poll.ts @@ -5,7 +5,7 @@ * @param delay The delay (in milliseconds) to wait before calling the function again. * @param shouldStopPolling A callback function indicating whether to stop polling. */ -export default async function poll( +export async function poll( fn: () => any, delay: number, shouldStopPolling: () => boolean = () => false diff --git a/tsconfig.json b/tsconfig.json index 6c84b5a..ef468cd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,9 +1,9 @@ { "compilerOptions": { "strict": true, - "target": "es2020", - "module": "esnext", - "moduleResolution": "node", + "target": "ES2020", + "module": "ES2020", + "moduleResolution": "Node", "esModuleInterop": true, "noFallthroughCasesInSwitch": true, "noImplicitAny": true,