Skip to content

Commit

Permalink
feat: provide package as plain ES module
Browse files Browse the repository at this point in the history
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'`.
  • Loading branch information
kleinfreund committed Aug 16, 2021
1 parent aa6fb29 commit 57e4333
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 66 deletions.
53 changes: 53 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
118 changes: 72 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<script type="module">
import { poll } from './poll.js'
```js
import poll from 'poll/dist/poll.js'
function fn() {
console.log('Hello, beautiful!')
}
function fn() {
console.log('Hello, beautiful!')
}

poll(fn, 1000)
```
poll(fn, 1000)
</script>
```

## Documentation

Expand Down Expand Up @@ -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
```
2 changes: 1 addition & 1 deletion dist/poll.js
Original file line number Diff line number Diff line change
@@ -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};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"async",
"await"
],
"type": "module",
"exports": "./dist/poll.js",
"main": "./dist/poll.js",
"module": "./dist/poll.js",
Expand Down
23 changes: 9 additions & 14 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -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(),
],
}
2 changes: 1 addition & 1 deletion src/poll.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import poll from './poll'
import { poll } from './poll'

describe('poll', () => {
beforeEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 57e4333

Please sign in to comment.